Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| import os | |
| API_TOKEN = os.getenv("HF_API_TOKEN") | |
| BACKEND_URL = "https://your-backend-url/api/predict/" # 替换为你的后端地址 | |
| def call_backend(input_text): | |
| try: | |
| headers = { | |
| "Authorization": f"Bearer {API_TOKEN}" | |
| } | |
| response = requests.post( | |
| BACKEND_URL, | |
| headers=headers, | |
| 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)}" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("## 地理信息识别系统") | |
| with gr.Row(): | |
| input_box = gr.Textbox(label="输入描述文本") | |
| output_box = gr.Textbox(label="识别结果", interactive=False) | |
| submit_btn = gr.Button("提交") | |
| submit_btn.click(fn=call_backend, inputs=input_box, outputs=output_box) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |