Shunfeng Zheng commited on
Commit
5f9c4de
·
verified ·
1 Parent(s): 1f634b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -1,23 +1,21 @@
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-SpatialParsebackcopy.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,20 +26,21 @@ def call_backend(input_text):
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)
 
1
+ import streamlit as st
2
  import requests
3
+ import os
4
 
5
+ # ✅ 从 Hugging Face Secrets 中读取 API Token
6
  API_TOKEN = os.getenv("HF_API_TOKEN")
7
 
8
+ # ✅ 设置后端 API 地址
9
  BACKEND_URL = "https://dsbb0707-SpatialParsebackcopy.hf.space/api/predict/"
10
 
11
  def call_backend(input_text):
12
  try:
 
13
  headers = {
14
  "Authorization": f"Bearer {API_TOKEN}"
15
  }
 
16
  response = requests.post(
17
  BACKEND_URL,
18
+ headers=headers,
19
  json={"data": [input_text]},
20
  timeout=10
21
  )
 
26
  except Exception as e:
27
  return f"⚠️ Connection Error: {str(e)}"
28
 
29
+ # ✅ Streamlit UI 界面
30
+ st.set_page_config(page_title="空间信息前端", page_icon="🧠")
31
+ st.title("🌏 空间解析前端")
32
+ st.markdown("通过 Hugging Face API 与后端交互")
33
+
34
+ # 用户输入
35
+ user_input = st.text_input("请输入文本", "")
36
+
37
+ # ✅ 提交按钮
38
+ if st.button("提交"):
39
+ if not user_input.strip():
40
+ st.warning("请先输入文本。")
41
+ else:
42
+ with st.spinner("🔄 正在调用后端..."):
43
+ result = call_backend(user_input)
44
+ st.success("完成!")
45
+ st.text_area("后端返回结果", result, height=150)
46