Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
from gtts import gTTS
|
| 4 |
import tempfile
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def speak_text(text):
|
| 9 |
tts = gTTS(text)
|
|
@@ -11,6 +14,27 @@ def speak_text(text):
|
|
| 11 |
tts.save(fp.name)
|
| 12 |
return fp.name
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def respond(message, history):
|
| 15 |
history = history or []
|
| 16 |
prompt = ""
|
|
@@ -18,16 +42,7 @@ def respond(message, history):
|
|
| 18 |
prompt += f"<|user|> {user_msg}\n<|assistant|> {bot_msg}\n"
|
| 19 |
prompt += f"<|user|> {message}\n<|assistant|>"
|
| 20 |
|
| 21 |
-
response =
|
| 22 |
-
prompt,
|
| 23 |
-
max_new_tokens=512,
|
| 24 |
-
temperature=0.7,
|
| 25 |
-
top_p=0.95,
|
| 26 |
-
repetition_penalty=1.0,
|
| 27 |
-
do_sample=True
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
response = response.strip().split("<|assistant|>")[-1].strip()
|
| 31 |
history.append((message, response))
|
| 32 |
audio_path = speak_text(response)
|
| 33 |
return history, history, audio_path
|
|
@@ -35,16 +50,12 @@ def respond(message, history):
|
|
| 35 |
with gr.Blocks() as demo:
|
| 36 |
gr.Markdown("# Sambit AI 🤖")
|
| 37 |
chatbot = gr.Chatbot([], label="Chat with AI", type="messages")
|
| 38 |
-
msg = gr.Textbox(label="Type your message here", placeholder="Ask me anything..."
|
| 39 |
audio_output = gr.Audio(label="AI Voice", interactive=False)
|
| 40 |
-
|
| 41 |
-
clear = gr.Button("🧹 Clear Chat")
|
| 42 |
-
|
| 43 |
state = gr.State([])
|
| 44 |
|
| 45 |
msg.submit(respond, [msg, state], [chatbot, state, audio_output])
|
| 46 |
clear.click(lambda: ([], [], None), None, [chatbot, state, audio_output])
|
| 47 |
|
| 48 |
-
|
| 49 |
-
demo.launch()
|
| 50 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
from gtts import gTTS
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
+
|
| 8 |
+
TOGETHER_API_KEY = os.environ.get("TOGETHER_API_KEY")
|
| 9 |
+
TOGETHER_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 together_generate(prompt):
|
| 18 |
+
url = "https://api.together.xyz/v1/completions"
|
| 19 |
+
headers = {
|
| 20 |
+
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
| 21 |
+
"Content-Type": "application/json"
|
| 22 |
+
}
|
| 23 |
+
data = {
|
| 24 |
+
"model": TOGETHER_MODEL,
|
| 25 |
+
"prompt": prompt,
|
| 26 |
+
"max_tokens": 512,
|
| 27 |
+
"temperature": 0.7,
|
| 28 |
+
"top_p": 0.95,
|
| 29 |
+
"repetition_penalty": 1.0
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
response = requests.post(url, headers=headers, json=data)
|
| 33 |
+
if response.status_code == 200:
|
| 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 = ""
|
|
|
|
| 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
|
|
|
|
| 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", placeholder="Ask me anything...")
|
| 54 |
audio_output = gr.Audio(label="AI Voice", interactive=False)
|
| 55 |
+
clear = gr.Button("Clear Chat")
|
|
|
|
|
|
|
| 56 |
state = gr.State([])
|
| 57 |
|
| 58 |
msg.submit(respond, [msg, state], [chatbot, state, audio_output])
|
| 59 |
clear.click(lambda: ([], [], None), None, [chatbot, state, audio_output])
|
| 60 |
|
| 61 |
+
demo.queue()
|
|
|
|
|
|