Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,23 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from gtts import gTTS
|
| 4 |
import tempfile
|
| 5 |
-
|
| 6 |
|
| 7 |
-
#
|
| 8 |
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
|
| 9 |
-
client = Together(api_key=os.getenv("TOGETHER_API_KEY"))
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def speak_text(text):
|
| 12 |
tts = gTTS(text)
|
| 13 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 14 |
tts.save(fp.name)
|
| 15 |
return fp.name
|
| 16 |
|
|
|
|
| 17 |
def respond(message, history):
|
| 18 |
history = history or []
|
| 19 |
prompt = ""
|
|
@@ -21,6 +25,7 @@ def respond(message, history):
|
|
| 21 |
prompt += f"<|user|> {user_msg}\n<|assistant|> {bot_msg}\n"
|
| 22 |
prompt += f"<|user|> {message}\n<|assistant|>"
|
| 23 |
|
|
|
|
| 24 |
response = client.chat.completions.create(
|
| 25 |
model="meta-llama/Llama-3-8B-Instruct",
|
| 26 |
messages=[{"role": "user", "content": prompt}],
|
|
@@ -29,14 +34,16 @@ def respond(message, history):
|
|
| 29 |
top_p=0.95
|
| 30 |
)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
history.append((message,
|
| 34 |
-
audio_path = speak_text(
|
| 35 |
return history, history, audio_path
|
| 36 |
|
|
|
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
-
gr.Markdown("
|
| 39 |
-
|
|
|
|
| 40 |
msg = gr.Textbox(label="Type your message here")
|
| 41 |
audio_output = gr.Audio(label="AI Voice", interactive=False)
|
| 42 |
clear = gr.Button("Clear Chat")
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from together import Together
|
| 3 |
from gtts import gTTS
|
| 4 |
import tempfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Get your Together API key from Hugging Face secret
|
| 8 |
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
|
|
|
|
| 9 |
|
| 10 |
+
# Initialize Together client
|
| 11 |
+
client = Together(api_key=TOGETHER_API_KEY)
|
| 12 |
+
|
| 13 |
+
# Text-to-speech with gTTS
|
| 14 |
def speak_text(text):
|
| 15 |
tts = gTTS(text)
|
| 16 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as fp:
|
| 17 |
tts.save(fp.name)
|
| 18 |
return fp.name
|
| 19 |
|
| 20 |
+
# Main chat function
|
| 21 |
def respond(message, history):
|
| 22 |
history = history or []
|
| 23 |
prompt = ""
|
|
|
|
| 25 |
prompt += f"<|user|> {user_msg}\n<|assistant|> {bot_msg}\n"
|
| 26 |
prompt += f"<|user|> {message}\n<|assistant|>"
|
| 27 |
|
| 28 |
+
# Call Together API
|
| 29 |
response = client.chat.completions.create(
|
| 30 |
model="meta-llama/Llama-3-8B-Instruct",
|
| 31 |
messages=[{"role": "user", "content": prompt}],
|
|
|
|
| 34 |
top_p=0.95
|
| 35 |
)
|
| 36 |
|
| 37 |
+
reply = response.choices[0].message.content.strip()
|
| 38 |
+
history.append((message, reply))
|
| 39 |
+
audio_path = speak_text(reply)
|
| 40 |
return history, history, audio_path
|
| 41 |
|
| 42 |
+
# Gradio UI
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
+
gr.Markdown("## Sambit AI 🤖 — Powered by Together & LLaMA 3")
|
| 45 |
+
|
| 46 |
+
chatbot = gr.Chatbot(label="Chat with AI")
|
| 47 |
msg = gr.Textbox(label="Type your message here")
|
| 48 |
audio_output = gr.Audio(label="AI Voice", interactive=False)
|
| 49 |
clear = gr.Button("Clear Chat")
|