Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,33 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import os
|
| 3 |
|
| 4 |
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
{"role": "user", "content":
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
print(data["choices"][0]["message"]["content"])
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
API_KEY = os.getenv("OPENROUTER_API_KEY")
|
| 6 |
|
| 7 |
+
def ask_llm(message):
|
| 8 |
+
response = requests.post(
|
| 9 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 10 |
+
headers={
|
| 11 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 12 |
+
"Content-Type": "application/json",
|
| 13 |
+
},
|
| 14 |
+
json={
|
| 15 |
+
"model": "kwaipilot/kat-coder-pro:free",
|
| 16 |
+
"messages": [{"role": "user", "content": message}]
|
| 17 |
+
}
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
data = response.json()
|
| 21 |
|
| 22 |
+
# return text output
|
| 23 |
+
return data["choices"][0]["message"]["content"]
|
| 24 |
+
|
| 25 |
+
# Gradio Interface
|
| 26 |
+
ui = gr.Interface(
|
| 27 |
+
fn=ask_llm,
|
| 28 |
+
inputs=gr.Textbox(label="Ask your question:"),
|
| 29 |
+
outputs=gr.Textbox(label="LLM Reply"),
|
| 30 |
+
title="OpenRouter Free Chat",
|
| 31 |
+
)
|
| 32 |
|
| 33 |
+
ui.launch()
|
|
|