Update app.py
Browse files
app.py
CHANGED
|
@@ -1,55 +1,40 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from together import Together
|
| 3 |
-
from gtts import gTTS
|
| 4 |
-
import tempfile
|
| 5 |
import os
|
| 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 |
-
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")
|
| 50 |
-
state = gr.State([])
|
| 51 |
-
|
| 52 |
-
msg.submit(respond, [msg, state], [chatbot, state, audio_output])
|
| 53 |
-
clear.click(lambda: ([], [], None), None, [chatbot, state, audio_output])
|
| 54 |
-
|
| 55 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
+
|
| 5 |
+
# β
Load API key from Hugging Face Secrets (set in Settings β Secrets)
|
| 6 |
+
API_KEY = os.getenv("TOGETHER_API_KEY")
|
| 7 |
+
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 8 |
+
|
| 9 |
+
def ask_ai(message):
|
| 10 |
+
if not API_KEY:
|
| 11 |
+
return "β API key not found. Please set it in Settings β Secrets."
|
| 12 |
+
|
| 13 |
+
url = "https://api.together.xyz/v1/chat/completions"
|
| 14 |
+
headers = {
|
| 15 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 16 |
+
"Content-Type": "application/json"
|
| 17 |
+
}
|
| 18 |
+
data = {
|
| 19 |
+
"model": MODEL_NAME,
|
| 20 |
+
"messages": [
|
| 21 |
+
{"role": "system", "content": "You are Sambit AI, a helpful assistant."},
|
| 22 |
+
{"role": "user", "content": message}
|
| 23 |
+
]
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
response = requests.post(url, headers=headers, json=data)
|
| 28 |
+
response.raise_for_status()
|
| 29 |
+
result = response.json()
|
| 30 |
+
reply = result['choices'][0]['message']['content']
|
| 31 |
+
return reply
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"β Error: {str(e)}"
|
| 34 |
+
|
| 35 |
+
# β
Gradio UI
|
| 36 |
+
chat_interface = gr.ChatInterface(fn=ask_ai, title="Sambit AI π€ β Powered by Together & LLaMA 3")
|
| 37 |
+
|
| 38 |
+
# β
Launch the app
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
chat_interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|