jsakshi commited on
Commit
d11f457
·
verified ·
1 Parent(s): f75b1ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -31
app.py CHANGED
@@ -2,27 +2,34 @@ import os
2
  import requests
3
  import gradio as gr
4
 
5
- # Set this environment variable before running:
6
- # export OPENROUTER_API_KEY="your_openrouter_api_key_here"
7
- API_KEY = os.getenv("Open_Router")
 
 
 
 
 
 
8
  if not API_KEY:
9
- raise ValueError("OPENROUTER_API_KEY not found. Please set it in your environment variables.")
 
 
 
10
 
11
- # OpenRouter API endpoint
12
  MODEL_URL = "https://openrouter.ai/api/v1/chat/completions"
13
-
14
  HEADERS = {
15
  "Authorization": f"Bearer {API_KEY}",
16
  "Content-Type": "application/json",
17
- # Optional headers:
18
- # "HTTP-Referer": "https://your-app-url.com",
19
- # "X-Title": "LocoBot"
20
  }
21
 
22
- def query_gpt35(messages, max_tokens=500, temperature=0.7):
 
 
 
23
  """
24
- Send a list of messages (OpenAI-style) to OpenRouter and return assistant text.
25
- messages: [{"role":"system"/"user"/"assistant", "content": "..."} , ...]
26
  """
27
  payload = {
28
  "model": "openai/gpt-3.5-turbo",
@@ -32,50 +39,67 @@ def query_gpt35(messages, max_tokens=500, temperature=0.7):
32
  }
33
 
34
  try:
35
- resp = requests.post(MODEL_URL, headers=HEADERS, json=payload, timeout=30)
36
  resp.raise_for_status()
37
  data = resp.json()
38
- # OpenRouter's response format mirrors OpenAI ChatCompletion:
39
- return data["choices"][0]["message"]["content"].strip()
 
 
 
 
 
 
 
 
 
 
 
 
40
  except requests.exceptions.RequestException as e:
41
  return f"Error: Could not connect to the API - {e}"
42
- except (KeyError, IndexError):
43
- return "Error: Unexpected response format from API"
44
 
 
 
 
45
  def respond(user_message, chat_history):
46
  """
47
- Gradio will pass chat_history as a list of (user, bot) tuples.
48
- We convert that to a messages list, append the new user message, query the model,
49
- then append the assistant reply and return updated history.
50
  """
51
- # Start with a system prompt to constrain behavior
 
 
 
52
  messages = [
53
  {"role": "system", "content": "You are LocoBot, a helpful assistant. Answer concisely and politely."}
54
  ]
55
 
56
- # Recreate prior conversation
57
- if chat_history:
58
- for user, bot in chat_history:
59
- messages.append({"role": "user", "content": user})
60
- messages.append({"role": "assistant", "content": bot})
61
 
62
  # Add current user message
63
  messages.append({"role": "user", "content": user_message})
64
 
65
  assistant_reply = query_gpt35(messages)
66
 
67
- # Update history for Gradio (list of tuples)
68
  new_history = chat_history + [(user_message, assistant_reply)]
69
  return new_history, ""
70
 
71
- # Build Gradio UI
 
 
72
  with gr.Blocks() as demo:
73
- gr.Markdown("## LocoBot — Your AI Companion (via OpenRouter GPT-3.5)")
74
  chatbot = gr.Chatbot()
75
- message = gr.Textbox(placeholder="Type your message and press Enter")
76
  send = gr.Button("Send")
77
 
78
- # Hook up interactions
79
  send.click(fn=respond, inputs=[message, chatbot], outputs=[chatbot, message])
80
  message.submit(fn=respond, inputs=[message, chatbot], outputs=[chatbot, message])
81
 
 
2
  import requests
3
  import gradio as gr
4
 
