|
|
|
import os |
|
from getpass import getpass |
|
from openai import OpenAI |
|
import gradio as gr |
|
|
|
|
|
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"], |
|
) |
|
|
|
def openrouter_chat(user_message, history): |
|
"""Send user_message to mistralai/devstral-small:free and append to history.""" |
|
|
|
msgs = [{"role": "user", "content": user_message}] |
|
|
|
resp = client.chat.completions.create( |
|
model="mistralai/devstral-small:free", |
|
messages=msgs, |
|
|
|
) |
|
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() |
|
|