Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,89 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
try:
|
| 6 |
-
#
|
| 7 |
target_url = f"http://beibeioo.top/{path}"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
except Exception as e:
|
| 15 |
-
return
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
app
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.responses import JSONResponse, Response
|
| 4 |
import requests
|
| 5 |
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
@app.get("/api/{path:path}")
|
| 9 |
+
@app.post("/api/{path:path}")
|
| 10 |
+
async def proxy_api(path: str, request: Request):
|
| 11 |
try:
|
| 12 |
+
# 构建 API 请求的目标 URL
|
| 13 |
target_url = f"http://beibeioo.top/{path}"
|
| 14 |
|
| 15 |
+
# 获取原始请求方法
|
| 16 |
+
method = request.method
|
| 17 |
+
|
| 18 |
+
# 转发请求
|
| 19 |
+
response = requests.request(
|
| 20 |
+
method=method,
|
| 21 |
+
url=target_url,
|
| 22 |
+
headers={
|
| 23 |
+
'User-Agent': 'HuggingFace-Space-Proxy',
|
| 24 |
+
'Accept': '*/*'
|
| 25 |
+
}
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
# 返回响应
|
| 29 |
+
return Response(
|
| 30 |
+
content=response.content,
|
| 31 |
+
status_code=response.status_code,
|
| 32 |
+
headers={
|
| 33 |
+
'Content-Type': response.headers.get('Content-Type', 'application/json'),
|
| 34 |
+
'Access-Control-Allow-Origin': '*',
|
| 35 |
+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
| 36 |
+
'Access-Control-Allow-Headers': '*'
|
| 37 |
+
}
|
| 38 |
+
)
|
| 39 |
except Exception as e:
|
| 40 |
+
return JSONResponse(
|
| 41 |
+
content={"error": str(e)},
|
| 42 |
+
status_code=500
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
@app.get("/")
|
| 46 |
+
@app.post("/")
|
| 47 |
+
async def proxy_root(request: Request):
|
| 48 |
+
try:
|
| 49 |
+
# 直接访问 beibeioo.top
|
| 50 |
+
target_url = "http://beibeioo.top"
|
| 51 |
+
|
| 52 |
+
# 获取原始请求方法
|
| 53 |
+
method = request.method
|
| 54 |
+
|
| 55 |
+
# 转发请求
|
| 56 |
+
response = requests.request(
|
| 57 |
+
method=method,
|
| 58 |
+
url=target_url,
|
| 59 |
+
headers={
|
| 60 |
+
'User-Agent': 'HuggingFace-Space-Proxy',
|
| 61 |
+
'Accept': '*/*'
|
| 62 |
+
}
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
# 返回响应
|
| 66 |
+
return Response(
|
| 67 |
+
content=response.content,
|
| 68 |
+
status_code=response.status_code,
|
| 69 |
+
headers={
|
| 70 |
+
'Content-Type': response.headers.get('Content-Type', 'application/json'),
|
| 71 |
+
'Access-Control-Allow-Origin': '*',
|
| 72 |
+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
| 73 |
+
'Access-Control-Allow-Headers': '*'
|
| 74 |
+
}
|
| 75 |
+
)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return JSONResponse(
|
| 78 |
+
content={"error": str(e)},
|
| 79 |
+
status_code=500
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
# 挂载 FastAPI 到 Gradio
|
| 83 |
+
gr.mount_gradio_app(app, gr.Interface(
|
| 84 |
+
fn=lambda x: x,
|
| 85 |
+
inputs=gr.Textbox(visible=False),
|
| 86 |
+
outputs=gr.Textbox(visible=False),
|
| 87 |
+
examples=[],
|
| 88 |
+
title="API Proxy"
|
| 89 |
+
), path="/ui")
|