Spaces:
Runtime error
Runtime error
Shunfeng Zheng
commited on
Upload 2 files
Browse files- app.py +10 -21
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,23 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import os
|
| 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,
|
| 21 |
json={"data": [input_text]},
|
| 22 |
timeout=10
|
| 23 |
)
|
|
@@ -28,20 +23,14 @@ 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="
|
| 36 |
-
output_box = gr.Textbox(label="
|
| 37 |
-
submit_btn = gr.Button("提交"
|
| 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 gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
|
|
|
|
| 5 |
API_TOKEN = os.getenv("HF_API_TOKEN")
|
| 6 |
+
BACKEND_URL = "https://your-backend-url/api/predict/" # 替换为你的后端地址
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def call_backend(input_text):
|
| 9 |
try:
|
|
|
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {API_TOKEN}"
|
| 12 |
}
|
|
|
|
| 13 |
response = requests.post(
|
| 14 |
BACKEND_URL,
|
| 15 |
+
headers=headers,
|
| 16 |
json={"data": [input_text]},
|
| 17 |
timeout=10
|
| 18 |
)
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
return f"⚠️ Connection Error: {str(e)}"
|
| 25 |
|
|
|
|
| 26 |
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("## 地理信息识别系统")
|
| 28 |
with gr.Row():
|
| 29 |
+
input_box = gr.Textbox(label="输入描述文本")
|
| 30 |
+
output_box = gr.Textbox(label="识别结果", interactive=False)
|
| 31 |
+
submit_btn = gr.Button("提交")
|
| 32 |
+
|
| 33 |
+
submit_btn.click(fn=call_backend, inputs=input_box, outputs=output_box)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
|
| 2 |
requests
|
|
|
|
| 1 |
+
gradio
|
| 2 |
requests
|