Spaces:
Paused
Paused
Update main.py
#1
by
giyos1212
- opened
main.py
CHANGED
|
@@ -1,92 +1,108 @@
|
|
| 1 |
-
# app/main.py -
|
| 2 |
-
|
| 3 |
-
import os
|
| 4 |
-
import asyncio
|
| 5 |
-
from contextlib import asynccontextmanager
|
| 6 |
-
from fastapi import FastAPI
|
| 7 |
-
from fastapi.responses import FileResponse
|
| 8 |
-
from fastapi.staticfiles import StaticFiles
|
| 9 |
-
|
| 10 |
-
from app.api.routes import router as bemor_router
|
| 11 |
-
from app.api.dispatcher_routes import router as dispatcher_router
|
| 12 |
-
from app.services.models import load_models
|
| 13 |
-
|
| 14 |
-
# π Brigade Simulator import
|
| 15 |
-
from app.services.brigade_simulator import get_simulator
|
| 16 |
-
from app.core.database import db
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
os.
|
| 26 |
-
os.
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
os.makedirs(
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
#
|
| 44 |
-
print("
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
print("β
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# Bemor
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app/main.py - PERMISSION ERROR TUZATILGAN VERSIYA
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import asyncio
|
| 5 |
+
from contextlib import asynccontextmanager
|
| 6 |
+
from fastapi import FastAPI
|
| 7 |
+
from fastapi.responses import FileResponse
|
| 8 |
+
from fastapi.staticfiles import StaticFiles
|
| 9 |
+
|
| 10 |
+
from app.api.routes import router as bemor_router
|
| 11 |
+
from app.api.dispatcher_routes import router as dispatcher_router
|
| 12 |
+
from app.services.models import load_models
|
| 13 |
+
|
| 14 |
+
# π Brigade Simulator import
|
| 15 |
+
from app.services.brigade_simulator import get_simulator
|
| 16 |
+
from app.core.database import db
|
| 17 |
+
|
| 18 |
+
# --- YECHIM UCHUN O'ZGARTIRISH ---
|
| 19 |
+
# Barcha o'zgaruvchan (dinamik) ma'lumotlar uchun yagona papka yaratamiz.
|
| 20 |
+
# Bu papkaga yozish uchun ruxsat bo'ladi.
|
| 21 |
+
PERSISTENT_DIR = "persistent_storage"
|
| 22 |
+
|
| 23 |
+
# Yaratilishi kerak bo'lgan papkalar yo'lini yangi asosiy papkaga nisbatan belgilaymiz
|
| 24 |
+
TEMP_AUDIO_DIR = os.path.join(PERSISTENT_DIR, "audio")
|
| 25 |
+
UPLOAD_DIR = os.path.join(PERSISTENT_DIR, "uploads")
|
| 26 |
+
DATA_DIR = os.path.join(PERSISTENT_DIR, "data")
|
| 27 |
+
|
| 28 |
+
# Faqat dinamik papkalarni yaratamiz
|
| 29 |
+
os.makedirs(TEMP_AUDIO_DIR, exist_ok=True)
|
| 30 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
| 31 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 32 |
+
|
| 33 |
+
# ESLATMA: `static/bemor` va `static/dispatcher` uchun `os.makedirs` olib tashlandi.
|
| 34 |
+
# Chunki bu papkalardagi `index.html` kabi fayllar sizning loyihangizda
|
| 35 |
+
# oldindan mavjud bo'lishi kerak va ularni dastur yordamida yaratish shart emas.
|
| 36 |
+
# ------------------------------------
|
| 37 |
+
|
| 38 |
+
# π Global simulator task
|
| 39 |
+
simulator_task = None
|
| 40 |
+
|
| 41 |
+
@asynccontextmanager
|
| 42 |
+
async def lifespan(app: FastAPI):
|
| 43 |
+
# Ilova ishga tushishidan oldin
|
| 44 |
+
print("π Ilova ishga tushmoqda...")
|
| 45 |
+
|
| 46 |
+
print("π¦ AI modellari yuklanmoqda...")
|
| 47 |
+
load_models()
|
| 48 |
+
print("β
Barcha modellar tayyor!")
|
| 49 |
+
|
| 50 |
+
# π Brigade simulator ishga tushirish
|
| 51 |
+
print("π Brigade simulator ishga tushmoqda...")
|
| 52 |
+
global simulator_task
|
| 53 |
+
simulator = get_simulator(db)
|
| 54 |
+
simulator_task = asyncio.create_task(simulator.start())
|
| 55 |
+
print("β
Brigade simulator tayyor!")
|
| 56 |
+
|
| 57 |
+
yield
|
| 58 |
+
|
| 59 |
+
# Ilova to'xtagandan keyin
|
| 60 |
+
print("π Ilova to'xtatilmoqda...")
|
| 61 |
+
|
| 62 |
+
# π Simulator to'xtatish
|
| 63 |
+
if simulator_task:
|
| 64 |
+
simulator.stop()
|
| 65 |
+
simulator_task.cancel()
|
| 66 |
+
try:
|
| 67 |
+
await simulator_task
|
| 68 |
+
except asyncio.CancelledError:
|
| 69 |
+
pass
|
| 70 |
+
print("β
Simulator to'xtatildi")
|
| 71 |
+
|
| 72 |
+
print("π Ilova ishini yakunladi.")
|
| 73 |
+
|
| 74 |
+
app = FastAPI(
|
| 75 |
+
title="Help.me - Tez Tibbiy Yordam AI Tizimi",
|
| 76 |
+
description="Bemorlardan ovozli xabarlar qabul qilib, AI tahlil qiluvchi va dispetcherlarga yuboruvchi tizim",
|
| 77 |
+
version="1.0.0 (MVP)",
|
| 78 |
+
lifespan=lifespan
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Routerlarni ulash
|
| 82 |
+
app.include_router(bemor_router) # Bemor WebSocket va APIlar
|
| 83 |
+
app.include_router(dispatcher_router) # Dispetcher APIlar
|
| 84 |
+
|
| 85 |
+
# Static fayllar
|
| 86 |
+
# --- YECHIM UCHUN O'ZGARTIRISH ---
|
| 87 |
+
# /audio endi yangi, yozish mumkin bo'lgan `persistent_storage/audio` papkasidan olinadi
|
| 88 |
+
app.mount("/audio", StaticFiles(directory=TEMP_AUDIO_DIR), name="audio")
|
| 89 |
+
|
| 90 |
+
# Bu qator o'zgarmaydi. U `static` papkasidagi (bemor/index.html kabi)
|
| 91 |
+
# o'zgarmas fayllarni topish uchun kerak.
|
| 92 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 93 |
+
# ------------------------------------
|
| 94 |
+
|
| 95 |
+
# Bemor interfeysi (Root URL)
|
| 96 |
+
@app.get("/", include_in_schema=False)
|
| 97 |
+
async def read_root():
|
| 98 |
+
"""Bemor uchun asosiy sahifa"""
|
| 99 |
+
# Bu yo'l to'g'ri, chunki u `app.mount("/static", ...)` ga nisbatan ishlaydi
|
| 100 |
+
return FileResponse('static/bemor/index.html')
|
| 101 |
+
|
| 102 |
+
# Dispetcher paneli
|
| 103 |
+
@app.get("/dispatcher", include_in_schema=False)
|
| 104 |
+
async def dispatcher_panel():
|
| 105 |
+
"""Dispetcher dashboard"""
|
| 106 |
+
# Bu yo'l ham to'g'ri
|
| 107 |
+
return FileResponse('static/dispatcher/index.html')
|
| 108 |
+
|