Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,60 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
|
| 4 |
-
MODEL = "mistralai/mixtral-8x7b-instruct" # or let users choose
|
| 5 |
-
API_URL = "https://openrouter.ai/api/v1/chat/completions"
|
| 6 |
-
|
| 7 |
-
def chat_with_openrouter(api_key, message, history):
|
| 8 |
-
if not api_key:
|
| 9 |
-
return "Please enter your OpenRouter API key."
|
| 10 |
-
headers = {
|
| 11 |
-
"Authorization": f"Bearer {api_key}",
|
| 12 |
-
"Content-Type": "application/json"
|
| 13 |
-
}
|
| 14 |
-
messages = []
|
| 15 |
-
for user_msg, bot_msg in history:
|
| 16 |
-
messages.append({"role": "user", "content": user_msg})
|
| 17 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 18 |
-
messages.append({"role": "user", "content": message})
|
| 19 |
-
data = {
|
| 20 |
-
"model": MODEL,
|
| 21 |
-
"messages": messages
|
| 22 |
-
}
|
| 23 |
-
response = requests.post(API_URL, headers=headers, json=data)
|
| 24 |
-
if response.status_code == 200:
|
| 25 |
-
reply = response.json()["choices"][0]["message"]["content"]
|
| 26 |
-
return reply
|
| 27 |
-
else:
|
| 28 |
-
return f"Error: {response.status_code} - {response.text}"
|
| 29 |
-
|
| 30 |
-
def respond(api_key, message, chat_history):
|
| 31 |
-
bot_message = chat_with_openrouter(api_key, message, chat_history)
|
| 32 |
-
chat_history.append((message, bot_message))
|
| 33 |
-
return "", chat_history
|
| 34 |
-
|
| 35 |
-
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 36 |
-
gr.Markdown(
|
| 37 |
-
"""
|
| 38 |
-
# 🤖 OpenRouter AI Chatbot
|
| 39 |
-
Enter your [OpenRouter API key](https://openrouter.ai/keys) to start chatting!
|
| 40 |
-
"""
|
| 41 |
-
)
|
| 42 |
-
api_key_box = gr.Textbox(
|
| 43 |
-
placeholder="Paste your OpenRouter API key here",
|
| 44 |
-
label="OpenRouter API Key",
|
| 45 |
-
type="password"
|
| 46 |
-
)
|
| 47 |
-
chatbot = gr.Chatbot([], label="OpenRouter Chatbot", height=500)
|
| 48 |
-
with gr.Row():
|
| 49 |
-
msg = gr.Textbox(
|
| 50 |
-
placeholder="Type your message and press Enter...",
|
| 51 |
-
show_label=False,
|
| 52 |
-
lines=1,
|
| 53 |
-
max_lines=3,
|
| 54 |
-
autofocus=True
|
| 55 |
-
)
|
| 56 |
-
clear = gr.Button("🧹 Clear", variant="secondary")
|
| 57 |
-
msg.submit(respond, [api_key_box, msg, chatbot], [msg, chatbot])
|
| 58 |
-
clear.click(lambda: [], None, chatbot, queue=False)
|
| 59 |
-
|
| 60 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|