Spaces:
Sleeping
Sleeping
vaka.pro commited on
Commit ·
e2f3a86
1
Parent(s): 6819847
refactor: wraps FastAPI in Gradio interface for Hugging Face compatibility
Browse files
app.py
CHANGED
|
@@ -1,46 +1,89 @@
|
|
| 1 |
import os
|
| 2 |
import requests
|
|
|
|
| 3 |
from fastapi import FastAPI, Request, HTTPException
|
| 4 |
from fastapi.responses import JSONResponse
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
PRIVATE_SPACE_URL = os.getenv("PRIVATE_SPACE_URL")
|
| 9 |
PUBLIC_ENDPOINT = os.getenv("PUBLIC_ENDPOINT")
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
if PUBLIC_ENDPOINT:
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
if not PRIVATE_SPACE_URL:
|
| 15 |
-
raise HTTPException(500, "Service misconfigured")
|
| 16 |
-
|
| 17 |
-
# ПРОСТО используем PRIVATE_SPACE_URL как есть
|
| 18 |
-
target = PRIVATE_SPACE_URL
|
| 19 |
-
|
| 20 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
response = requests.post(
|
| 22 |
-
|
| 23 |
-
data=await request.body(),
|
| 24 |
-
headers={"Content-Type": request.headers.get("content-type")},
|
| 25 |
-
timeout=30
|
| 26 |
)
|
|
|
|
|
|
|
| 27 |
return JSONResponse(
|
| 28 |
content=response.json() if response.content else {},
|
| 29 |
-
status_code=response.status_code
|
| 30 |
)
|
|
|
|
| 31 |
except requests.exceptions.Timeout:
|
| 32 |
-
raise HTTPException(504, "
|
| 33 |
except requests.exceptions.ConnectionError:
|
| 34 |
-
raise HTTPException(
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 40 |
return {
|
| 41 |
-
"status": "ok" if PRIVATE_SPACE_URL and PUBLIC_ENDPOINT else "error"
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
from fastapi import FastAPI, Request, HTTPException
|
| 5 |
from fastapi.responses import JSONResponse
|
| 6 |
+
import json
|
| 7 |
|
| 8 |
+
# Конфигурация
|
|
|
|
| 9 |
PRIVATE_SPACE_URL = os.getenv("PRIVATE_SPACE_URL")
|
| 10 |
PUBLIC_ENDPOINT = os.getenv("PUBLIC_ENDPOINT")
|
| 11 |
|
| 12 |
+
# Создаем FastAPI приложение
|
| 13 |
+
fastapi_app = FastAPI(title="Yandex Forms Proxy")
|
| 14 |
+
|
| 15 |
+
# FastAPI эндпоинт
|
| 16 |
if PUBLIC_ENDPOINT:
|
| 17 |
+
|
| 18 |
+
@fastapi_app.post(f"/{PUBLIC_ENDPOINT}")
|
| 19 |
+
async def proxy_webhook(request: Request):
|
| 20 |
if not PRIVATE_SPACE_URL:
|
| 21 |
+
raise HTTPException(status_code=500, detail="Service misconfigured")
|
| 22 |
+
|
|
|
|
|
|
|
|
|
|
| 23 |
try:
|
| 24 |
+
# Получаем данные запроса
|
| 25 |
+
body_bytes = await request.body()
|
| 26 |
+
|
| 27 |
+
# Подготавливаем заголовки
|
| 28 |
+
headers = {
|
| 29 |
+
"Content-Type": request.headers.get("content-type", "application/json"),
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Копируем заголовки
|
| 33 |
+
for header in ["X-FORM-ID", "X-DELIVERY-ID", "X-FORM-ANSWER-ID"]:
|
| 34 |
+
if value := request.headers.get(header):
|
| 35 |
+
headers[header] = value
|
| 36 |
+
|
| 37 |
+
# Отправляем в приватное пространство
|
| 38 |
response = requests.post(
|
| 39 |
+
PRIVATE_SPACE_URL, data=body_bytes, headers=headers, timeout=30
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
+
|
| 42 |
+
# Возвращаем ответ
|
| 43 |
return JSONResponse(
|
| 44 |
content=response.json() if response.content else {},
|
| 45 |
+
status_code=response.status_code,
|
| 46 |
)
|
| 47 |
+
|
| 48 |
except requests.exceptions.Timeout:
|
| 49 |
+
raise HTTPException(status_code=504, detail="Service timeout")
|
| 50 |
except requests.exceptions.ConnectionError:
|
| 51 |
+
raise HTTPException(
|
| 52 |
+
status_code=502, detail="Cannot connect to target service"
|
| 53 |
+
)
|
| 54 |
+
except Exception as e:
|
| 55 |
+
raise HTTPException(status_code=500, detail="Internal server error")
|
| 56 |
+
|
| 57 |
|
| 58 |
+
# Health check
|
| 59 |
+
@fastapi_app.get("/health")
|
| 60 |
+
async def health_check():
|
| 61 |
return {
|
| 62 |
+
"status": "ok" if PRIVATE_SPACE_URL and PUBLIC_ENDPOINT else "error",
|
| 63 |
+
"service": "yandex-forms-proxy",
|
| 64 |
+
"webhook_configured": bool(PUBLIC_ENDPOINT),
|
| 65 |
+
"target_configured": bool(PRIVATE_SPACE_URL),
|
| 66 |
}
|
| 67 |
|
| 68 |
+
|
| 69 |
+
# Корневой эндпоинт
|
| 70 |
+
@fastapi_app.get("/")
|
| 71 |
+
async def root():
|
| 72 |
+
return {
|
| 73 |
+
"service": "Yandex Forms Proxy",
|
| 74 |
+
"description": "Сервисный прокси для перенаправления запросов",
|
| 75 |
+
"status": "operational",
|
| 76 |
+
"health_check": "/health",
|
| 77 |
+
"docs": "/docs",
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# Создаем Gradio интерфейс
|
| 82 |
+
with gr.Blocks(title="Proxy Service") as demo:
|
| 83 |
+
gr.Markdown("# 🔒 Proxy Service")
|
| 84 |
+
gr.Markdown("## Сервисное приложение для системной интеграции")
|
| 85 |
+
|
| 86 |
+
gr.Markdown("### ⚠️ см. README")
|
| 87 |
+
|
| 88 |
+
# Монтируем FastAPI в Gradio
|
| 89 |
+
app = gr.mount_gradio_app(fastapi_app, demo, path="/")
|