Upload sambit_ai_app.py
Browse files- sambit_ai_app.py +82 -0
sambit_ai_app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
API_KEY = os.environ.get("TOGETHER_API_KEY")
|
| 7 |
+
MODEL_NAME = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 8 |
+
|
| 9 |
+
def ask_ai(message, history):
|
| 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 |
+
|
| 19 |
+
messages = [{"role": "system", "content": "You are Sambit AI, a helpful assistant."}]
|
| 20 |
+
for user, bot in history:
|
| 21 |
+
messages.append({"role": "user", "content": user})
|
| 22 |
+
messages.append({"role": "assistant", "content": bot})
|
| 23 |
+
messages.append({"role": "user", "content": message})
|
| 24 |
+
|
| 25 |
+
data = {
|
| 26 |
+
"model": MODEL_NAME,
|
| 27 |
+
"messages": messages
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
response = requests.post(url, headers=headers, json=data)
|
| 32 |
+
response.raise_for_status()
|
| 33 |
+
result = response.json()
|
| 34 |
+
reply = result['choices'][0]['message']['content']
|
| 35 |
+
return reply
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"❌ Error: {str(e)}"
|
| 38 |
+
|
| 39 |
+
with gr.Blocks(
|
| 40 |
+
theme=gr.themes.Base(),
|
| 41 |
+
css="""
|
| 42 |
+
.gradio-container {
|
| 43 |
+
max-width: 100% !important;
|
| 44 |
+
padding: 1rem;
|
| 45 |
+
font-family: 'Poppins', sans-serif;
|
| 46 |
+
}
|
| 47 |
+
#header-title {
|
| 48 |
+
text-align: center;
|
| 49 |
+
font-weight: 700;
|
| 50 |
+
font-size: 2rem;
|
| 51 |
+
margin-bottom: 0.25rem;
|
| 52 |
+
}
|
| 53 |
+
#subtext {
|
| 54 |
+
text-align: center;
|
| 55 |
+
font-weight: 300;
|
| 56 |
+
font-size: 1rem;
|
| 57 |
+
margin-bottom: 1rem;
|
| 58 |
+
}
|
| 59 |
+
"""
|
| 60 |
+
) as demo:
|
| 61 |
+
with gr.Column():
|
| 62 |
+
gr.Markdown("Sambit AI", elem_id="header-title")
|
| 63 |
+
gr.Markdown("powered by acrilc", elem_id="subtext")
|
| 64 |
+
|
| 65 |
+
chatbot = gr.Chatbot(label="Chat with Sambit AI", height=400)
|
| 66 |
+
with gr.Row():
|
| 67 |
+
msg = gr.Textbox(placeholder="Ask anything...", scale=4)
|
| 68 |
+
send = gr.Button("Send", scale=1)
|
| 69 |
+
|
| 70 |
+
clear = gr.Button("🧹 Clear chat")
|
| 71 |
+
state = gr.State([])
|
| 72 |
+
|
| 73 |
+
def respond(message, history):
|
| 74 |
+
reply = ask_ai(message, history)
|
| 75 |
+
history.append((message, reply))
|
| 76 |
+
return history, ""
|
| 77 |
+
|
| 78 |
+
send.click(respond, [msg, state], [chatbot, msg])
|
| 79 |
+
msg.submit(respond, [msg, state], [chatbot, msg])
|
| 80 |
+
clear.click(lambda: ([], ""), None, [chatbot, msg])
|
| 81 |
+
|
| 82 |
+
demo.launch()
|