Joe7oo7 commited on
Commit
a456ebe
·
verified ·
1 Parent(s): 8021fc0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -17
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
 
4
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
@@ -35,6 +36,29 @@ def respond(
35
  response += token
36
  yield response
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Define custom CSS
39
  custom_css = """
40
  /* Add your custom CSS styles here */
@@ -69,23 +93,35 @@ body {
69
  }
70
  """
71
 
72
- # Create a Gradio chat interface with custom CSS
73
- demo = gr.ChatInterface(
74
- fn=respond,
75
- additional_inputs=[
76
- gr.Textbox(value="You are a Chatbot.Your name is Evy.Your are Developed By Joe.", label="System message"),
77
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
78
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
79
- gr.Slider(
80
- minimum=0.1,
81
- maximum=1.0,
82
- value=0.95,
83
- step=0.05,
84
- label="Top-p (nucleus sampling)",
85
- ),
86
- ],
87
- css=custom_css
88
- )
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  if __name__ == "__main__":
91
  demo.launch()
 
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
  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
  }
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()