Joe7oo7 commited on
Commit
9a415c7
·
verified ·
1 Parent(s): 846ed04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -52
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import gradio as gr
2
- import speech_recognition as sr
3
  from huggingface_hub import InferenceClient
4
 
5
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
@@ -36,29 +35,6 @@ def respond(
36
  response += token
37
  yield response
38
 
39
- # Voice recognition function with error handling
40
- def recognize_speech():
41
- recognizer = sr.Recognizer()
42
- with sr.Microphone() as source:
43
- print("Listening...")
44
- try:
45
- audio = recognizer.listen(source, timeout=5)
46
- print("Recognizing...")
47
- text = recognizer.recognize_google(audio)
48
- print(f"Recognized: {text}")
49
- return text
50
- except sr.WaitTimeoutError:
51
- return "Error: Listening timed out."
52
- except sr.UnknownValueError:
53
- return "Error: Could not understand the audio."
54
- except sr.RequestError:
55
- return "Error: Could not request results from Google Speech Recognition service."
56
-
57
- # Gradio interface with a button to trigger voice recognition
58
- def get_voice_input():
59
- voice_input = recognize_speech()
60
- return voice_input
61
-
62
  # Define custom CSS
63
  custom_css = """
64
  /* Add your custom CSS styles here */
@@ -93,35 +69,47 @@ body {
93
  }
94
  """
95
 
96
- # Create a Gradio interface
97
- with gr.Blocks() as demo:
98
- gr.Markdown("## Voice Recognition Chatbot")
99
-
100
- # Voice recognition button
101
- voice_button = gr.Button("🎤 Speak").click(fn=get_voice_input)
102
 
103
- # Text input and Chat Interface
104
- text_input = gr.Textbox(label="Your Message")
105
- chatbot = gr.ChatInterface(
106
- fn=respond,
107
- inputs=[text_input],
108
- additional_inputs=[
109
- gr.Textbox(value="You are a Chatbot.Your name is Evy.Your are Developed By Joe.", label="System message"),
110
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
111
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
112
- gr.Slider(
113
- minimum=0.1,
114
- maximum=1.0,
115
- value=0.95,
116
- step=0.05,
117
- label="Top-p (nucleus sampling)",
118
- ),
119
- ],
120
- css=custom_css
121
- )
122
-
123
- # Link voice input to text input
124
- voice_button.click(fn=get_voice_input, outputs=text_input)
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  if __name__ == "__main__":
127
  demo.launch()
 
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
35
  response += token
36
  yield response
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Define custom CSS
39
  custom_css = """
40
  /* Add your custom CSS styles here */
 
69
  }
70
  """
71
 
72
+ # JavaScript for voice recognition
73
+ custom_js = """
74
+ <script>
75
+ function startRecognition() {
76
+ var recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
77
+ recognition.lang = 'en-US';
78
 
79
+ recognition.onresult = function(event) {
80
+ var speechResult = event.results[0][0].transcript;
81
+ document.getElementById('input_message').value = speechResult;
82
+ };
83
+
84
+ recognition.start();
85
+ }
86
+ </script>
87
+ """
88
+
89
+ # Create a Gradio chat interface with custom CSS and JS
90
+ demo = gr.ChatInterface(
91
+ fn=respond,
92
+ additional_inputs=[
93
+ gr.Textbox(value="You are a Chatbot.Your name is Evy.Your are Developed By Joe.", label="System message"),
94
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
95
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
96
+ gr.Slider(
97
+ minimum=0.1,
98
+ maximum=1.0,
99
+ value=0.95,
100
+ step=0.05,
101
+ label="Top-p (nucleus sampling)",
102
+ ),
103
+ ],
104
+ css=custom_css,
105
+ javascript=custom_js
106
+ )
107
+
108
+ # Add a button to start voice recognition
109
+ def add_voice_button():
110
+ return gr.Button("🎤 Speak").onclick(startRecognition)
111
+
112
+ demo.add_component(add_voice_button(), "left", component_id="input_message")
113
 
114
  if __name__ == "__main__":
115
  demo.launch()