Gopikanth123 commited on
Commit
834d8bd
·
verified ·
1 Parent(s): c50f268

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -15
app.py CHANGED
@@ -1,21 +1,48 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
- import numpy as np
4
 
5
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- def transcribe(audio):
8
- sr, y = audio
9
- y = y.astype(np.float32)
10
- y /= np.max(np.abs(y))
11
 
12
- return transcriber({"sampling_rate": sr, "raw": y})["text"]
 
 
 
 
 
 
13
 
 
 
 
14
 
15
- demo = gr.Interface(
16
- transcribe,
17
- gr.Audio(sources=["microphone"]),
18
- "text",
19
- )
20
 
21
- demo.launch()
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import streamlit.components.v1 as components
 
3
 
4
+ # HTML and JavaScript for Speech Recognition using webkitSpeechRecognition
5
+ speech_recognition_html = """
6
+ <html>
7
+ <body>
8
+ <button onclick="startRecognition()">Start Speech Recognition</button>
9
+ <p id="output">Speak something...</p>
10
+
11
+ <script>
12
+ var recognition = new webkitSpeechRecognition();
13
+ recognition.continuous = false; // Stops after speech input
14
+ recognition.interimResults = true;
15
+
16
+ recognition.onresult = function(event) {
17
+ var transcript = event.results[event.resultIndex][0].transcript;
18
+ document.getElementById('output').textContent = transcript;
19
+ // Send transcript back to Streamlit using postMessage
20
+ window.parent.postMessage({func: 'update_output', transcript: transcript}, '*');
21
+ };
22
 
23
+ recognition.onerror = function(event) {
24
+ console.error("Speech recognition error", event.error);
25
+ document.getElementById('output').textContent = "Error in recognition";
26
+ };
27
 
28
+ function startRecognition() {
29
+ recognition.start();
30
+ }
31
+ </script>
32
+ </body>
33
+ </html>
34
+ """
35
 
36
+ # Streamlit UI
37
+ st.title("Speech-to-Text Demo")
38
+ st.write("Click the button below and start speaking. The recognized text will be shown here:")
39
 
40
+ # Display the HTML with the embedded speech recognition
41
+ components.html(speech_recognition_html, height=200)
 
 
 
42
 
43
+ # Output area where the recognized speech will be displayed
44
+ output = st.empty()
45
+
46
+ # The Streamlit component listens for the postMessage event and updates the output
47
+ st.write("Recognized Text:")
48
+ transcript = st.text_area("Transcript:", "", height=150)