bobocup commited on
Commit
b1049e3
·
verified ·
1 Parent(s): 5a3c4df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -16
app.py CHANGED
@@ -1,29 +1,52 @@
1
  import gradio as gr
2
  import requests
 
 
3
 
4
- def proxy_request(path=""):
 
 
 
 
5
  try:
6
- if path.startswith("/api/"):
7
- # 如果路径以 /api/ 开头,去掉 /api/ 后转发
8
- actual_path = path[5:] # 跳过 "/api/"
9
- url = f"http://beibeioo.top/{actual_path}"
 
10
  else:
11
  # 否则直接访问根路径
12
- url = "http://beibeioo.top"
13
-
14
- response = requests.get(url)
15
- return response.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  except Exception as e:
17
- return f"Error: {str(e)}"
18
 
19
- # 创建 Gradio 接口
20
  demo = gr.Interface(
21
- fn=proxy_request,
22
- inputs=gr.Textbox(label="Path (leave empty for root, or use /api/v1 for API)"),
23
- outputs=gr.Textbox(),
24
  title="API Proxy",
25
  description="Access beibeioo.top through this proxy"
26
  )
27
 
28
- if __name__ == "__main__":
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")