Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
import threading
|
| 5 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
+
import uvicorn
|
| 9 |
+
from typing import Dict
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
from fastapi.responses import FileResponse
|
| 13 |
+
|
| 14 |
+
# Import from cursor_tracker
|
| 15 |
+
from test_ai_integration_http import (
|
| 16 |
+
test_ai_integration_http
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# FastAPI App Definition
|
| 22 |
+
app = FastAPI()
|
| 23 |
+
|
| 24 |
+
# Add CORS middleware to allow cross-origin requests
|
| 25 |
+
app.add_middleware(
|
| 26 |
+
CORSMiddleware,
|
| 27 |
+
allow_origins=["*"], # Allows all origins
|
| 28 |
+
allow_credentials=True,
|
| 29 |
+
allow_methods=["*"], # Allows all methods
|
| 30 |
+
allow_headers=["*"],
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Global variable to track if processing is running
|
| 34 |
+
processing_thread = None
|
| 35 |
+
|
| 36 |
+
def log_message(message):
|
| 37 |
+
"""Add a log message with timestamp"""
|
| 38 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 39 |
+
log_entry = f"[{timestamp}] {message}"
|
| 40 |
+
processing_status["logs"].append(log_entry)
|
| 41 |
+
|
| 42 |
+
# Keep only the last 100 logs
|
| 43 |
+
if len(processing_status["logs"]) > 100:
|
| 44 |
+
processing_status["logs"] = processing_status["logs"][-100:]
|
| 45 |
+
|
| 46 |
+
print(log_entry)
|
| 47 |
+
|
| 48 |
+
@app.on_event("startup")
|
| 49 |
+
async def startup_event():
|
| 50 |
+
global processing_thread
|
| 51 |
+
if not (processing_thread and processing_thread.is_alive()):
|
| 52 |
+
processing_thread = threading.Thread(target=test_ai_integration)
|
| 53 |
+
processing_thread.daemon = True
|
| 54 |
+
processing_thread.start()
|
| 55 |
+
|
| 56 |
+
from fastapi.staticfiles import StaticFiles
|
| 57 |
+
|
| 58 |
+
# app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 59 |
+
|
| 60 |
+
# Serve your main HTML file
|
| 61 |
+
@app.get("/")
|
| 62 |
+
async def root():
|
| 63 |
+
return ()
|
| 64 |
+
|
| 65 |
+
from fastapi.encoders import jsonable_encoder
|
| 66 |
+
|
| 67 |
+
def get_disk_usage(path: str) -> Dict[str, float]:
|
| 68 |
+
"""Get disk usage statistics in GB"""
|
| 69 |
+
statvfs = os.statvfs(path)
|
| 70 |
+
total = statvfs.f_frsize * statvfs.f_blocks / (1024**3)
|
| 71 |
+
free = statvfs.f_frsize * statvfs.f_bavail / (1024**3)
|
| 72 |
+
used = total - free
|
| 73 |
+
return {"total": total, "free": free, "used": used}
|
| 74 |
+
|
| 75 |
+
class SafeJSONEncoder(json.JSONEncoder):
|
| 76 |
+
def default(self, obj):
|
| 77 |
+
try:
|
| 78 |
+
if isinstance(obj, float):
|
| 79 |
+
if obj != obj: # Check for NaN
|
| 80 |
+
return None
|
| 81 |
+
if obj == float('inf') or obj == float('-inf'):
|
| 82 |
+
return None
|
| 83 |
+
return super().default(obj)
|
| 84 |
+
except:
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
get_disk_usage()
|
| 91 |
+
uvicorn.run(
|
| 92 |
+
app,
|
| 93 |
+
host="0.0.0.0",
|
| 94 |
+
port=8000,
|
| 95 |
+
log_level="info",
|
| 96 |
+
reload=False # Set to False for production
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
|