5
+ # -------------------------
6
+ # Configuration (edit here)
7
+ # -------------------------
8
+ # Option A (recommended): set an environment variable named OPENROUTER_API_KEY
9
+ # Option B: paste your key directly (not recommended for production)
10
+ API_KEY = os.getenv("Open_Router") or "" # read from env
11
+ # If you prefer to paste manually, replace the line above with e.g.
12
+ # API_KEY = "sk-or-v1-your-openrouter-key-here"
13
+
14
  if not API_KEY:
15
+ raise ValueError(
16
+ "OPENROUTER_API_KEY not found. "
17
+ "Set it in your shell, or paste the key directly in the script (not recommended)."
18
+ )
19
 
 
20
  MODEL_URL = "https://openrouter.ai/api/v1/chat/completions"
 
21
  HEADERS = {
22
  "Authorization": f"Bearer {API_KEY}",
23
  "Content-Type": "application/json",
 
 
 
24
  }
25
 
26
+ # -------------------------
27
+ # Helper: call OpenRouter
28
+ # -------------------------
29
+ def query_gpt35(messages, max_tokens=500, temperature=0.7, timeout=30):
30
  """
31
+ Send OpenAI-style messages to OpenRouter and return assistant text.
32
+ messages: list of {"role": "...", "content": "..."}
33
  """
34
  payload = {
35
  "model": "openai/gpt-3.5-turbo",
 
39
  }
40
 
41
  try:
42
+ resp = requests.post(MODEL_URL, headers=HEADERS, json=payload, timeout=timeout)
43
  resp.raise_for_status()
44
  data = resp.json()
45
+ # Support both typical "choices[0].message.content" and possible alternative formats
46
+ if "choices" in data and len(data["choices"]) > 0:
47
+ choice = data["choices"][0]
48
+ # Typical OpenAI-style response:
49
+ msg = choice.get("message", {}).get("content")
50
+ if msg:
51
+ return msg.strip()
52
+ # Fall back to 'text' or other fields if present
53
+ if "text" in choice:
54
+ return choice["text"].strip()
55
+ # fallback: try to find a top-level text
56
+ if isinstance(data, dict) and "text" in data:
57
+ return data["text"].strip()
58
+ return "Error: Unexpected response format from API"
59
  except requests.exceptions.RequestException as e:
60
  return f"Error: Could not connect to the API - {e}"
61
+ except ValueError:
62
+ return "Error: Could not decode JSON response from API"
63
 
64
+ # -------------------------
65
+ # Gradio response function
66
+ # -------------------------
67
  def respond(user_message, chat_history):
68
  """
69
+ Gradio passes chat_history as a list of (user, bot) tuples, or None.
70
+ Return updated history and an empty string to clear the input box.
 
71
  """
72
+ if chat_history is None:
73
+ chat_history = []
74
+
75
+ # Build messages for the model
76
  messages = [
77
  {"role": "system", "content": "You are LocoBot, a helpful assistant. Answer concisely and politely."}
78
  ]
79
 
80
+ # Re-add previous messages so model has context
81
+ for user, bot in chat_history:
82
+ messages.append({"role": "user", "content": user})
83
+ messages.append({"role": "assistant", "content": bot})
 
84
 
85
  # Add current user message
86
  messages.append({"role": "user", "content": user_message})
87
 
88
  assistant_reply = query_gpt35(messages)
89
 
90
+ # Append to history and return (and clear the input textbox)
91
  new_history = chat_history + [(user_message, assistant_reply)]
92
  return new_history, ""
93
 
94
+ # -------------------------
95
+ # Build and launch UI
96
+ # -------------------------
97
  with gr.Blocks() as demo:
98
+ gr.Markdown("## LocoBot — Your AI Companion (OpenRouter)")
99
  chatbot = gr.Chatbot()
100
+ message = gr.Textbox(placeholder="Type your message and press Enter", lines=2)
101
  send = gr.Button("Send")
102
 
 
103
  send.click(fn=respond, inputs=[message, chatbot], outputs=[chatbot, message])
104
  message.submit(fn=respond, inputs=[message, chatbot], outputs=[chatbot, message])
105