PvRecNet / backend /main.py
desinez's picture
updating frontend and backend files
6a2365a
Raw History Blame Contribute Delete
1.78 kB
from fastapi import FastAPI, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.wsgi import WSGIMiddleware
import pandas as pd
import io
import plotly.io as pio
import uvicorn
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Shared memory dictionary accessible by both FastAPI endpoints and Dash callbacks
storage = {}
# ─── INLINE IMPORTS TO PREVENT PYTHONS CIRCULAR DEPENDENCY SNAGS ───
from backend.PvDeepSetNet.src.DeepSets import run_prediction
from frontend.app import app as dash_app
@app.post("/api/upload-data")
async def upload_data(file: UploadFile = File(...)):
contents = await file.read()
dataset_id = "ds_001"
storage[dataset_id] = contents
return {"dataset_id": dataset_id}
@app.post("/api/predict")
async def predict(data: dict):
dataset_id = data.get("dataset_id")
file_bytes = storage.get(dataset_id)
if not file_bytes:
return {"error": "Dataset not found"}
df = pd.read_csv(io.BytesIO(file_bytes))
model_output = run_prediction(df)
res_df = model_output['results_table']
fig1 = model_output.get('donut_plot')
fig2 = model_output.get('distribution_plot')
plot_json1 = pio.to_json(fig1) if fig1 else None
plot_json2 = pio.to_json(fig2) if fig2 else None
return {
"results_table": res_df.to_dict('records'),
"plot_json1": plot_json1,
"plot_json2": plot_json2
}
# Mount Dash to the root. Because our API paths now start with /api,
# FastAPI will handle them first and send everything else to Dash!
app.mount("/", WSGIMiddleware(dash_app.server))