Spaces:
Sleeping
Sleeping
File size: 3,008 Bytes
bd27e5d 664ec6f bd27e5d 41b4ba4 f8f9b79 bd27e5d 27853a1 f34580e bd27e5d f8f9b79 bd27e5d f8f9b79 bd27e5d f8f9b79 bd27e5d |
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 |
from fastapi import FastAPI, HTTPException, BackgroundTasks
from fastapi.responses import RedirectResponse
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
import os
# Import the FactoryPredictor class
from factory_predictor import FactoryPredictor
app = FastAPI()
# Serve static files
app.mount("/app/static", StaticFiles(directory="static"), name="static")
app.add_middleware(
CORSMiddleware,
allow_origins=["http://127.0.0.1:5173","http://localhost:5173","http://127.0.0.1:7860",
"http://localhost:7860","http://0.0.0.0:7860", "https://santanche-factory-ml.hf.space"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Create a global instance of FactoryPredictor
predictor = FactoryPredictor()
# Global variable to store training status
training_status = "Not started"
# Redirect root to /docs
@app.get("/")
def root():
return RedirectResponse(url="/editor/")
@app.get("/editor/")
def get_editor():
print(os.path.join("static", "editor", "index.html"))
return FileResponse(os.path.join("static", "editor", "index.html"))
def train_model():
global training_status
training_status = "In progress"
try:
predictor.train()
training_status = "Completed"
except Exception as e:
training_status = f"Failed: {str(e)}"
@app.post("/train")
async def train(background_tasks: BackgroundTasks):
background_tasks.add_task(train_model)
return {"message": "Training started in the background"}
@app.get("/training_status")
async def get_training_status():
return {"status": training_status}
@app.get("/predict")
async def predict(temperature: int, pressure: int):
if training_status != "Completed":
raise HTTPException(status_code=400, detail="Model not trained yet")
try:
predicted_diagnosis = predictor.predict(temperature, pressure)
return {"diagnosis": str(predicted_diagnosis)}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
@app.post("/inform_temperature")
async def inform_temperature(value: int):
if training_status != "Completed":
raise HTTPException(status_code=400, detail="Model not trained yet")
try:
predicted_diagnosis = predictor.inform_temperature(value)
return str(predicted_diagnosis)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
@app.post("/inform_pressure")
async def inform_pressure(value: int):
if training_status != "Completed":
raise HTTPException(status_code=400, detail="Model not trained yet")
try:
predicted_diagnosis = predictor.inform_pressure(value)
return str(predicted_diagnosis)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) from e
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
|