Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|