Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,35 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
HF_TOKEN = os.environ.get('HF_TOKEN')
|
| 7 |
-
# ✅ 核心:这是针对你这个模型的唯一合法地址
|
| 8 |
-
API_URL = "https://router.huggingface.co/jiang1002/chatglm-6b-adgen"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return {"success": False, "error": f"HF_{response.status_code}", "detail": response.text}
|
| 29 |
-
except Exception as e:
|
| 30 |
-
return {"success": False, "error": "SPACE_INTERNAL_ERROR", "detail": str(e)}
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# 这里的逻辑是:你的 Space 已经是一个运行环境了
|
| 6 |
+
# 我们直接在网页上通过 Gradio 搭建对话框
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def chat_function(message, history):
|
| 9 |
+
# 这里依然调用你本地模型或 API 的逻辑,但我们换一种更稳的方式
|
| 10 |
+
# 如果你本地有加载模型代码,就写在这里;
|
| 11 |
+
# 如果还是想调 API,我们用这个最简单的逻辑:
|
| 12 |
+
|
| 13 |
+
API_URL = "https://router.huggingface.co/jiang1002/chatglm-6b-adgen"
|
| 14 |
+
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
|
| 15 |
+
payload = {"inputs": message, "options": {"wait_for_model": True}}
|
| 16 |
+
|
| 17 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 18 |
+
|
| 19 |
+
if response.status_code == 200:
|
| 20 |
+
result = response.json()
|
| 21 |
+
if isinstance(result, list):
|
| 22 |
+
return result[0].get("generated_text", "出错了")
|
| 23 |
+
return str(result)
|
| 24 |
+
else:
|
| 25 |
+
return f"错误:{response.status_code}, {response.text}"
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# 搭建漂亮的网页界面
|
| 28 |
+
demo = gr.ChatInterface(
|
| 29 |
+
fn=chat_function,
|
| 30 |
+
title="ChatGLM-6B 广告生成助手",
|
| 31 |
+
description="点击下方输入框,输入你的需求,直接开始测试!"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch()
|