wifix199 commited on
Commit
df55a30
Β·
verified Β·
1 Parent(s): 953ec23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -28
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
- 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
- #mistralai/Devstral-Small-2505
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()