Spaces:
Sleeping
Sleeping
Shunfeng Zheng
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,66 @@
|
|
| 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 |
-
BACKEND_URL = "https://dsbb0707--dockerb2.hf.space/predict"
|
| 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 |
-
)
|
| 22 |
-
if response.status_code == 200:
|
| 23 |
-
result = response.json()["data"][0]
|
| 24 |
-
return f"✅ {result['result']}\n⏰ {result['timestamp']}"
|
| 25 |
-
return f"❌ Backend Error (HTTP {response.status_code})"
|
| 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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# BACKEND_URL = "https://dsbb0707--dockerb2.hf.space/predict"
|
| 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 |
+
# )
|
| 22 |
+
# if response.status_code == 200:
|
| 23 |
+
# result = response.json()["data"][0]
|
| 24 |
+
# return f"✅ {result['result']}\n⏰ {result['timestamp']}"
|
| 25 |
+
# return f"❌ Backend Error (HTTP {response.status_code})"
|
| 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 |
+
|
| 47 |
+
import gradio as gr
|
| 48 |
+
import time # 模拟处理耗时
|
| 49 |
+
|
| 50 |
+
def process_api(input_text):
|
| 51 |
+
# 这里编写实际的后端处理逻辑
|
| 52 |
+
time.sleep(1) # 模拟处理延迟
|
| 53 |
+
return {
|
| 54 |
+
"status": "success",
|
| 55 |
+
"result": f"Processed: {input_text.upper()}",
|
| 56 |
+
"timestamp": time.time()
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# 设置API格式为JSON
|
| 60 |
+
gr.Interface(
|
| 61 |
+
fn=process_api,
|
| 62 |
+
inputs="text",
|
| 63 |
+
outputs="json",
|
| 64 |
+
title="Backend API",
|
| 65 |
+
allow_flagging="never"
|
| 66 |
+
).launch()
|