File size: 3,409 Bytes
f3adb3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Gradio Video Upload with Progress</title>
  <style>
    #progress-container {
      width: 100%;
      background-color: #f3f3f3;
      border: 1px solid #ccc;
      margin-top: 10px;
      display: none;
    }
    #progress-bar {
      width: 0%;
      height: 20px;
      background-color: #4caf50;
      text-align: center;
      color: white;
      line-height: 20px;
      transition: width 0.3s;
    }
    pre {
      background: #f3f3f3;
      padding: 10px;
      border: 1px solid #ccc;
      overflow-x: auto;
      white-space: pre-wrap;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <h1>Upload Video and Process via Gradio API</h1>
  
  <!-- Formulář pro nahrání videa -->
  <form id="uploadForm" enctype="multipart/form-data">
    <label for="video">Choose a video file:</label>
    <input type="file" id="video" name="video" accept="video/*" required>
    <button type="submit">Upload and Process</button>
  </form>
  
  <!-- Progress bar -->
  <div id="progress-container">
    <div id="progress-bar">0%</div>
  </div>
  
  <!-- Debug výpis -->
  <h2>Debug Output:</h2>
  <pre id="debug">Waiting for upload...</pre>

  <script>
    document.getElementById('uploadForm').addEventListener('submit', async (event) => {
      event.preventDefault(); // Zabrání obnovení stránky

      const fileInput = document.getElementById('video');
      const file = fileInput.files[0];
      const debugOutput = document.getElementById('debug');
      const progressBar = document.getElementById('progress-bar');
      const progressContainer = document.getElementById('progress-container');
      
      if (!file) {
        alert('Please select a video file to upload.');
        return;
      }

      debugOutput.textContent = "Starting upload...\n";
      progressContainer.style.display = "block";

      const formData = new FormData();
      formData.append('data', file);

      const apiUrl = "http://127.0.0.1:7860/api/predict/";

      try {
        const xhr = new XMLHttpRequest();

        xhr.open("POST", apiUrl, true);

        // Aktualizace progress baru
        xhr.upload.addEventListener("progress", (event) => {
          if (event.lengthComputable) {
            const percentComplete = Math.round((event.loaded / event.total) * 100);
            progressBar.style.width = percentComplete + "%";
            progressBar.textContent = percentComplete + "%";
          }
        });

        // Zpracování odpovědi
        xhr.onload = function () {
          if (xhr.status === 200) {
            debugOutput.textContent += "Upload complete! Server response:\n" + xhr.responseText;
            progressBar.style.width = "100%";
            progressBar.textContent = "Done";
          } else {
            debugOutput.textContent += `Error: ${xhr.statusText}\nServer response:\n${xhr.responseText}`;
          }
        };

        xhr.onerror = function () {
          debugOutput.textContent += "Error during the request.";
        };

        xhr.send(formData);
        debugOutput.textContent += "File is being uploaded...\n";

      } catch (error) {
        debugOutput.textContent += `Unexpected error: ${error.message}`;
        console.error("Error:", error);
      }
    });
  </script>
</body>
</html>