AgentizedSolutions commited on
Commit
b5e73cc
·
verified ·
1 Parent(s): 4c4719f

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +9 -7
main.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  from fastapi import FastAPI, Request, Response, HTTPException
3
  import httpx
 
4
 
5
  app = FastAPI()
6
 
@@ -17,26 +18,27 @@ async def root():
17
  async def listen_webhook(request: Request):
18
  if not TARGET_SPACE_URL or not HF_TOKEN:
19
  raise HTTPException(status_code=500, detail="Bridge configuration missing.")
20
-
21
- # Забираємо JSON, який прислав поштовий сервіс
22
  payload = await request.json()
23
-
24
  # Формуємо заголовки для пробиття приватності твого головного Space
25
  headers = {
26
  "Authorization": f"Bearer {HF_TOKEN}",
27
  "Content-Type": "application/json"
28
  }
29
-
30
  # Перенаправляємо запит у твій приватний Space
31
  async with httpx.AsyncClient() as client:
32
  try:
33
  response = await client.post(TARGET_SPACE_URL, json=payload, headers=headers, timeout=10.0)
34
- # Повертаємо поштовому сервісу те, що відповів твій приватний агент (наприклад, 200 OK)
35
  return Response(content=response.content, status_code=response.status_code, media_type="application/json")
36
  except httpx.RequestError as exc:
37
  return {"status": "failed", "error": f"Could not forward webhook: {exc}"}
38
- if __name__ == "__main__":
39
-
 
40
  # HF автоматично передає порт, але якщо ні — ставимо hardcode 7860
41
  port = int(os.environ.get("PORT", 7860))
42
  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
 
 
18
  async def listen_webhook(request: Request):
19
  if not TARGET_SPACE_URL or not HF_TOKEN:
20
  raise HTTPException(status_code=500, detail="Bridge configuration missing.")
21
+
22
+ # Забираємо JSON, який прислав сервіс
23
  payload = await request.json()
24
+
25
  # Формуємо заголовки для пробиття приватності твого головного Space
26
  headers = {
27
  "Authorization": f"Bearer {HF_TOKEN}",
28
  "Content-Type": "application/json"
29
  }
30
+
31
  # Перенаправляємо запит у твій приватний Space
32
  async with httpx.AsyncClient() as client:
33
  try:
34
  response = await client.post(TARGET_SPACE_URL, json=payload, headers=headers, timeout=10.0)
35
+ # Повертаємо сервісу те, що відповів твий приватний агент (наприклад, 200 OK)
36
  return Response(content=response.content, status_code=response.status_code, media_type="application/json")
37
  except httpx.RequestError as exc:
38
  return {"status": "failed", "error": f"Could not forward webhook: {exc}"}
39
+
40
+ # <--- Виносимо блок запуску на самий край (нульовий відступ)
41
+ if __name__ == "__main__":
42
  # HF автоматично передає порт, але якщо ні — ставимо hardcode 7860
43
  port = int(os.environ.get("PORT", 7860))
44
  uvicorn.run(app, host="0.0.0.0", port=port)