Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
-
from fastapi import FastAPI, Request
|
| 4 |
-
from fastapi.responses import Response
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
@
|
| 9 |
-
|
| 10 |
-
async def proxy(full_path: str, request: Request):
|
| 11 |
try:
|
| 12 |
# 构建目标 URL
|
| 13 |
if full_path.startswith("api/"):
|
|
@@ -19,34 +18,26 @@ async def proxy(full_path: str, request: Request):
|
|
| 19 |
target_url = "http://beibeioo.top"
|
| 20 |
|
| 21 |
# 转发请求
|
| 22 |
-
response = requests.
|
| 23 |
-
|
| 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 |
-
#
|
| 44 |
demo = gr.Interface(
|
| 45 |
-
fn=lambda x:
|
| 46 |
-
inputs=
|
| 47 |
-
outputs=
|
| 48 |
title="API Proxy",
|
| 49 |
description="Access beibeioo.top through this proxy"
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
import requests
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# 创建 FastAPI 应用
|
| 6 |
+
fastapi_app = FastAPI()
|
| 7 |
|
| 8 |
+
@fastapi_app.get("/{full_path:path}")
|
| 9 |
+
async def proxy(full_path: str):
|
|
|
|
| 10 |
try:
|
| 11 |
# 构建目标 URL
|
| 12 |
if full_path.startswith("api/"):
|
|
|
|
| 18 |
target_url = "http://beibeioo.top"
|
| 19 |
|
| 20 |
# 转发请求
|
| 21 |
+
response = requests.get(target_url)
|
| 22 |
+
return response.json() # 假设返回的是 JSON 数据
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
return {"error": str(e)}
|
| 25 |
|
| 26 |
+
# 创建 Gradio 接口
|
| 27 |
demo = gr.Interface(
|
| 28 |
+
fn=lambda x: "API Proxy is running",
|
| 29 |
+
inputs=None,
|
| 30 |
+
outputs="text",
|
| 31 |
title="API Proxy",
|
| 32 |
description="Access beibeioo.top through this proxy"
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# 重要:使用 Gradio 的 app 属性
|
| 36 |
+
app = demo.app
|
| 37 |
+
|
| 38 |
+
# 将 FastAPI 路由添加到 Gradio 的 app 中
|
| 39 |
+
for route in fastapi_app.routes:
|
| 40 |
+
app.add_route(route.path, route.endpoint, methods=route.methods)
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
demo.launch()
|