Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, Request, Response, HTTPException
|
| 3 |
import httpx
|
| 4 |
-
import uvicorn
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
@@ -11,35 +11,37 @@ TARGET_SPACE_URL = os.getenv("TARGET_SPACE_URL")
|
|
| 11 |
|
| 12 |
@app.get("/")
|
| 13 |
async def root():
|
| 14 |
-
# Фейкова сторінка-заглушка
|
| 15 |
return {"status": "error", "message": "404 Not Found - Service Under Construction"}
|
| 16 |
|
|
|
|
| 17 |
@app.post("/listener")
|
| 18 |
@app.post("/listener/")
|
| 19 |
async def listen_webhook(request: Request):
|
| 20 |
if not TARGET_SPACE_URL or not HF_TOKEN:
|
| 21 |
raise HTTPException(status_code=500, detail="Bridge configuration missing.")
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
headers = {
|
| 28 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 29 |
"Content-Type": "application/json"
|
| 30 |
}
|
| 31 |
-
|
| 32 |
# Перенаправляємо запит у твій приватний Space
|
| 33 |
async with httpx.AsyncClient() as client:
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
return Response(content=response.content, status_code=response.status_code, media_type="application/json")
|
| 38 |
except httpx.RequestError as exc:
|
| 39 |
return {"status": "failed", "error": f"Could not forward webhook: {exc}"}
|
| 40 |
|
| 41 |
-
# <--- Виносимо блок запуску на самий край (нульовий відступ)
|
| 42 |
if __name__ == "__main__":
|
| 43 |
-
# HF автоматично передає порт, але якщо ні — ставимо hardcode 7860
|
| 44 |
port = int(os.environ.get("PORT", 7860))
|
| 45 |
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
| 1 |
import os
|
| 2 |
from fastapi import FastAPI, Request, Response, HTTPException
|
| 3 |
import httpx
|
| 4 |
+
import uvicorn
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
| 11 |
|
| 12 |
@app.get("/")
|
| 13 |
async def root():
|
| 14 |
+
# Фейкова сторінка-заглушка
|
| 15 |
return {"status": "error", "message": "404 Not Found - Service Under Construction"}
|
| 16 |
|
| 17 |
+
# Прописуємо обидва роути окремо, щоб уникнути проблем із втратою payload при редіректах
|
| 18 |
@app.post("/listener")
|
| 19 |
@app.post("/listener/")
|
| 20 |
async def listen_webhook(request: Request):
|
| 21 |
if not TARGET_SPACE_URL or not HF_TOKEN:
|
| 22 |
raise HTTPException(status_code=500, detail="Bridge configuration missing.")
|
| 23 |
+
|
| 24 |
+
# ТЕПЕР КОД СТОЇТЬ НА СВОЄМУ МІСЦІ (на рівні з if)
|
| 25 |
+
try:
|
| 26 |
+
payload = await request.json()
|
| 27 |
+
except Exception:
|
| 28 |
+
raise HTTPException(status_code=400, detail="Invalid JSON payload")
|
| 29 |
+
|
| 30 |
+
# Формуємо заголовки для приватної Ллами
|
| 31 |
headers = {
|
| 32 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 33 |
"Content-Type": "application/json"
|
| 34 |
}
|
| 35 |
+
|
| 36 |
# Перенаправляємо запит у твій приватний Space
|
| 37 |
async with httpx.AsyncClient() as client:
|
| 38 |
try:
|
| 39 |
+
# Збільшив timeout до 30.0 секунд, бо Ллама генерує відповідь довго
|
| 40 |
+
response = await client.post(TARGET_SPACE_URL, json=payload, headers=headers, timeout=30.0)
|
| 41 |
return Response(content=response.content, status_code=response.status_code, media_type="application/json")
|
| 42 |
except httpx.RequestError as exc:
|
| 43 |
return {"status": "failed", "error": f"Could not forward webhook: {exc}"}
|
| 44 |
|
|
|
|
| 45 |
if __name__ == "__main__":
|
|
|
|
| 46 |
port = int(os.environ.get("PORT", 7860))
|
| 47 |
uvicorn.run(app, host="0.0.0.0", port=port)
|