Spaces:
Sleeping
Sleeping
import streamlit as st | |
import requests | |
import os | |
# ✅ 从 Hugging Face Secrets 中读取 API Token | |
API_TOKEN = os.getenv("HF_API_TOKEN") | |
# ✅ 设置后端 API 地址 | |
BACKEND_URL = "https://dsbb0707-SpatialParsebackcopy.hf.space/api/predict/" | |
# BACKEND_URL = "https://dsbb0707--dockerb2.hf.space/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)}" | |
# ✅ Streamlit UI 界面 | |
st.set_page_config(page_title="空间信息前端", page_icon="🧠") | |
st.title("🌏 空间解析前端") | |
st.markdown("通过 Hugging Face API 与后端交互") | |
# ✅ 用户输入 | |
user_input = st.text_input("请输入文本", "") | |
# ✅ 提交按钮 | |
if st.button("提交"): | |
if not user_input.strip(): | |
st.warning("请先输入文本。") | |
else: | |
with st.spinner("🔄 正在调用后端..."): | |
result = call_backend(user_input) | |
st.success("完成!") | |
st.text_area("后端返回结果", result, height=150) | |
# import gradio as gr | |
# import time # 模拟处理耗时 | |
# def process_api(input_text): | |
# # 这里编写实际的后端处理逻辑 | |
# time.sleep(1) # 模拟处理延迟 | |
# return { | |
# "status": "success", | |
# "result": f"Processed: {input_text.upper()}", | |
# "timestamp": time.time() | |
# } | |
# # 设置API格式为JSON | |
# gr.Interface( | |
# fn=process_api, | |
# inputs="text", | |
# outputs="json", | |
# title="Backend API", | |
# allow_flagging="never" | |
# ).launch() |