jiang1002 commited on
Commit
2e301f3
·
verified ·
1 Parent(s): 32bb8ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -31
app.py CHANGED
@@ -1,34 +1,35 @@
1
- from fastapi import FastAPI, Request
2
- import os, requests
 
3
 
4
- app = FastAPI()
5
- # 获取你的 Secret
6
- HF_TOKEN = os.environ.get('HF_TOKEN')
7
- # ✅ 核心:这是针对你这个模型的唯一合法地址
8
- API_URL = "https://router.huggingface.co/jiang1002/chatglm-6b-adgen"
9
 
10
- @app.post("/generate")
11
- async def generate(request: Request):
12
- try:
13
- body = await request.json()
14
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
15
- payload = {
16
- "inputs": body.get("text", "你好"),
17
- "parameters": {"max_new_tokens": 512, "do_sample": True},
18
- "options": {"wait_for_model": True}
19
- }
20
-
21
- # 发送请求
22
- response = requests.post(API_URL, headers=headers, json=payload, timeout=120)
23
-
24
- if response.status_code == 200:
25
- return {"success": True, "response": response.json()}
26
- else:
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
- @app.get("/")
33
- def home():
34
- return {"status": "Running", "token_set": bool(HF_TOKEN), "current_endpoint": API_URL}
 
 
 
 
 
 
 
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()