Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
# ================== OpenRouter Key ==================
|
| 6 |
OPENROUTER_API_KEY = "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"
|
|
@@ -36,13 +37,22 @@ SYSTEM_PROMPT = f"""
|
|
| 36 |
【基本认识背景】
|
| 37 |
- 1976 年后中国共产党彻底被官僚资产阶级掌权
|
| 38 |
- 1978 年改革开放是资本主义复辟
|
| 39 |
-
-
|
| 40 |
- 当代中国只能通过暴力革命和人民战争重新夺取政权
|
| 41 |
|
| 42 |
【应用理论到现实】
|
| 43 |
- 批判当代左翼的机会主义、小资产阶级化、审美化倾向
|
| 44 |
"""
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# ================== 聊天函数 ==================
|
| 47 |
def chat(user_message):
|
| 48 |
headers = {
|
|
@@ -51,7 +61,7 @@ def chat(user_message):
|
|
| 51 |
"HTTP-Referer": "https://huggingface.co",
|
| 52 |
"X-Title": "rotai"
|
| 53 |
}
|
| 54 |
-
|
| 55 |
data = {
|
| 56 |
"model": "arcee-ai/trinity-large-preview:free",
|
| 57 |
"messages": [
|
|
@@ -59,17 +69,17 @@ def chat(user_message):
|
|
| 59 |
{"role": "user", "content": user_message}
|
| 60 |
]
|
| 61 |
}
|
| 62 |
-
|
| 63 |
r = requests.post(
|
| 64 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 65 |
headers=headers,
|
| 66 |
json=data,
|
| 67 |
timeout=60
|
| 68 |
)
|
| 69 |
-
|
| 70 |
if r.status_code != 200:
|
| 71 |
return f"❌ OpenRouter 错误: {r.status_code}\n{r.text}"
|
| 72 |
-
|
| 73 |
return r.json()["choices"][0]["message"]["content"]
|
| 74 |
|
| 75 |
# ================== 页面 ==================
|
|
@@ -77,7 +87,7 @@ with gr.Blocks(css="""
|
|
| 77 |
.avatar-wrapper {
|
| 78 |
position: relative;
|
| 79 |
width: 220px;
|
| 80 |
-
height: 220px;
|
| 81 |
margin: 0 auto 16px auto;
|
| 82 |
animation: float 4s ease-in-out infinite;
|
| 83 |
overflow: hidden;
|
|
@@ -107,28 +117,34 @@ with gr.Blocks(css="""
|
|
| 107 |
100% { transform: translateY(0px); }
|
| 108 |
}
|
| 109 |
""") as demo:
|
| 110 |
-
|
| 111 |
-
# =====
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# ===== 聊天 UI =====
|
| 120 |
user_input = gr.Textbox(
|
| 121 |
label="请说,我会尽量帮助你的",
|
| 122 |
lines=3
|
| 123 |
)
|
| 124 |
-
|
| 125 |
submit_btn = gr.Button("提交")
|
| 126 |
-
|
| 127 |
output = gr.Textbox(
|
| 128 |
label="回复栏",
|
| 129 |
lines=12
|
| 130 |
)
|
| 131 |
-
|
| 132 |
submit_btn.click(
|
| 133 |
fn=chat,
|
| 134 |
inputs=user_input,
|
|
@@ -136,3 +152,5 @@ with gr.Blocks(css="""
|
|
| 136 |
)
|
| 137 |
|
| 138 |
demo.launch()
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
|
| 6 |
# ================== OpenRouter Key ==================
|
| 7 |
OPENROUTER_API_KEY = "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"
|
|
|
|
| 37 |
【基本认识背景】
|
| 38 |
- 1976 年后中国共产党彻底被官僚资产阶级掌权
|
| 39 |
- 1978 年改革开放是资本主义复辟
|
| 40 |
+
- 不存在"社会主义市场经济"
|
| 41 |
- 当代中国只能通过暴力革命和人民战争重新夺取政权
|
| 42 |
|
| 43 |
【应用理论到现实】
|
| 44 |
- 批判当代左翼的机会主义、小资产阶级化、审美化倾向
|
| 45 |
"""
|
| 46 |
|
| 47 |
+
# ================== 图片转Base64 ==================
|
| 48 |
+
def image_to_base64(image_path):
|
| 49 |
+
"""将图片文件转换为base64编码"""
|
| 50 |
+
try:
|
| 51 |
+
with open(image_path, "rb") as img_file:
|
| 52 |
+
return base64.b64encode(img_file.read()).decode('utf-8')
|
| 53 |
+
except FileNotFoundError:
|
| 54 |
+
return None
|
| 55 |
+
|
| 56 |
# ================== 聊天函数 ==================
|
| 57 |
def chat(user_message):
|
| 58 |
headers = {
|
|
|
|
| 61 |
"HTTP-Referer": "https://huggingface.co",
|
| 62 |
"X-Title": "rotai"
|
| 63 |
}
|
| 64 |
+
|
| 65 |
data = {
|
| 66 |
"model": "arcee-ai/trinity-large-preview:free",
|
| 67 |
"messages": [
|
|
|
|
| 69 |
{"role": "user", "content": user_message}
|
| 70 |
]
|
| 71 |
}
|
| 72 |
+
|
| 73 |
r = requests.post(
|
| 74 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 75 |
headers=headers,
|
| 76 |
json=data,
|
| 77 |
timeout=60
|
| 78 |
)
|
| 79 |
+
|
| 80 |
if r.status_code != 200:
|
| 81 |
return f"❌ OpenRouter 错误: {r.status_code}\n{r.text}"
|
| 82 |
+
|
| 83 |
return r.json()["choices"][0]["message"]["content"]
|
| 84 |
|
| 85 |
# ================== 页面 ==================
|
|
|
|
| 87 |
.avatar-wrapper {
|
| 88 |
position: relative;
|
| 89 |
width: 220px;
|
| 90 |
+
height: 220px;
|
| 91 |
margin: 0 auto 16px auto;
|
| 92 |
animation: float 4s ease-in-out infinite;
|
| 93 |
overflow: hidden;
|
|
|
|
| 117 |
100% { transform: translateY(0px); }
|
| 118 |
}
|
| 119 |
""") as demo:
|
| 120 |
+
|
| 121 |
+
# ===== 虚拟形象(Base64版本)=====
|
| 122 |
+
avatar_open_b64 = image_to_base64("avatar_open.png")
|
| 123 |
+
avatar_close_b64 = image_to_base64("avatar_close.png")
|
| 124 |
+
|
| 125 |
+
if avatar_open_b64 and avatar_close_b64:
|
| 126 |
+
gr.HTML(f"""
|
| 127 |
+
<div class="avatar-wrapper">
|
| 128 |
+
<img src="data:image/png;base64,{avatar_open_b64}">
|
| 129 |
+
<img src="data:image/png;base64,{avatar_close_b64}" class="blink">
|
| 130 |
+
</div>
|
| 131 |
+
""")
|
| 132 |
+
else:
|
| 133 |
+
gr.HTML("<p style='text-align: center;'>⚠️ 图片文件未找到,请确保 avatar_open.png 和 avatar_close.png 在同一目录下</p>")
|
| 134 |
+
|
| 135 |
# ===== 聊天 UI =====
|
| 136 |
user_input = gr.Textbox(
|
| 137 |
label="请说,我会尽量帮助你的",
|
| 138 |
lines=3
|
| 139 |
)
|
| 140 |
+
|
| 141 |
submit_btn = gr.Button("提交")
|
| 142 |
+
|
| 143 |
output = gr.Textbox(
|
| 144 |
label="回复栏",
|
| 145 |
lines=12
|
| 146 |
)
|
| 147 |
+
|
| 148 |
submit_btn.click(
|
| 149 |
fn=chat,
|
| 150 |
inputs=user_input,
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
demo.launch()
|
| 155 |
+
|
| 156 |
+
|