File size: 7,766 Bytes
ab4e093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env python3
"""
Minimal version of the AI Knowledge Distillation Platform
For testing and debugging purposes
"""

import os
import sys
import logging
from datetime import datetime
from pathlib import Path

# Add src to path
sys.path.insert(0, str(Path(__file__).parent / "src"))

from fastapi import FastAPI, Request, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

# Setup basic logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialize FastAPI app
app = FastAPI(
    title="AI Knowledge Distillation Platform",
    description="Minimal version for testing",
    version="2.0.0-minimal"
)

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Create directories
for directory in ["static", "templates", "cache", "database", "logs"]:
    Path(directory).mkdir(exist_ok=True)

# Mount static files and templates
try:
    app.mount("/static", StaticFiles(directory="static"), name="static")
    templates = Jinja2Templates(directory="templates")
except Exception as e:
    logger.warning(f"Could not mount static files: {e}")
    templates = None

# Initialize components with error handling
memory_manager = None
token_manager = None
medical_dataset_manager = None

try:
    from src.core.memory_manager import AdvancedMemoryManager
    memory_manager = AdvancedMemoryManager(max_memory_gb=14.0)
    logger.info("βœ… Memory manager initialized")
except Exception as e:
    logger.warning(f"⚠️ Could not initialize memory manager: {e}")

try:
    from src.core.token_manager import TokenManager
    token_manager = TokenManager()
    logger.info("βœ… Token manager initialized")
except Exception as e:
    logger.warning(f"⚠️ Could not initialize token manager: {e}")

try:
    from src.medical.medical_datasets import MedicalDatasetManager
    if memory_manager:
        medical_dataset_manager = MedicalDatasetManager(memory_manager)
        logger.info("βœ… Medical dataset manager initialized")
except Exception as e:
    logger.warning(f"⚠️ Could not initialize medical dataset manager: {e}")

@app.get("/", response_class=HTMLResponse)
async def read_root():
    """Serve the main web interface"""
    if templates:
        try:
            return templates.TemplateResponse("index.html", {"request": {}})
        except Exception as e:
            logger.error(f"Template error: {e}")
            return HTMLResponse("<h1>AI Knowledge Distillation Platform</h1><p>Minimal version running</p>")
    else:
        return HTMLResponse("<h1>AI Knowledge Distillation Platform</h1><p>Minimal version running</p>")

@app.get("/health")
async def health_check():
    """Health check endpoint"""
    try:
        status = {
            "status": "healthy",
            "version": "2.0.0-minimal",
            "timestamp": datetime.now().isoformat(),
            "components": {
                "memory_manager": memory_manager is not None,
                "token_manager": token_manager is not None,
                "medical_datasets": medical_dataset_manager is not None,
                "templates": templates is not None
            }
        }
        
        if memory_manager:
            try:
                memory_info = memory_manager.get_memory_info()
                status["memory"] = {
                    "usage_percent": memory_info.get("process_memory_percent", 0),
                    "available_gb": memory_info.get("system_memory_available_gb", 0)
                }
            except Exception as e:
                status["memory"] = {"error": str(e)}
        
        return status
    except Exception as e:
        logger.error(f"Health check failed: {e}")
        return {
            "status": "unhealthy",
            "error": str(e),
            "timestamp": datetime.now().isoformat()
        }

@app.get("/tokens")
async def token_management_page(request: Request):
    """Token management page"""
    if templates:
        try:
            return templates.TemplateResponse("token-management.html", {"request": request})
        except Exception as e:
            logger.error(f"Template error: {e}")
            return HTMLResponse("<h1>Token Management</h1><p>Template not available</p>")
    else:
        return HTMLResponse("<h1>Token Management</h1><p>Templates not available</p>")

@app.get("/medical-datasets")
async def medical_datasets_page(request: Request):
    """Medical datasets page"""
    if templates:
        try:
            return templates.TemplateResponse("medical-datasets.html", {"request": request})
        except Exception as e:
            logger.error(f"Template error: {e}")
            return HTMLResponse("<h1>Medical Datasets</h1><p>Template not available</p>")
    else:
        return HTMLResponse("<h1>Medical Datasets</h1><p>Templates not available</p>")

@app.get("/api/tokens")
async def list_tokens():
    """List all saved tokens"""
    if token_manager:
        try:
            tokens = token_manager.list_tokens()
            return {"tokens": tokens}
        except Exception as e:
            logger.error(f"Error listing tokens: {e}")
            raise HTTPException(status_code=500, detail=str(e))
    else:
        return {"tokens": [], "error": "Token manager not available"}

@app.get("/api/medical-datasets")
async def list_medical_datasets():
    """List supported medical datasets"""
    if medical_dataset_manager:
        try:
            datasets = medical_dataset_manager.list_supported_datasets()
            return {"datasets": datasets}
        except Exception as e:
            logger.error(f"Error listing medical datasets: {e}")
            raise HTTPException(status_code=500, detail=str(e))
    else:
        return {"datasets": [], "error": "Medical dataset manager not available"}

@app.get("/api/system/memory")
async def get_memory_info():
    """Get current memory information"""
    if memory_manager:
        try:
            memory_info = memory_manager.get_memory_info()
            return memory_info
        except Exception as e:
            logger.error(f"Error getting memory info: {e}")
            raise HTTPException(status_code=500, detail=str(e))
    else:
        return {"error": "Memory manager not available"}

@app.get("/debug")
async def debug_info():
    """Debug information"""
    import psutil
    
    return {
        "python_version": sys.version,
        "platform": sys.platform,
        "memory_gb": psutil.virtual_memory().total / (1024**3),
        "cpu_cores": os.cpu_count(),
        "working_directory": str(Path.cwd()),
        "python_path": sys.path[:3],  # First 3 entries
        "environment_variables": {
            "OMP_NUM_THREADS": os.getenv("OMP_NUM_THREADS"),
            "MKL_NUM_THREADS": os.getenv("MKL_NUM_THREADS"),
            "HF_TOKEN": "***" if os.getenv("HF_TOKEN") else None
        },
        "components_status": {
            "memory_manager": memory_manager is not None,
            "token_manager": token_manager is not None,
            "medical_datasets": medical_dataset_manager is not None,
            "templates": templates is not None
        }
    }

if __name__ == "__main__":
    print("πŸš€ Starting AI Knowledge Distillation Platform (Minimal)")
    print("🌐 Access at: http://localhost:8000")
    print("πŸ” Debug info: http://localhost:8000/debug")
    print("πŸ’Š Health check: http://localhost:8000/health")
    
    uvicorn.run(
        app,
        host="0.0.0.0",
        port=8000,
        log_level="info"
    )