Spaces:
Paused
Paused
| # app/main.py - /tmp BILAN ISHLAYDIGAN TOZA KOD | |
| import os | |
| import asyncio | |
| from contextlib import asynccontextmanager | |
| from fastapi import FastAPI | |
| from fastapi.responses import FileResponse | |
| from fastapi.staticfiles import StaticFiles | |
| import shutil # <-- IMPORT QO'SHING | |
| import glob # <-- IMPORT QO'SHING | |
| # Loyihaning boshqa modullarini import qilish | |
| 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 | |
| from app.services.brigade_simulator import get_simulator | |
| from app.core.database import db | |
| # Diagnostika uchun barcha yoziladigan papkalarni /tmp jildiga joylashtiramiz | |
| BASE_WRITE_DIR = "/tmp" | |
| TEMP_AUDIO_DIR = os.path.join(BASE_WRITE_DIR, "audio") | |
| UPLOAD_DIR = os.path.join(BASE_WRITE_DIR, "uploads") | |
| DATA_DIR = os.path.join(BASE_WRITE_DIR, "data") | |
| # Haqiqiy (read-only) data papkasi | |
| ORIGINAL_DATA_DIR = "data" | |
| # Papkalarni yaratish | |
| print(f"DIAGNOSTIKA: Papkalar {BASE_WRITE_DIR} ichida yaratilmoqda...") | |
| os.makedirs(TEMP_AUDIO_DIR, exist_ok=True) | |
| os.makedirs(UPLOAD_DIR, exist_ok=True) | |
| os.makedirs(DATA_DIR, exist_ok=True) # /tmp/data yaratildi | |
| print("DIAGNOSTIKA: Papkalar muvaffaqiyatli yaratildi.") | |
| print(f"DIAGNOSTIKA: Asosiy ma'lumotlar {ORIGINAL_DATA_DIR} dan {DATA_DIR} ga ko'chirilmoqda...") | |
| try: | |
| # data/ papkasidagi barcha .json fayllarni topish | |
| json_files = glob.glob(os.path.join(ORIGINAL_DATA_DIR, "*.json")) | |
| if not json_files: | |
| print(f"DIAGNOSTIKA: OGOHLANTIRISH! {ORIGINAL_DATA_DIR} da .json fayllar topilmadi.") | |
| copied_count = 0 | |
| for file_path in json_files: | |
| file_name = os.path.basename(file_path) | |
| dest_path = os.path.join(DATA_DIR, file_name) | |
| # Agar /tmp da bu fayl mavjud bo'lmasa, uni ko'chiramiz | |
| if not os.path.exists(dest_path): | |
| shutil.copy(file_path, dest_path) | |
| copied_count += 1 | |
| print(f"DIAGNOSTIKA: {copied_count} ta fayl {DATA_DIR} ga ko'chirildi.") | |
| except Exception as e: | |
| print(f"DIAGNOSTIKA: XATOLIK! Ma'lumotlarni ko'chirishda xatolik: {e}") | |
| # Global o'zgaruvchi | |
| simulator_task = None | |
| async def lifespan(app: FastAPI): | |
| # Ilova ishga tushishi | |
| print("π Ilova ishga tushmoqda...") | |
| print("π¦ AI modellari yuklanmoqda...") | |
| load_models() | |
| print("β Barcha modellar tayyor!") | |
| 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'xtashi | |
| print("π Ilova to'xtatilmoqda...") | |
| 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) | |
| app.include_router(dispatcher_router) | |
| # Static fayllar | |
| app.mount("/audio", StaticFiles(directory=TEMP_AUDIO_DIR), name="audio") | |
| app.mount("/static", StaticFiles(directory="static"), name="static") | |
| # Frontend sahifalari | |
| async def read_root(): | |
| return FileResponse('static/bemor/index.html') | |
| async def dispatcher_panel(): | |
| return FileResponse('static/dispatcher/index.html') | |