Update app.py
Browse files
app.py
CHANGED
@@ -3,34 +3,61 @@ import os
|
|
3 |
from getpass import getpass
|
4 |
from openai import OpenAI
|
5 |
import gradio as gr
|
|
|
6 |
|
7 |
# βββ Configure your OpenRouter key βββ
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
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 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
from getpass import getpass
|
4 |
from openai import OpenAI
|
5 |
import gradio as gr
|
6 |
+
from google.colab import userdata
|
7 |
|
8 |
# βββ Configure your OpenRouter key βββ
|
9 |
+
# Assuming OPENROUTER_API_KEY is already set in the environment or Colab secrets
|
10 |
+
|
11 |
+
# Retrieve the API key from Colab secrets
|
12 |
+
api_key=OPENROUTER_API_KEY
|
13 |
+
|
14 |
+
# Check if the API key was retrieved
|
15 |
+
if not OPENROUTER_API_KEY:
|
16 |
+
print("Error: OPENROUTER_API_KEY not found in Colab secrets.")
|
17 |
+
print("Please add your API key to Colab secrets by clicking on the key icon in the left panel and naming it OPENROUTER_API_KEY.")
|
18 |
+
else:
|
19 |
+
client = OpenAI(
|
20 |
+
base_url="https://openrouter.ai/api/v1",
|
21 |
+
api_key=OPENROUTER_API_KEY,
|
|
|
|
|
|
|
|
|
22 |
)
|
23 |
+
|
24 |
+
def openrouter_chat(user_message, history):
|
25 |
+
"""Send user_message and history to mistralai/devstral-small:free and append to history."""
|
26 |
+
history = history or []
|
27 |
+
|
28 |
+
# Build the messages list from the history and the current user message
|
29 |
+
# Gradio history is a list of tuples [(user_msg, bot_msg), ...]
|
30 |
+
# We need to convert it to the OpenAI API format: list of dicts [{"role": "user", "content": ...}, {"role": "assistant", "content": ...}, ...]
|
31 |
+
messages_for_api = []
|
32 |
+
for human_message, ai_message in history:
|
33 |
+
messages_for_api.append({"role": "user", "content": human_message})
|
34 |
+
if ai_message is not None: # Only add assistant message if it exists
|
35 |
+
messages_for_api.append({"role": "assistant", "content": ai_message})
|
36 |
+
|
37 |
+
# Add the current user message
|
38 |
+
messages_for_api.append({"role": "user", "content": user_message})
|
39 |
+
|
40 |
+
try:
|
41 |
+
# Call the model with the full conversation history
|
42 |
+
resp = client.chat.completions.create(
|
43 |
+
model="mistralai/devstral-small:free",
|
44 |
+
messages=messages_for_api,
|
45 |
+
# you can tweak max_tokens, temperature, etc. here
|
46 |
+
)
|
47 |
+
bot_reply = resp.choices[0].message.content
|
48 |
+
# Append the user message and bot reply to the history for Gradio display
|
49 |
+
history.append((user_message, bot_reply))
|
50 |
+
except Exception as e:
|
51 |
+
# Handle potential errors and append an error message to the history
|
52 |
+
history.append((user_message, f"Error: {e}"))
|
53 |
+
|
54 |
+
|
55 |
+
return history, ""
|
56 |
+
|
57 |
+
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("## π¦π Gradio + OpenRouter Chat with Devstral-Small (with History)")
|
59 |
+
chatbot = gr.Chatbot(label="Chat")
|
60 |
+
msg_in = gr.Textbox(placeholder="Type your question hereβ¦", label="You")
|
61 |
+
msg_in.submit(openrouter_chat, inputs=[msg_in, chatbot], outputs=[chatbot, msg_in])
|
62 |
+
|
63 |
+
demo.launch()
|