File size: 3,707 Bytes
85a64b2
92bc115
 
 
 
 
 
 
c082d51
 
92bc115
85a64b2
92bc115
 
 
 
 
 
85a64b2
1f9d309
 
 
 
 
92bc115
c082d51
 
 
 
85a64b2
92bc115
 
c082d51
85a64b2
92bc115
c082d51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85a64b2
92bc115
 
 
 
85a64b2
92bc115
 
 
 
 
 
 
 
 
 
 
 
 
85a64b2
92bc115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d0b0207
 
92bc115
 
 
 
 
85a64b2
92bc115
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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

@asynccontextmanager
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
@app.get("/", include_in_schema=False)
async def read_root():
    return FileResponse('static/bemor/index.html')

@app.get("/dispatcher", include_in_schema=False)
async def dispatcher_panel():
    return FileResponse('static/dispatcher/index.html')