Shunfeng Zheng commited on
Commit
bce96ee
·
verified ·
1 Parent(s): 2d8da02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -14
app.py CHANGED
@@ -1,19 +1,47 @@
1
  import gradio as gr
 
 
2
 
3
- def parse_spatial(text):
4
- # 在这里添加您的文本解析逻辑
5
- return f"[Parsed Spatial Info] {text.upper()}"
6
 
7
- # 创建 Gradio 接口,并指定 API 名称
8
- demo = gr.Interface(
9
- fn=parse_spatial,
10
- inputs=gr.Textbox(lines=2, placeholder="Enter your spatial description here..."),
11
- outputs="text",
12
- title="Spatial Parser Demo",
13
- description="Enter a spatial description and receive the parsed output.",
14
- api_name="/predict"
15
- )
16
 
17
- # 启动应用
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  if __name__ == "__main__":
19
- demo.launch()
 
1
  import gradio as gr
2
+ import requests
3
+ import os # ✅ 用于读取 secret
4
 
5
+ # ✅ 从 Hugging Face Secret 中获取 token
6
+ API_TOKEN = os.getenv("HF_API_TOKEN")
 
7
 
8
+ # 你私有后端的空间地址(注意大小写)
9
+ BACKEND_URL = "https://dsbb0707-SpatialParseback.hf.space/api/predict/"
 
 
 
 
 
 
 
10
 
11
+ def call_backend(input_text):
12
+ try:
13
+ # ✅ 加入 Authorization 头
14
+ headers = {
15
+ "Authorization": f"Bearer {API_TOKEN}"
16
+ }
17
+
18
+ response = requests.post(
19
+ BACKEND_URL,
20
+ headers=headers, # ✅ 加入 header
21
+ json={"data": [input_text]},
22
+ timeout=10
23
+ )
24
+ if response.status_code == 200:
25
+ result = response.json()["data"][0]
26
+ return f"✅ {result['result']}\n⏰ {result['timestamp']}"
27
+ return f"❌ Backend Error (HTTP {response.status_code})"
28
+ except Exception as e:
29
+ return f"⚠️ Connection Error: {str(e)}"
30
+
31
+ # ✅ Gradio 界面构建
32
+ with gr.Blocks() as demo:
33
+ gr.Markdown("## 前端交互界面")
34
+ with gr.Row():
35
+ input_box = gr.Textbox(label="输入文本", placeholder="请输入...")
36
+ output_box = gr.Textbox(label="处理结果", interactive=False)
37
+ submit_btn = gr.Button("提交", variant="primary")
38
+
39
+ submit_btn.click(
40
+ fn=call_backend,
41
+ inputs=input_box,
42
+ outputs=output_box
43
+ )
44
+
45
+ # ✅ 正确的 Gradio 启动方式
46
  if __name__ == "__main__":
47
+ demo.launch(server_name="0.0.0.0", server_port=7860)