Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
OPENROUTER_API_KEY = "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"
|
| 6 |
+
|
| 7 |
+
def chat(user_message):
|
| 8 |
+
headers = {
|
| 9 |
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
| 10 |
+
"Content-Type": "application/json",
|
| 11 |
+
"HTTP-Referer": "https://huggingface.co",
|
| 12 |
+
"X-Title": "rotai"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
data = {
|
| 16 |
+
"model": "arcee-ai/trinity-large-preview:free",
|
| 17 |
+
"messages": [
|
| 18 |
+
{
|
| 19 |
+
"role": "system",
|
| 20 |
+
"content": "你是一个站无产阶级立场,运用马克思主义的世界观和方法论的理论助手。"
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"role": "user",
|
| 24 |
+
"content": user_message
|
| 25 |
+
}
|
| 26 |
+
]
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
r = requests.post(
|
| 30 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 31 |
+
headers=headers,
|
| 32 |
+
json=data,
|
| 33 |
+
timeout=60
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
if r.status_code != 200:
|
| 37 |
+
return f"❌ OpenRouter 错误: {r.status_code}\n{r.text}"
|
| 38 |
+
|
| 39 |
+
return r.json()["choices"][0]["message"]["content"]
|
| 40 |
+
|
| 41 |
+
gr.Interface(
|
| 42 |
+
fn=chat,
|
| 43 |
+
inputs=gr.Textbox(label="你说"),
|
| 44 |
+
outputs=gr.Textbox(label="AI 回复")
|
| 45 |
+
).launch()
|