Shunfeng Zheng commited on
Commit
7681731
·
verified ·
1 Parent(s): 816f570

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -3
app.py CHANGED
@@ -1,12 +1,23 @@
1
  import gradio as gr
2
  import requests
 
3
 
 
 
 
 
4
  BACKEND_URL = "https://dsbb0707-SpatialParseback.hf.space/api/predict/"
5
 
6
  def call_backend(input_text):
7
  try:
 
 
 
 
 
8
  response = requests.post(
9
  BACKEND_URL,
 
10
  json={"data": [input_text]},
11
  timeout=10
12
  )
@@ -17,7 +28,7 @@ def call_backend(input_text):
17
  except Exception as e:
18
  return f"⚠️ Connection Error: {str(e)}"
19
 
20
- # Gradio界面构建
21
  with gr.Blocks() as demo:
22
  gr.Markdown("## 前端交互界面")
23
  with gr.Row():
@@ -31,6 +42,6 @@ with gr.Blocks() as demo:
31
  outputs=output_box
32
  )
33
 
34
- # 关键修改:使用正确的Gradio启动方式
35
  if __name__ == "__main__":
36
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
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
  )
 
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():
 
42
  outputs=output_box
43
  )
44
 
45
+ # ✅ 正确的 Gradio 启动方式
46
  if __name__ == "__main__":
47
+ demo.launch(server_name="0.0.0.0", server_port=7860)