Updated files to fix runtime error and add gTTS
Browse files- app.py +42 -84
- requirements.txt +4 -4
app.py
CHANGED
|
@@ -1,84 +1,42 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
json={
|
| 44 |
-
"model": "meta-llama/Llama-3-70b-instruct",
|
| 45 |
-
"messages": messages,
|
| 46 |
-
"temperature": 0.7,
|
| 47 |
-
}
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
if response.status_code == 200:
|
| 51 |
-
reply = response.json()["choices"][0]["message"]["content"]
|
| 52 |
-
speak(reply)
|
| 53 |
-
return reply
|
| 54 |
-
elif response.status_code == 429:
|
| 55 |
-
return "Rate limit reached. Please wait a bit."
|
| 56 |
-
else:
|
| 57 |
-
return f"Error: {response.json()['error']['message']}"
|
| 58 |
-
|
| 59 |
-
# 🎤🎧 UI function (text + voice input)
|
| 60 |
-
def chatbot_interface(message, audio, history=[]):
|
| 61 |
-
if audio is not None:
|
| 62 |
-
message = transcribe_audio(audio)
|
| 63 |
-
if message.strip() == "":
|
| 64 |
-
return "", history
|
| 65 |
-
reply = call_together_api(message, history)
|
| 66 |
-
history.append((message, reply))
|
| 67 |
-
return "", history
|
| 68 |
-
|
| 69 |
-
# 🎨 Dark theme UI
|
| 70 |
-
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
| 71 |
-
gr.Markdown("# 🤖 Sambit AI\nAsk anything via text or voice")
|
| 72 |
-
|
| 73 |
-
chatbot = gr.Chatbot(type="messages")
|
| 74 |
-
msg = gr.Textbox(placeholder="Type your message here...", label="Text Input")
|
| 75 |
-
audio_input = gr.Audio(sources="microphone", type="filepath", label="Or speak here")
|
| 76 |
-
|
| 77 |
-
submit_btn = gr.Button("Send")
|
| 78 |
-
|
| 79 |
-
state = gr.State([])
|
| 80 |
-
|
| 81 |
-
submit_btn.click(chatbot_interface, inputs=[msg, audio_input, state], outputs=[msg, state])
|
| 82 |
-
msg.submit(chatbot_interface, inputs=[msg, audio_input, state], outputs=[msg, state])
|
| 83 |
-
|
| 84 |
-
demo.launch()
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
+
|
| 9 |
+
def speak_text(text):
|
| 10 |
+
tts = gTTS(text)
|
| 11 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 12 |
+
tts.save(fp.name)
|
| 13 |
+
return fp.name
|
| 14 |
+
|
| 15 |
+
def respond(message, history):
|
| 16 |
+
history = history or []
|
| 17 |
+
prompt = ""
|
| 18 |
+
for user_msg, bot_msg in history:
|
| 19 |
+
prompt += f"<|user|> {user_msg}\n<|assistant|> {bot_msg}\n"
|
| 20 |
+
prompt += f"<|user|> {message}\n<|assistant|>"
|
| 21 |
+
|
| 22 |
+
response = client.text_generation(prompt, max_new_tokens=512, temperature=0.7, top_p=0.95, repetition_penalty=1.0, do_sample=True)
|
| 23 |
+
response = response.strip().split("<|assistant|>")[-1].strip()
|
| 24 |
+
|
| 25 |
+
history.append((message, response))
|
| 26 |
+
audio_path = speak_text(response)
|
| 27 |
+
return history, history, audio_path
|
| 28 |
+
|
| 29 |
+
with gr.Blocks() as demo:
|
| 30 |
+
gr.Markdown("# Sambit AI 🤖")
|
| 31 |
+
chatbot = gr.Chatbot([], label="Chat with AI", type="messages")
|
| 32 |
+
msg = gr.Textbox(label="Type your message here")
|
| 33 |
+
audio_output = gr.Audio(label="AI Voice", interactive=False)
|
| 34 |
+
|
| 35 |
+
clear = gr.Button("Clear Chat")
|
| 36 |
+
|
| 37 |
+
state = gr.State([])
|
| 38 |
+
|
| 39 |
+
msg.submit(respond, [msg, state], [chatbot, state, audio_output])
|
| 40 |
+
clear.click(lambda: ([], [], None), None, [chatbot, state, audio_output])
|
| 41 |
+
|
| 42 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 1 |
+
|
| 2 |
+
gradio
|
| 3 |
+
huggingface_hub
|
| 4 |
+
gTTS
|