Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
from gtts import gTTS
|
5 |
+
import speech_recognition as sr
|
6 |
+
import pyaudio
|
7 |
+
import io
|
8 |
+
from tempfile import NamedTemporaryFile
|
9 |
+
|
10 |
+
api = os.getenv("HF_API_TOKEN")
|
11 |
+
client = InferenceClient("meta-llama/Meta-Llama-3.1-70B-Instruct", token=f"{api}")
|
12 |
+
|
13 |
+
def respond(
|
14 |
+
message,
|
15 |
+
history: list[tuple[str, str]],
|
16 |
+
system_message,
|
17 |
+
max_tokens,
|
18 |
+
temperature,
|
19 |
+
top_p,
|
20 |
+
):
|
21 |
+
messages = [{"role": "system", "content": system_message}]
|
22 |
+
|
23 |
+
for val in history:
|
24 |
+
if val[0]:
|
25 |
+
messages.append({"role": "user", "content": val[0]})
|
26 |
+
if val[1]:
|
27 |
+
messages.append({"role": "assistant", "content": val[1]})
|
28 |
+
|
29 |
+
messages.append({"role": "user", "content": message})
|
30 |
+
|
31 |
+
response = ""
|
32 |
+
|
33 |
+
for message in client.chat_completion(
|
34 |
+
messages,
|
35 |
+
max_tokens=max_tokens,
|
36 |
+
stream=True,
|
37 |
+
temperature=temperature,
|
38 |
+
top_p=top_p,
|
39 |
+
):
|
40 |
+
token = message.choices[0].delta.content
|
41 |
+
|
42 |
+
response += token
|
43 |
+
yield response
|
44 |
+
|
45 |
+
def text_to_speech(text):
|
46 |
+
tts = gTTS(text=text, lang='en')
|
47 |
+
with NamedTemporaryFile(delete=True) as tmpfile:
|
48 |
+
tts.save(tmpfile.name)
|
49 |
+
with open(tmpfile.name, "rb") as f:
|
50 |
+
return f.read()
|
51 |
+
|
52 |
+
def speech_to_text(audio):
|
53 |
+
recognizer = sr.Recognizer()
|
54 |
+
with sr.AudioFile(io.BytesIO(audio)) as source:
|
55 |
+
audio_data = recognizer.record(source)
|
56 |
+
return recognizer.recognize_google(audio_data)
|
57 |
+
|
58 |
+
def process_audio(audio, system_message, max_tokens, temperature, top_p):
|
59 |
+
text = speech_to_text(audio)
|
60 |
+
response_gen = respond(
|
61 |
+
message=text,
|
62 |
+
history=[],
|
63 |
+
system_message=system_message,
|
64 |
+
max_tokens=max_tokens,
|
65 |
+
temperature=temperature,
|
66 |
+
top_p=top_p,
|
67 |
+
)
|
68 |
+
response_text = next(response_gen)
|
69 |
+
audio_response = text_to_speech(response_text)
|
70 |
+
return audio_response
|
71 |
+
|
72 |
+
demo = gr.Interface(
|
73 |
+
fn=process_audio,
|
74 |
+
inputs=[
|
75 |
+
gr.Audio(source="microphone", type="bytes"),
|
76 |
+
gr.Textbox(value="You are a friendly Chatbot.", 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 |
+
outputs=gr.Audio(type="bytes"),
|
88 |
+
)
|
89 |
+
|
90 |
+
if __name__ == "__main__":
|
91 |
+
demo.launch()
|