Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
try:
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
else:
|
| 11 |
# 否则直接访问根路径
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
except Exception as e:
|
| 17 |
-
return
|
| 18 |
|
| 19 |
-
#
|
| 20 |
demo = gr.Interface(
|
| 21 |
-
fn=
|
| 22 |
-
inputs=gr.Textbox(
|
| 23 |
-
outputs=gr.Textbox(),
|
| 24 |
title="API Proxy",
|
| 25 |
description="Access beibeioo.top through this proxy"
|
| 26 |
)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.responses import Response
|
| 5 |
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
@app.get("/{full_path:path}")
|
| 9 |
+
@app.post("/{full_path:path}")
|
| 10 |
+
async def proxy(full_path: str, request: Request):
|
| 11 |
try:
|
| 12 |
+
# 构建目标 URL
|
| 13 |
+
if full_path.startswith("api/"):
|
| 14 |
+
# 如果路径以 api/ 开头,去掉 api/ 后转发
|
| 15 |
+
actual_path = full_path[4:] # 跳过 "api/"
|
| 16 |
+
target_url = f"http://beibeioo.top/{actual_path}"
|
| 17 |
else:
|
| 18 |
# 否则直接访问根路径
|
| 19 |
+
target_url = "http://beibeioo.top"
|
| 20 |
+
|
| 21 |
+
# 转发请求
|
| 22 |
+
response = requests.request(
|
| 23 |
+
method=request.method,
|
| 24 |
+
url=target_url,
|
| 25 |
+
headers={
|
| 26 |
+
'User-Agent': 'HuggingFace-Space-Proxy',
|
| 27 |
+
'Accept': '*/*'
|
| 28 |
+
}
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# 返回响应
|
| 32 |
+
return Response(
|
| 33 |
+
content=response.content,
|
| 34 |
+
status_code=response.status_code,
|
| 35 |
+
headers={
|
| 36 |
+
'Content-Type': response.headers.get('Content-Type', 'application/json'),
|
| 37 |
+
'Access-Control-Allow-Origin': '*'
|
| 38 |
+
}
|
| 39 |
+
)
|
| 40 |
except Exception as e:
|
| 41 |
+
return {"error": str(e)}
|
| 42 |
|
| 43 |
+
# 为了保持 Gradio UI 可见,添加一个简单的界面
|
| 44 |
demo = gr.Interface(
|
| 45 |
+
fn=lambda x: x,
|
| 46 |
+
inputs=gr.Textbox(visible=False),
|
| 47 |
+
outputs=gr.Textbox(visible=False),
|
| 48 |
title="API Proxy",
|
| 49 |
description="Access beibeioo.top through this proxy"
|
| 50 |
)
|
| 51 |
|
| 52 |
+
app = gr.mount_gradio_app(app, demo, path="/ui")
|
|
|