File size: 1,515 Bytes
ded7c37
7d707d7
7681731
11a5c67
7681731
 
8d9e8f5
7681731
816f570
7d707d7
ded7c37
 
7681731
 
 
 
 
ded7c37
 
7681731
ded7c37
 
 
 
1d49548
ded7c37
a8939bc
ded7c37
 
 
7681731
ded7c37
a8939bc
ded7c37
 
 
a8939bc
ded7c37
 
 
 
 
 
 
7681731
a8939bc
7681731
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
import requests
import os  # ✅ 用于读取 secret

# ✅ 从 Hugging Face Secret 中获取 token
API_TOKEN = os.getenv("HF_API_TOKEN")
st.markdown(API_TOKEN)
# ✅ 你私有后端的空间地址(注意大小写)
BACKEND_URL = "https://dsbb0707-SpatialParseback.hf.space/api/predict/"

def call_backend(input_text):
    try:
        # ✅ 加入 Authorization 头
        headers = {
            "Authorization": f"Bearer {API_TOKEN}"
        }

        response = requests.post(
            BACKEND_URL,
            headers=headers,  # ✅ 加入 header
            json={"data": [input_text]},
            timeout=10
        )
        if response.status_code == 200:
            result = response.json()["data"][0]
            return f"✅ {result['result']}\n⏰ {result['timestamp']}"
        return f"❌ Backend Error (HTTP {response.status_code})"
    except Exception as e:
        return f"⚠️ Connection Error: {str(e)}"

# ✅ Gradio 界面构建
with gr.Blocks() as demo:
    gr.Markdown("## 前端交互界面")
    with gr.Row():
        input_box = gr.Textbox(label="输入文本", placeholder="请输入...")
        output_box = gr.Textbox(label="处理结果", interactive=False)
    submit_btn = gr.Button("提交", variant="primary")
    
    submit_btn.click(
        fn=call_backend,
        inputs=input_box,
        outputs=output_box
    )

# ✅ 正确的 Gradio 启动方式
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)