StralMind / app.py
wifix199's picture
Update app.py
e78d71d verified
raw
history blame
1.35 kB
# 2) The actual app
import os
from getpass import getpass
from openai import OpenAI
import gradio as gr
# β€”β€”β€” Configure your OpenRouter key β€”β€”β€”
if "OPENROUTER_API_KEY" not in os.environ or not os.environ["OPENROUTER_API_KEY"]:
os.environ["OPENROUTER_API_KEY"] = getpass("πŸ”‘ Enter your OpenRouter API key: ")
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key=os.environ["OPENROUTER_API_KEY"],
)
#mistralai/Devstral-Small-2505
def openrouter_chat(user_message, history):
"""Send user_message to mistralai/devstral-small:free and append to history."""
# build the messages list
msgs = [{"role": "user", "content": user_message}]
# call the model
resp = client.chat.completions.create(
model="mistralai/devstral-small:free",
messages=msgs,
# you can tweak max_tokens, temperature, etc. here
)
bot_reply = resp.choices[0].message.content
history = history or []
history.append((user_message, bot_reply))
return history, ""
with gr.Blocks() as demo:
gr.Markdown("## πŸ¦œπŸ”— Gradio + OpenRouter Chat with Devstral-Small")
chatbot = gr.Chatbot(label="Chat")
msg_in = gr.Textbox(placeholder="Type your question here…", label="You")
msg_in.submit(openrouter_chat, inputs=[msg_in, chatbot], outputs=[chatbot, msg_in])
demo.launch()