Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# Retrieve the Open Router API Key from the Space secrets
|
| 6 |
API_KEY = os.getenv("OpenRounter_API_KEY")
|
| 7 |
|
| 8 |
-
def chat_with_openrouter(
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
"
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"top_p": 1,
|
| 15 |
"temperature": 1,
|
|
|
|
|
|
|
| 16 |
"repetition_penalty": 1,
|
| 17 |
-
"
|
| 18 |
-
|
| 19 |
-
|
| 20 |
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
inputs=gr.Textbox(lines=2, placeholder="Ask me anything"),
|
| 25 |
-
outputs="text",
|
| 26 |
-
title="Chat with OpenRouter",
|
| 27 |
-
)
|
| 28 |
|
| 29 |
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import json
|
| 4 |
import os
|
| 5 |
|
| 6 |
# Retrieve the Open Router API Key from the Space secrets
|
| 7 |
API_KEY = os.getenv("OpenRounter_API_KEY")
|
| 8 |
|
| 9 |
+
def chat_with_openrouter(input_text):
|
| 10 |
+
response = requests.post(
|
| 11 |
+
url="https://openrouter.ai/api/v1/chat/completions",
|
| 12 |
+
headers={
|
| 13 |
+
"Authorization": f"Bearer {API_KEY}"
|
| 14 |
+
},
|
| 15 |
+
data=json.dumps({
|
| 16 |
+
"model": "openai/gpt-4o-mini-2024-07-18", # Optional
|
| 17 |
+
"messages": [
|
| 18 |
+
{"role": "user", "content": input_text}
|
| 19 |
+
]
|
| 20 |
"top_p": 1,
|
| 21 |
"temperature": 1,
|
| 22 |
+
"frequency_penalty": 0,
|
| 23 |
+
"presence_penalty": 0,
|
| 24 |
"repetition_penalty": 1,
|
| 25 |
+
"top_k": 0,
|
| 26 |
+
})
|
| 27 |
+
)
|
| 28 |
return response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response")
|
| 29 |
|
| 30 |
+
# Create Gradio interface
|
| 31 |
+
iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
iface.launch()
|