Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
-
|
| 2 |
-
import speech_recognition as sr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
-
from io import BytesIO
|
| 5 |
|
| 6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 7 |
|
|
@@ -13,44 +11,29 @@ def respond(
|
|
| 13 |
temperature,
|
| 14 |
top_p,
|
| 15 |
):
|
| 16 |
-
|
| 17 |
-
messages = [{"role": "system", "content": system_message}]
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
response += token
|
| 38 |
-
yield response
|
| 39 |
-
|
| 40 |
-
except Exception as e:
|
| 41 |
-
yield f"An error occurred: {str(e)}"
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
with sr.AudioFile(audio_file) as source:
|
| 46 |
-
audio_data = recognizer.record(source)
|
| 47 |
-
try:
|
| 48 |
-
text = recognizer.recognize_google(audio_data)
|
| 49 |
-
return text
|
| 50 |
-
except sr.UnknownValueError:
|
| 51 |
-
return "Sorry, I could not understand the audio."
|
| 52 |
-
except sr.RequestError as e:
|
| 53 |
-
return f"Could not request results; {e}"
|
| 54 |
|
| 55 |
# Define custom CSS
|
| 56 |
custom_css = """
|
|
@@ -87,23 +70,22 @@ body {
|
|
| 87 |
"""
|
| 88 |
|
| 89 |
# Create a Gradio chat interface with custom CSS
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
submit_button.click(respond, inputs=[input_textbox, gr.State([]), system_msg, max_tokens, temperature, top_p], outputs=chatbot_output)
|
| 105 |
-
voice_input.change(lambda x: recognize_speech(BytesIO(x)), inputs=[voice_input], outputs=input_textbox)
|
| 106 |
-
|
| 107 |
-
# Launch the interface
|
| 108 |
if __name__ == "__main__":
|
| 109 |
-
demo.launch()
|
|
|
|
| 1 |
+
mport gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
|
|
|
| 11 |
temperature,
|
| 12 |
top_p,
|
| 13 |
):
|
| 14 |
+
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 15 |
|
| 16 |
+
for val in history:
|
| 17 |
+
if val[0]:
|
| 18 |
+
messages.append({"role": "user", "content": val[0]})
|
| 19 |
+
if val[1]:
|
| 20 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 21 |
|
| 22 |
+
messages.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
+
response = ""
|
| 25 |
|
| 26 |
+
for message in client.chat_completion(
|
| 27 |
+
messages,
|
| 28 |
+
max_tokens=max_tokens,
|
| 29 |
+
stream=True,
|
| 30 |
+
temperature=temperature,
|
| 31 |
+
top_p=top_p,
|
| 32 |
+
):
|
| 33 |
+
token = message.choices[0].delta.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
response += token
|
| 36 |
+
yield response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Define custom CSS
|
| 39 |
custom_css = """
|
|
|
|
| 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()
|