Spaces:
Paused
Paused
| # app/main.py - YANGILANGAN VERSIYA | |
| import os | |
| import asyncio | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.responses import FileResponse | |
| from fastapi.staticfiles import StaticFiles | |
| from app.api.routes import router as bemor_router | |
| from app.api.dispatcher_routes import router as dispatcher_router | |
| from app.services.models import load_models | |
| # π Brigade Simulator import | |
| from app.services.brigade_simulator import get_simulator | |
| from app.core.database import db | |
| # Papkalarni yaratish | |
| TEMP_AUDIO_DIR = "static/audio" | |
| UPLOAD_DIR = "static/uploads" | |
| DATA_DIR = "data" | |
| BEMOR_FRONTEND = "static/bemor" | |
| DISPATCHER_FRONTEND = "static/dispatcher" | |
| os.makedirs(TEMP_AUDIO_DIR, exist_ok=True) | |
| os.makedirs(UPLOAD_DIR, exist_ok=True) | |
| os.makedirs(DATA_DIR, exist_ok=True) | |
| os.makedirs(BEMOR_FRONTEND, exist_ok=True) | |
| os.makedirs(DISPATCHER_FRONTEND, exist_ok=True) | |
| # π Global simulator task | |
| simulator_task = None | |
| async def lifespan(app: FastAPI): | |
| # Ilova ishga tushishidan oldin | |
| print("π Ilova ishga tushmoqda...") | |
| print("π¦ AI modellari yuklanmoqda...") | |
| load_models() | |
| print("β Barcha modellar tayyor!") | |
| # π Brigade simulator ishga tushirish | |
| print("π Brigade simulator ishga tushmoqda...") | |
| global simulator_task | |
| simulator = get_simulator(db) | |
| simulator_task = asyncio.create_task(simulator.start()) | |
| print("β Brigade simulator tayyor!") | |
| yield | |
| # Ilova to'xtagandan keyin | |
| print("π Ilova to'xtatilmoqda...") | |
| # π Simulator to'xtatish | |
| if simulator_task: | |
| simulator.stop() | |
| simulator_task.cancel() | |
| try: | |
| await simulator_task | |
| except asyncio.CancelledError: | |
| pass | |
| print("β Simulator to'xtatildi") | |
| print("π Ilova ishini yakunladi.") | |
| app = FastAPI( | |
| title="Help.me - Tez Tibbiy Yordam AI Tizimi", | |
| description="Bemorlardan ovozli xabarlar qabul qilib, AI tahlil qiluvchi va dispetcherlarga yuboruvchi tizim", | |
| version="1.0.0 (MVP)", | |
| lifespan=lifespan | |
| ) | |
| # Routerlarni ulash | |
| app.include_router(bemor_router) # Bemor WebSocket va APIlar | |
| app.include_router(dispatcher_router) # Dispetcher APIlar | |
| # Static fayllar | |
| app.mount("/audio", StaticFiles(directory=TEMP_AUDIO_DIR), name="audio") | |
| app.mount("/static", StaticFiles(directory="static"), name="static") | |
| # Bemor interfeysi (Root URL) | |
| async def read_root(): | |
| """Bemor uchun asosiy sahifa""" | |
| return FileResponse('static/bemor/index.html') | |
| # Dispetcher paneli | |
| async def dispatcher_panel(): | |
| """Dispetcher dashboard""" | |
| return FileResponse('static/dispatcher/index.html') |