Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,53 @@
|
|
| 1 |
import requests
|
| 2 |
-
import os
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# OpenRouter API endpoint
|
| 10 |
MODEL_URL = "https://openrouter.ai/api/v1/chat/completions"
|
| 11 |
|
| 12 |
HEADERS = {
|
| 13 |
"Authorization": f"Bearer {API_KEY}",
|
| 14 |
-
"Content-Type": "application/json"
|
| 15 |
-
"HTTP-Referer": "https://your-app-url.com", # Optional but recommended
|
| 16 |
-
"X-Title": "LocoBot" # Optional: your app name
|
| 17 |
}
|
| 18 |
|
| 19 |
-
def query_gpt35(
|
| 20 |
-
"""Send a
|
| 21 |
payload = {
|
| 22 |
"model": "openai/gpt-3.5-turbo",
|
| 23 |
-
"messages":
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
],
|
| 27 |
-
"max_tokens": 200,
|
| 28 |
-
"temperature": 0.7
|
| 29 |
}
|
| 30 |
-
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
return
|
| 36 |
except requests.exceptions.RequestException as e:
|
| 37 |
-
return f"Error: Could not connect to the API - {
|
| 38 |
except (KeyError, IndexError):
|
| 39 |
return "Error: Unexpected response format from API"
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
"""
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
css = """
|
| 46 |
button {
|
|
@@ -55,13 +62,14 @@ button:hover {
|
|
| 55 |
}
|
| 56 |
"""
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
)
|
|
|
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
|
|
|
|
| 1 |
import requests
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# 🔑 Paste your OpenRouter API key here
|
| 5 |
+
API_KEY = "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"
|
| 6 |
+
|
| 7 |
+
if not API_KEY or API_KEY.startswith("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"):
|
| 8 |
+
raise ValueError("Please paste your actual OpenRouter API key in API_KEY variable.")
|
| 9 |
|
| 10 |
# OpenRouter API endpoint
|
| 11 |
MODEL_URL = "https://openrouter.ai/api/v1/chat/completions"
|
| 12 |
|
| 13 |
HEADERS = {
|
| 14 |
"Authorization": f"Bearer {API_KEY}",
|
| 15 |
+
"Content-Type": "application/json"
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
+
def query_gpt35(messages, max_tokens=500, temperature=0.7):
|
| 19 |
+
"""Send a list of messages to OpenRouter GPT-3.5-Turbo."""
|
| 20 |
payload = {
|
| 21 |
"model": "openai/gpt-3.5-turbo",
|
| 22 |
+
"messages": messages,
|
| 23 |
+
"max_tokens": max_tokens,
|
| 24 |
+
"temperature": temperature
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
+
|
| 27 |
try:
|
| 28 |
+
resp = requests.post(MODEL_URL, headers=HEADERS, json=payload, timeout=30)
|
| 29 |
+
resp.raise_for_status()
|
| 30 |
+
data = resp.json()
|
| 31 |
+
return data["choices"][0]["message"]["content"].strip()
|
| 32 |
except requests.exceptions.RequestException as e:
|
| 33 |
+
return f"Error: Could not connect to the API - {e}"
|
| 34 |
except (KeyError, IndexError):
|
| 35 |
return "Error: Unexpected response format from API"
|
| 36 |
|
| 37 |
+
def respond(user_message, chat_history):
|
| 38 |
+
"""Handles the chat flow in Gradio."""
|
| 39 |
+
messages = [
|
| 40 |
+
{"role": "system", "content": "You are LocoBot, a helpful assistant."}
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
if chat_history:
|
| 44 |
+
for user, bot in chat_history:
|
| 45 |
+
messages.append({"role": "user", "content": user})
|
| 46 |
+
messages.append({"role": "assistant", "content": bot})
|
| 47 |
+
|
| 48 |
+
messages.append({"role": "user", "content": user_message})
|
| 49 |
+
assistant_reply = query_gpt35(messages)
|
| 50 |
+
return chat_history + [(user_message, assistant_reply)], ""
|
| 51 |
|
| 52 |
css = """
|
| 53 |
button {
|
|
|
|
| 62 |
}
|
| 63 |
"""
|
| 64 |
|
| 65 |
+
with gr.Blocks(css=css) as demo:
|
| 66 |
+
gr.Markdown("## LocoBot — Your AI Companion (via OpenRouter GPT-3.5 Turbo)")
|
| 67 |
+
chatbot = gr.Chatbot()
|
| 68 |
+
message = gr.Textbox(placeholder="Type your message and press Enter")
|
| 69 |
+
send = gr.Button("Send")
|
| 70 |
+
|
| 71 |
+
send.click(fn=respond, inputs=[message, chatbot], outputs=[chatbot, message])
|
| 72 |
+
message.submit(fn=respond, inputs=[message, chatbot], outputs=[chatbot, message])
|
| 73 |
|
| 74 |
if __name__ == "__main__":
|
| 75 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|