bobocup commited on
Commit
3b27764
·
verified ·
1 Parent(s): b9e7409

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -24
app.py CHANGED
@@ -1,34 +1,89 @@
1
  import gradio as gr
 
 
2
  import requests
3
 
4
- def proxy_request(path=""):
 
 
 
 
5
  try:
6
- # 构建目标 URL
7
  target_url = f"http://beibeioo.top/{path}"
8
 
9
- # 发送请求
10
- response = requests.get(target_url)
 
 
 
 
 
 
 
 
 
 
11
 
12
- # 返回响应内容
13
- return response.json()
 
 
 
 
 
 
 
 
 
14
  except Exception as e:
15
- return {"error": str(e)}
 
 
 
16
 
17
- # 创建 Gradio 接口
18
- app = gr.Interface(
19
- fn=proxy_request,
20
- inputs=gr.Textbox(label="Path"),
21
- outputs=gr.JSON(),
22
- title="API Proxy"
23
- )
24
-
25
- # 添加自定义路由处理
26
- @app.queue()
27
- def proxy_all(request: gr.Request):
28
- path = request.path
29
- if path.startswith("/"):
30
- path = path[1:]
31
- return proxy_request(path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- if __name__ == "__main__":
34
- app.launch()
 
 
 
 
 
 
 
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")