Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from fastapi import FastAPI
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
|
| 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 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
-
return
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
fn=
|
| 26 |
-
inputs=gr.Textbox(
|
| 27 |
-
outputs=gr.Textbox(
|
| 28 |
-
title="API Proxy"
|
|
|
|
| 29 |
)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 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()
|