Update
Browse files- app.py +21 -27
- requirements.txt +2 -2
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
-
import gradio as gr
|
| 4 |
from gtts import gTTS
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
TOGETHER_MODEL = "meta-llama/Llama-3-70B-Instruct"
|
| 10 |
|
| 11 |
def speak_text(text):
|
| 12 |
tts = gTTS(text)
|
|
@@ -14,43 +14,37 @@ def speak_text(text):
|
|
| 14 |
tts.save(fp.name)
|
| 15 |
return fp.name
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
headers = {
|
| 20 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 21 |
"Content-Type": "application/json"
|
| 22 |
}
|
| 23 |
-
|
| 24 |
-
"model":
|
| 25 |
"prompt": prompt,
|
| 26 |
"max_tokens": 512,
|
| 27 |
"temperature": 0.7,
|
| 28 |
"top_p": 0.95,
|
| 29 |
-
"repetition_penalty": 1.
|
| 30 |
}
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return response.json()['choices'][0]['text'].strip()
|
| 35 |
-
else:
|
| 36 |
-
return "⚠️ API error: " + response.text
|
| 37 |
-
|
| 38 |
-
def respond(message, history):
|
| 39 |
-
history = history or []
|
| 40 |
-
prompt = ""
|
| 41 |
-
for user_msg, bot_msg in history:
|
| 42 |
-
prompt += f"<|user|> {user_msg}\n<|assistant|> {bot_msg}\n"
|
| 43 |
-
prompt += f"<|user|> {message}\n<|assistant|>"
|
| 44 |
-
|
| 45 |
-
response = together_generate(prompt)
|
| 46 |
-
history.append((message, response))
|
| 47 |
-
audio_path = speak_text(response)
|
| 48 |
return history, history, audio_path
|
| 49 |
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
gr.Markdown("# Sambit AI 🤖")
|
| 52 |
chatbot = gr.Chatbot([], label="Chat with AI", type="messages")
|
| 53 |
-
msg = gr.Textbox(label="Type your message here"
|
| 54 |
audio_output = gr.Audio(label="AI Voice", interactive=False)
|
| 55 |
clear = gr.Button("Clear Chat")
|
| 56 |
state = gr.State([])
|
|
@@ -58,4 +52,4 @@ with gr.Blocks() as demo:
|
|
| 58 |
msg.submit(respond, [msg, state], [chatbot, state, audio_output])
|
| 59 |
clear.click(lambda: ([], [], None), None, [chatbot, state, audio_output])
|
| 60 |
|
| 61 |
-
demo.
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
import os
|
| 4 |
import requests
|
|
|
|
| 5 |
from gtts import gTTS
|
| 6 |
import tempfile
|
| 7 |
|
| 8 |
+
TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
|
| 9 |
+
MODEL = "meta-llama/Llama-3-70B-Instruct"
|
|
|
|
| 10 |
|
| 11 |
def speak_text(text):
|
| 12 |
tts = gTTS(text)
|
|
|
|
| 14 |
tts.save(fp.name)
|
| 15 |
return fp.name
|
| 16 |
|
| 17 |
+
def respond(message, history):
|
| 18 |
+
history = history or []
|
| 19 |
+
prompt = ""
|
| 20 |
+
for user_msg, bot_msg in history:
|
| 21 |
+
prompt += f"<|user|> {user_msg}\n<|assistant|> {bot_msg}\n"
|
| 22 |
+
prompt += f"<|user|> {message}\n<|assistant|>"
|
| 23 |
+
|
| 24 |
headers = {
|
| 25 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 26 |
"Content-Type": "application/json"
|
| 27 |
}
|
| 28 |
+
payload = {
|
| 29 |
+
"model": MODEL,
|
| 30 |
"prompt": prompt,
|
| 31 |
"max_tokens": 512,
|
| 32 |
"temperature": 0.7,
|
| 33 |
"top_p": 0.95,
|
| 34 |
+
"repetition_penalty": 1.1
|
| 35 |
}
|
| 36 |
+
response = requests.post("https://api.together.xyz/inference", headers=headers, json=payload)
|
| 37 |
+
response_json = response.json()
|
| 38 |
+
output_text = response_json.get("output", "").strip().split("<|assistant|>")[-1].strip()
|
| 39 |
|
| 40 |
+
history.append((message, output_text))
|
| 41 |
+
audio_path = speak_text(output_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return history, history, audio_path
|
| 43 |
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
gr.Markdown("# Sambit AI 🤖")
|
| 46 |
chatbot = gr.Chatbot([], label="Chat with AI", type="messages")
|
| 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")
|
| 50 |
state = gr.State([])
|
|
|
|
| 52 |
msg.submit(respond, [msg, state], [chatbot, state, audio_output])
|
| 53 |
clear.click(lambda: ([], [], None), None, [chatbot, state, audio_output])
|
| 54 |
|
| 55 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
|
| 2 |
-
gradio
|
| 3 |
-
|
| 4 |
gTTS
|
|
|
|
| 1 |
|
| 2 |
+
gradio==5.30.0
|
| 3 |
+
requests
|
| 4 |
gTTS
|