Upload 2 files
Browse files- app.py +84 -64
- requirements.txt +4 -1
app.py
CHANGED
|
@@ -1,64 +1,84 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 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 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
import pyttsx3
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
TOGETHER_API_KEY = "92269fefcc8df100e2d395daab78ba62c0f3b64dbd477227020dce924e7821fe"
|
| 9 |
+
|
| 10 |
+
# Text-to-speech setup
|
| 11 |
+
tts_engine = pyttsx3.init()
|
| 12 |
+
|
| 13 |
+
def speak(text):
|
| 14 |
+
tts_engine.say(text)
|
| 15 |
+
tts_engine.runAndWait()
|
| 16 |
+
|
| 17 |
+
# 🎤 Speech-to-text function
|
| 18 |
+
def transcribe_audio(audio):
|
| 19 |
+
recognizer = sr.Recognizer()
|
| 20 |
+
with sr.AudioFile(audio) as source:
|
| 21 |
+
audio_data = recognizer.record(source)
|
| 22 |
+
try:
|
| 23 |
+
return recognizer.recognize_google(audio_data)
|
| 24 |
+
except sr.UnknownValueError:
|
| 25 |
+
return "Sorry, I could not understand your voice."
|
| 26 |
+
except sr.RequestError:
|
| 27 |
+
return "Could not request results from speech recognition service."
|
| 28 |
+
|
| 29 |
+
# 🤖 Call Together API
|
| 30 |
+
def call_together_api(message, history):
|
| 31 |
+
messages = [{"role": "system", "content": "You are Sambit AI, a helpful assistant."}]
|
| 32 |
+
for user_msg, ai_msg in history:
|
| 33 |
+
messages.append({"role": "user", "content": user_msg})
|
| 34 |
+
messages.append({"role": "assistant", "content": ai_msg})
|
| 35 |
+
messages.append({"role": "user", "content": message})
|
| 36 |
+
|
| 37 |
+
response = requests.post(
|
| 38 |
+
"https://api.together.xyz/v1/chat/completions",
|
| 39 |
+
headers={
|
| 40 |
+
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 41 |
+
"Content-Type": "application/json"
|
| 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()
|
requirements.txt
CHANGED
|
@@ -1 +1,4 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
speechrecognition
|
| 4 |
+
pyttsx3
|