Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import time | |
| import threading | |
| from fastapi import FastAPI, HTTPException, BackgroundTasks | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse | |
| import uvicorn | |
| from typing import Dict | |
| from pathlib import Path | |
| from datetime import datetime | |
| from fastapi.responses import FileResponse | |
| # Import from cursor_tracker | |
| from test_ai_integration import ( | |
| test_ai_integration | |
| ) | |
| # FastAPI App Definition | |
| app = FastAPI() | |
| # Add CORS middleware to allow cross-origin requests | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Allows all origins | |
| allow_credentials=True, | |
| allow_methods=["*"], # Allows all methods | |
| allow_headers=["*"], | |
| ) | |
| # Global variable to track if processing is running | |
| processing_thread = None | |
| def log_message(message): | |
| """Add a log message with timestamp""" | |
| timestamp = datetime.now().strftime("%H:%M:%S") | |
| log_entry = f"[{timestamp}] {message}" | |
| processing_status["logs"].append(log_entry) | |
| # Keep only the last 100 logs | |
| if len(processing_status["logs"]) > 100: | |
| processing_status["logs"] = processing_status["logs"][-100:] | |
| print(log_entry) | |
| async def startup_event(): | |
| global processing_thread | |
| if not (processing_thread and processing_thread.is_alive()): | |
| processing_thread = threading.Thread(target=test_ai_integration) | |
| processing_thread.daemon = True | |
| processing_thread.start() | |
| from fastapi.staticfiles import StaticFiles | |
| # app.mount("/static", StaticFiles(directory="static"), name="static") | |
| # Serve your main HTML file | |
| async def root(): | |
| return () | |
| from fastapi.encoders import jsonable_encoder | |
| def get_disk_usage(path: str) -> Dict[str, float]: | |
| """Get disk usage statistics in GB""" | |
| statvfs = os.statvfs(path) | |
| total = statvfs.f_frsize * statvfs.f_blocks / (1024**3) | |
| free = statvfs.f_frsize * statvfs.f_bavail / (1024**3) | |
| used = total - free | |
| return {"total": total, "free": free, "used": used} | |
| class SafeJSONEncoder(json.JSONEncoder): | |
| def default(self, obj): | |
| try: | |
| if isinstance(obj, float): | |
| if obj != obj: # Check for NaN | |
| return None | |
| if obj == float('inf') or obj == float('-inf'): | |
| return None | |
| return super().default(obj) | |
| except: | |
| return None | |
| if __name__ == "__main__": | |
| get_disk_usage() | |
| uvicorn.run( | |
| app, | |
| host="0.0.0.0", | |
| port=8000, | |
| log_level="info", | |
| reload=False # Set to False for production | |
| ) | |