Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# 2) The actual app
|
| 2 |
+
import os
|
| 3 |
+
from getpass import getpass
|
| 4 |
+
from openai import OpenAI
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# βββ Configure your OpenRouter key βββ
|
| 8 |
+
if "OPENROUTER_API_KEY" not in os.environ or not os.environ["OPENROUTER_API_KEY"]:
|
| 9 |
+
os.environ["OPENROUTER_API_KEY"] = getpass("π Enter your OpenRouter API key: ")
|
| 10 |
+
|
| 11 |
+
client = OpenAI(
|
| 12 |
+
base_url="https://openrouter.ai/api/v1",
|
| 13 |
+
api_key=os.environ["OPENROUTER_API_KEY"],
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def openrouter_chat(user_message, history):
|
| 17 |
+
"""Send user_message to mistralai/devstral-small:free and append to history."""
|
| 18 |
+
# build the messages list
|
| 19 |
+
msgs = [{"role": "user", "content": user_message}]
|
| 20 |
+
# call the model
|
| 21 |
+
resp = client.chat.completions.create(
|
| 22 |
+
model="mistralai/devstral-small:free",
|
| 23 |
+
messages=msgs,
|
| 24 |
+
# you can tweak max_tokens, temperature, etc. here
|
| 25 |
+
)
|
| 26 |
+
bot_reply = resp.choices[0].message.content
|
| 27 |
+
history = history or []
|
| 28 |
+
history.append((user_message, bot_reply))
|
| 29 |
+
return history, ""
|
| 30 |
+
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("## π¦π Gradio + OpenRouter Chat with Devstral-Small")
|
| 33 |
+
chatbot = gr.Chatbot(label="Chat")
|
| 34 |
+
msg_in = gr.Textbox(placeholder="Type your question hereβ¦", label="You")
|
| 35 |
+
msg_in.submit(openrouter_chat, inputs=[msg_in, chatbot], outputs=[chatbot, msg_in])
|
| 36 |
+
demo.launch()
|