File size: 1,777 Bytes
9aecc8a a1bffb9 66dd119 12ba138 9aecc8a a1bffb9 6a2365a 66dd119 6a2365a 12ba138 9aecc8a 66dd119 12ba138 66dd119 9aecc8a 12ba138 9aecc8a 66dd119 9aecc8a 66dd119 12ba138 9aecc8a 12ba138 | 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 | 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)) |