Futuresony commited on
Commit
a71a723
·
verified ·
1 Parent(s): d40a7a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -33
app.py CHANGED
@@ -41,7 +41,7 @@ def respond(message, history: list[tuple[str, str]], system_message):
41
  # Gradio interface setup
42
  with gr.Blocks() as demo:
43
  # Chatbot Interface
44
- chatbot = gr.Chatbot()
45
  state = gr.State([])
46
  system_message = gr.Textbox(
47
  value="You are a helpful assistant.",
@@ -73,38 +73,40 @@ with gr.Blocks() as demo:
73
  outputs=[chatbot, state],
74
  )
75
 
76
- # Custom JavaScript for Speech Recognition
77
- speech_recognition_js = """
78
- let micButton = document.getElementById('mic_button');
79
- let userInputBox = document.getElementById('user_input_box');
80
- let recognition = null;
81
-
82
- if ('webkitSpeechRecognition' in window) {
83
- recognition = new webkitSpeechRecognition();
84
- recognition.continuous = false;
85
- recognition.interimResults = false;
86
-
87
- recognition.onresult = function(event) {
88
- const transcript = event.results[0][0].transcript;
89
- userInputBox.value = transcript;
90
- };
91
-
92
- recognition.onspeechend = function() {
93
- recognition.stop(); // Stop listening after 2 seconds of silence
94
- };
95
-
96
- micButton.onclick = function() {
97
- recognition.start(); // Start listening when mic button is clicked
98
- };
99
- } else {
100
- micButton.onclick = function() {
101
- alert("Speech recognition is not supported in this browser.");
102
- };
103
- }
104
- """
105
-
106
- # Inject JavaScript into the app
107
- demo.load(_js=speech_recognition_js)
 
108
 
109
  if __name__ == "__main__":
110
  demo.launch()
 
 
41
  # Gradio interface setup
42
  with gr.Blocks() as demo:
43
  # Chatbot Interface
44
+ chatbot = gr.Chatbot(type="messages") # Use 'messages' format for compatibility
45
  state = gr.State([])
46
  system_message = gr.Textbox(
47
  value="You are a helpful assistant.",
 
73
  outputs=[chatbot, state],
74
  )
75
 
76
+ # Inject JavaScript for Speech Recognition using Gradio's HTML component
77
+ gr.HTML(
78
+ """
79
+ <script>
80
+ let micButton = document.getElementById('mic_button');
81
+ let userInputBox = document.getElementById('user_input_box');
82
+ let recognition = null;
83
+
84
+ if ('webkitSpeechRecognition' in window) {
85
+ recognition = new webkitSpeechRecognition();
86
+ recognition.continuous = false;
87
+ recognition.interimResults = false;
88
+
89
+ recognition.onresult = function(event) {
90
+ const transcript = event.results[0][0].transcript;
91
+ userInputBox.value = transcript;
92
+ };
93
+
94
+ recognition.onspeechend = function() {
95
+ recognition.stop(); // Stop listening after 2 seconds of silence
96
+ };
97
+
98
+ micButton.onclick = function() {
99
+ recognition.start(); // Start listening when mic button is clicked
100
+ };
101
+ } else {
102
+ micButton.onclick = function() {
103
+ alert("Speech recognition is not supported in this browser.");
104
+ };
105
+ }
106
+ </script>
107
+ """
108
+ )
109
 
110
  if __name__ == "__main__":
111
  demo.launch()
112
+