Mudassir939 commited on
Commit
ad02f33
·
verified ·
1 Parent(s): c1b42aa

Upload 4 files

Browse files
Files changed (4) hide show
  1. gemini-api.js +72 -0
  2. index.html +29 -19
  3. main.js +48 -0
  4. style.css +74 -28
gemini-api.js ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Calls the given Gemini model with the given image and/or text
3
+ * parts, streaming output (as a generator function).
4
+ */
5
+ export async function* streamGemini({
6
+ model = 'gemini-pro', // use 'gemini-pro' for text -> text
7
+ contents = [],
8
+ } = {}) {
9
+ // Send the prompt to the Python backend
10
+ // Call API defined in main.py
11
+ let response = await fetch("/api/generate", {
12
+ method: "POST",
13
+ headers: { "content-type": "application/json" },
14
+ body: JSON.stringify({ model, contents })
15
+ });
16
+
17
+ yield* streamResponseChunks(response);
18
+ }
19
+
20
+ /**
21
+ * A helper that streams text output chunks from a fetch() response.
22
+ */
23
+ async function* streamResponseChunks(response) {
24
+ let buffer = '';
25
+
26
+ const CHUNK_SEPARATOR = '\n\n';
27
+
28
+ let processBuffer = async function* (streamDone = false) {
29
+ while (true) {
30
+ let flush = false;
31
+ let chunkSeparatorIndex = buffer.indexOf(CHUNK_SEPARATOR);
32
+ if (streamDone && chunkSeparatorIndex < 0) {
33
+ flush = true;
34
+ chunkSeparatorIndex = buffer.length;
35
+ }
36
+ if (chunkSeparatorIndex < 0) {
37
+ break;
38
+ }
39
+
40
+ let chunk = buffer.substring(0, chunkSeparatorIndex);
41
+ buffer = buffer.substring(chunkSeparatorIndex + CHUNK_SEPARATOR.length);
42
+ chunk = chunk.replace(/^data:\s*/, '').trim();
43
+ if (!chunk) {
44
+ if (flush) break;
45
+ continue;
46
+ }
47
+ let { error, text } = JSON.parse(chunk);
48
+ if (error) {
49
+ console.error(error);
50
+ throw new Error(error?.message || JSON.stringify(error));
51
+ }
52
+ yield text;
53
+ if (flush) break;
54
+ }
55
+ };
56
+
57
+ const reader = response.body.getReader();
58
+ try {
59
+ while (true) {
60
+ const { done, value } = await reader.read()
61
+ if (done) break;
62
+ buffer += new TextDecoder().decode(value);
63
+ console.log(new TextDecoder().decode(value));
64
+ yield* processBuffer();
65
+ }
66
+ } finally {
67
+ reader.releaseLock();
68
+ }
69
+
70
+ yield* processBuffer(true);
71
+ }
72
+
index.html CHANGED
@@ -1,19 +1,29 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html>
2
+ <html lang="en">
3
+
4
+ <head>
5
+ <meta charset="UTF-8" />
6
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
7
+ <title>Transcription_Bot</title>
8
+ <link rel="stylesheet" href="style.css">
9
+ <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
10
+ </head>
11
+
12
+ <body>
13
+ <main class="container">
14
+ <h1>Transcription_Bot</h1>
15
+ <!-- Form for uploading audio file -->
16
+ <form class="form-container" id="transcription-form" enctype="multipart/form-data">
17
+ <div class="file-upload-box">
18
+ <label for="audio-file">Upload Audio File:</label>
19
+ <input type="file" id="audio-file" name="audio_file" accept="audio/*" class="input-field">
20
+ </div>
21
+ <button type="submit" class="submit-button">Go</button>
22
+ </form>
23
+ <p class="output" id="output">(Results will appear here)</p>
24
+ </main>
25
+ <script type="module" src="/main.js"></script>
26
+ <script src="https://unpkg.com/[email protected]/dist/markdown-it.min.js"></script>
27
+ </body>
28
+
29
+ </html>
main.js ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { streamGemini } from './gemini-api.js';
2
+
3
+ let form = document.querySelector('form');
4
+ let output = document.querySelector('.output');
5
+ let audioFileInput = document.querySelector('input[name="audio_file"]');
6
+
7
+ form.onsubmit = async (ev) => {
8
+ ev.preventDefault();
9
+ output.textContent = 'Processing...';
10
+
11
+ try {
12
+ // Log form data before sending
13
+ console.log('Preparing to send audio file:', audioFileInput.files[0]);
14
+
15
+ // Create a FormData object to hold the audio file
16
+ let formData = new FormData();
17
+ formData.append('audio_file', audioFileInput.files[0]);
18
+
19
+ // Send the audio file to the Flask backend for transcription and option selection
20
+ let response = await fetch('/api/upload', {
21
+ method: 'POST',
22
+ body: formData
23
+ });
24
+
25
+ // Log the response status
26
+ console.log('Response status:', response.status);
27
+
28
+ if (!response.ok) {
29
+ throw new Error('Network response was not ok');
30
+ }
31
+
32
+ let result = await response.json();
33
+ let selectedOption = result.selected_option;
34
+
35
+ // Log the response data
36
+ console.log('Received result:', result);
37
+
38
+ // Display the transcription and the selected option
39
+ output.innerHTML = `
40
+ <p>Transcription: ${result.transcription}</p>
41
+ <p>Correct Option: ${selectedOption}</p>
42
+ `;
43
+
44
+ } catch (e) {
45
+ console.error('Error occurred:', e);
46
+ output.innerHTML = '<hr>' + e;
47
+ }
48
+ };
style.css CHANGED
@@ -1,28 +1,74 @@
1
- body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
4
- }
5
-
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
9
- }
10
-
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
16
- }
17
-
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
- }
25
-
26
- .card p:last-child {
27
- margin-bottom: 0;
28
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Global Styles */
2
+ body {
3
+ font-family: 'Roboto', sans-serif;
4
+ background-color: #f4f4f9;
5
+ color: #333;
6
+ margin: 0;
7
+ padding: 0;
8
+ display: flex;
9
+ justify-content: center;
10
+ align-items: center;
11
+ height: 100vh;
12
+ }
13
+
14
+ /* Container */
15
+ .container {
16
+ max-width: 600px;
17
+ width: 100%;
18
+ background-color: #fff;
19
+ padding: 20px;
20
+ border-radius: 8px;
21
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
22
+ text-align: center;
23
+ }
24
+
25
+ /* Header */
26
+ h1 {
27
+ font-weight: 700;
28
+ color: #4a90e2;
29
+ margin-bottom: 20px;
30
+ }
31
+
32
+ /* Form Container */
33
+ .form-container {
34
+ margin-bottom: 20px;
35
+ }
36
+
37
+ /* Input Field */
38
+ .input-field {
39
+ width: calc(100% - 100px);
40
+ padding: 10px;
41
+ border-radius: 4px;
42
+ border: 1px solid #ddd;
43
+ font-size: 16px;
44
+ margin-right: 10px;
45
+ box-sizing: border-box;
46
+ }
47
+
48
+ /* Submit Button */
49
+ .submit-button {
50
+ padding: 10px 20px;
51
+ border-radius: 4px;
52
+ border: none;
53
+ background-color: #4a90e2;
54
+ color: white;
55
+ font-size: 16px;
56
+ cursor: pointer;
57
+ transition: background-color 0.3s ease;
58
+ }
59
+
60
+ .submit-button:hover {
61
+ background-color: #357ab7;
62
+ }
63
+
64
+ /* Output Paragraph */
65
+ .output {
66
+ font-size: 16px;
67
+ color: #666;
68
+ margin-top: 20px;
69
+ padding: 10px;
70
+ background-color: #f9f9f9;
71
+ border-radius: 4px;
72
+ border: 1px solid #ddd;
73
+ }
74
+