Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
-
from fastapi.responses import JSONResponse, Response
|
| 4 |
import requests
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
@app.get("/
|
| 9 |
-
|
| 10 |
-
async def proxy_api(path: str, request: Request):
|
| 11 |
try:
|
| 12 |
-
|
| 13 |
-
|
| 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
|
| 41 |
-
content={"error": str(e)},
|
| 42 |
-
status_code=500
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
@app.get("/")
|
| 46 |
-
|
| 47 |
-
async def proxy_root(request: Request):
|
| 48 |
try:
|
| 49 |
-
|
| 50 |
-
|
| 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
|
| 78 |
-
content={"error": str(e)},
|
| 79 |
-
status_code=500
|
| 80 |
-
)
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
fn=lambda x: x,
|
| 85 |
inputs=gr.Textbox(visible=False),
|
| 86 |
outputs=gr.Textbox(visible=False),
|
| 87 |
-
examples=[],
|
| 88 |
title="API Proxy"
|
| 89 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from fastapi import FastAPI
|
|
|
|
| 3 |
import requests
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
@app.get("/")
|
| 8 |
+
def root():
|
|
|
|
| 9 |
try:
|
| 10 |
+
response = requests.get("http://beibeioo.top")
|
| 11 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
@app.get("/api/{path:path}")
|
| 16 |
+
def api_proxy(path: str):
|
|
|
|
| 17 |
try:
|
| 18 |
+
response = requests.get(f"http://beibeioo.top/{path}")
|
| 19 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
+
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# 创建一个简单的 Gradio 界面
|
| 24 |
+
interface = gr.Interface(
|
| 25 |
fn=lambda x: x,
|
| 26 |
inputs=gr.Textbox(visible=False),
|
| 27 |
outputs=gr.Textbox(visible=False),
|
|
|
|
| 28 |
title="API Proxy"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# 挂载应用
|
| 32 |
+
app = gr.mount_gradio_app(app, interface, path="/ui")
|