Files changed (1) hide show
  1. main.py +108 -92
main.py CHANGED
@@ -1,92 +1,108 @@
1
- # app/main.py - YANGILANGAN 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
- # Papkalarni yaratish
19
- TEMP_AUDIO_DIR = "static/audio"
20
- UPLOAD_DIR = "static/uploads"
21
- DATA_DIR = "data"
22
- BEMOR_FRONTEND = "static/bemor"
23
- DISPATCHER_FRONTEND = "static/dispatcher"
24
-
25
- os.makedirs(TEMP_AUDIO_DIR, exist_ok=True)
26
- os.makedirs(UPLOAD_DIR, exist_ok=True)
27
- os.makedirs(DATA_DIR, exist_ok=True)
28
- os.makedirs(BEMOR_FRONTEND, exist_ok=True)
29
- os.makedirs(DISPATCHER_FRONTEND, exist_ok=True)
30
-
31
- # πŸ†• Global simulator task
32
- simulator_task = None
33
-
34
- @asynccontextmanager
35
- async def lifespan(app: FastAPI):
36
- # Ilova ishga tushishidan oldin
37
- print("πŸš€ Ilova ishga tushmoqda...")
38
-
39
- print("πŸ“¦ AI modellari yuklanmoqda...")
40
- load_models()
41
- print("βœ… Barcha modellar tayyor!")
42
-
43
- # πŸ†• Brigade simulator ishga tushirish
44
- print("πŸš‘ Brigade simulator ishga tushmoqda...")
45
- global simulator_task
46
- simulator = get_simulator(db)
47
- simulator_task = asyncio.create_task(simulator.start())
48
- print("βœ… Brigade simulator tayyor!")
49
-
50
- yield
51
-
52
- # Ilova to'xtagandan keyin
53
- print("πŸ›‘ Ilova to'xtatilmoqda...")
54
-
55
- # πŸ†• Simulator to'xtatish
56
- if simulator_task:
57
- simulator.stop()
58
- simulator_task.cancel()
59
- try:
60
- await simulator_task
61
- except asyncio.CancelledError:
62
- pass
63
- print("βœ… Simulator to'xtatildi")
64
-
65
- print("πŸ‘‹ Ilova ishini yakunladi.")
66
-
67
- app = FastAPI(
68
- title="Help.me - Tez Tibbiy Yordam AI Tizimi",
69
- description="Bemorlardan ovozli xabarlar qabul qilib, AI tahlil qiluvchi va dispetcherlarga yuboruvchi tizim",
70
- version="1.0.0 (MVP)",
71
- lifespan=lifespan
72
- )
73
-
74
- # Routerlarni ulash
75
- app.include_router(bemor_router) # Bemor WebSocket va APIlar
76
- app.include_router(dispatcher_router) # Dispetcher APIlar
77
-
78
- # Static fayllar
79
- app.mount("/audio", StaticFiles(directory=TEMP_AUDIO_DIR), name="audio")
80
- app.mount("/static", StaticFiles(directory="static"), name="static")
81
-
82
- # Bemor interfeysi (Root URL)
83
- @app.get("/", include_in_schema=False)
84
- async def read_root():
85
- """Bemor uchun asosiy sahifa"""
86
- return FileResponse('static/bemor/index.html')
87
-
88
- # Dispetcher paneli
89
- @app.get("/dispatcher", include_in_schema=False)
90
- async def dispatcher_panel():
91
- """Dispetcher dashboard"""
92
- return FileResponse('static/dispatcher/index.html')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+