Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,39 +2,31 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from transformers import pipeline
|
| 4 |
|
|
|
|
| 5 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 6 |
|
| 7 |
-
|
| 8 |
stt_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
| 9 |
|
| 10 |
-
|
| 11 |
tts_pipeline = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
audio
|
| 16 |
-
message, # Text input
|
| 17 |
-
history: list[tuple[str, str]],
|
| 18 |
-
system_message,
|
| 19 |
-
max_tokens,
|
| 20 |
-
temperature,
|
| 21 |
-
top_p,
|
| 22 |
-
):
|
| 23 |
-
# Convert speech to text if audio input is given
|
| 24 |
-
if audio:
|
| 25 |
message = stt_pipeline(audio)["text"]
|
| 26 |
|
| 27 |
-
# Prepare
|
| 28 |
messages = [{"role": "system", "content": system_message}]
|
| 29 |
for user_msg, bot_msg in history:
|
| 30 |
if user_msg:
|
| 31 |
messages.append({"role": "user", "content": user_msg})
|
| 32 |
if bot_msg:
|
| 33 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 34 |
-
|
| 35 |
messages.append({"role": "user", "content": message})
|
| 36 |
|
| 37 |
-
#
|
| 38 |
response = ""
|
| 39 |
for msg in client.chat_completion(
|
| 40 |
messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
|
|
@@ -42,27 +34,36 @@ def respond(
|
|
| 42 |
token = msg.choices[0].delta.content
|
| 43 |
response += token
|
| 44 |
|
| 45 |
-
# Convert chatbot
|
| 46 |
speech = tts_pipeline(response)
|
| 47 |
|
| 48 |
return history + [(message, response)], speech["audio"]
|
| 49 |
|
| 50 |
-
# Gradio Interface
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
gr.
|
| 56 |
-
gr.Textbox(
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
gr.
|
| 63 |
-
gr.
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load LLM
|
| 6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 7 |
|
| 8 |
+
# Load Speech-to-Text (STT) model
|
| 9 |
stt_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-base")
|
| 10 |
|
| 11 |
+
# Load Text-to-Speech (TTS) model (using a public model without token requirements)
|
| 12 |
tts_pipeline = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 13 |
|
| 14 |
+
def respond(audio, message, history, system_message, max_tokens, temperature, top_p):
|
| 15 |
+
# Convert speech to text if audio input is provided
|
| 16 |
+
if audio is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
message = stt_pipeline(audio)["text"]
|
| 18 |
|
| 19 |
+
# Prepare conversation history
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
for user_msg, bot_msg in history:
|
| 22 |
if user_msg:
|
| 23 |
messages.append({"role": "user", "content": user_msg})
|
| 24 |
if bot_msg:
|
| 25 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 26 |
+
|
| 27 |
messages.append({"role": "user", "content": message})
|
| 28 |
|
| 29 |
+
# Generate response from LLM
|
| 30 |
response = ""
|
| 31 |
for msg in client.chat_completion(
|
| 32 |
messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p
|
|
|
|
| 34 |
token = msg.choices[0].delta.content
|
| 35 |
response += token
|
| 36 |
|
| 37 |
+
# Convert chatbot response to speech
|
| 38 |
speech = tts_pipeline(response)
|
| 39 |
|
| 40 |
return history + [(message, response)], speech["audio"]
|
| 41 |
|
| 42 |
+
# Gradio Interface using Blocks
|
| 43 |
+
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("# ποΈ Chatbot with Speech & Text")
|
| 45 |
+
|
| 46 |
+
with gr.Row():
|
| 47 |
+
audio_input = gr.Audio(type="filepath", label="π€ Speak (or type below)")
|
| 48 |
+
text_input = gr.Textbox(label="π¬ Or type your message")
|
| 49 |
+
|
| 50 |
+
chatbot = gr.Chatbot(label="Chat History")
|
| 51 |
+
|
| 52 |
+
with gr.Row():
|
| 53 |
+
system_msg = gr.Textbox(value="You are a friendly AI chatbot.", label="System Message")
|
| 54 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens")
|
| 55 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 56 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
| 57 |
+
|
| 58 |
+
audio_output = gr.Audio(label="π AI Response")
|
| 59 |
+
|
| 60 |
+
submit = gr.Button("Send")
|
| 61 |
+
|
| 62 |
+
submit.click(
|
| 63 |
+
respond,
|
| 64 |
+
inputs=[audio_input, text_input, chatbot, system_msg, max_tokens, temperature, top_p],
|
| 65 |
+
outputs=[chatbot, audio_output]
|
| 66 |
+
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|