desinez commited on
Commit
12ba138
Β·
1 Parent(s): 0748bc1

updating main.py

Browse files
Files changed (1) hide show
  1. backend/main.py +13 -21
backend/main.py CHANGED
@@ -3,16 +3,14 @@ from fastapi.middleware.cors import CORSMiddleware
3
  from starlette.middleware.wsgi import WSGIMiddleware
4
  import pandas as pd
5
  import io
6
- import plotly.io as pio # For converting Plotly figures to JSON
7
  import uvicorn
8
 
9
- # 1. Update the import path to match your folder structure
10
- from backend.PvDeepSetNet.src.DeepSets import run_prediction # Your packaged model
11
- from frontend.app import app as dash_app # Import your Dash app instance
12
 
13
  app = FastAPI()
14
 
15
- # Optional but recommended: Add CORS middleware so frontend/backend communication is frictionless
16
  app.add_middleware(
17
  CORSMiddleware,
18
  allow_origins=["*"],
@@ -21,48 +19,42 @@ app.add_middleware(
21
  allow_headers=["*"],
22
  )
23
 
24
- # Temporary storage for demonstration
25
  storage = {}
26
 
27
- @app.post("/upload-data")
 
28
  async def upload_data(file: UploadFile = File(...)):
29
  contents = await file.read()
30
- dataset_id = "ds_001" # In a real app, use: str(uuid.uuid4())
31
  storage[dataset_id] = contents
32
  return {"dataset_id": dataset_id}
33
 
34
- @app.post("/predict")
 
35
  async def predict(data: dict):
36
  dataset_id = data.get("dataset_id")
37
 
38
- # 1. Retrieve data from storage
39
  file_bytes = storage.get(dataset_id)
40
  if not file_bytes:
41
  return {"error": "Dataset not found"}
42
 
43
  df = pd.read_csv(io.BytesIO(file_bytes))
44
 
45
- # 2. Run the real PvDeepSetNet model
46
  model_output = run_prediction(df)
47
 
48
  res_df = model_output['results_table']
49
  fig1 = model_output.get('donut_plot')
50
  fig2 = model_output.get('distribution_plot')
51
 
52
- # 3. Serialize the Plotly Figure to JSON
53
  plot_json1 = pio.to_json(fig1) if fig1 else None
54
  plot_json2 = pio.to_json(fig2) if fig2 else None
55
 
56
  return {
57
  "results_table": res_df.to_dict('records'),
58
- "plot_json1": plot_json1, # The serialized figure
59
- "plot_json2": plot_json2 # The serialized figure
60
  }
61
 
62
- # 2. Mount the Dash app's Flask server into FastAPI.
63
- # This makes Dash serve as your primary visual user interface.
64
- app.mount("/", WSGIMiddleware(dash_app.server))
65
-
66
- # if __name__ == "__main__":
67
- # # Locally, you can still run this file directly
68
- # uvicorn.run(app, host="127.0.0.1", port=8000)
 
3
  from starlette.middleware.wsgi import WSGIMiddleware
4
  import pandas as pd
5
  import io
6
+ import plotly.io as pio
7
  import uvicorn
8
 
9
+ from backend.PvDeepSetNet.src.DeepSets import run_prediction
10
+ from frontend.app import app as dash_app
 
11
 
12
  app = FastAPI()
13
 
 
14
  app.add_middleware(
15
  CORSMiddleware,
16
  allow_origins=["*"],
 
19
  allow_headers=["*"],
20
  )
21
 
 
22
  storage = {}
23
 
24
+ # ─── UPDATED TO /api/upload-data ──────────────────────────────────
25
+ @app.post("/api/upload-data")
26
  async def upload_data(file: UploadFile = File(...)):
27
  contents = await file.read()
28
+ dataset_id = "ds_001"
29
  storage[dataset_id] = contents
30
  return {"dataset_id": dataset_id}
31
 
32
+ # ─── UPDATED TO /api/predict ──────────────────────────────────────
33
+ @app.post("/api/predict")
34
  async def predict(data: dict):
35
  dataset_id = data.get("dataset_id")
36
 
 
37
  file_bytes = storage.get(dataset_id)
38
  if not file_bytes:
39
  return {"error": "Dataset not found"}
40
 
41
  df = pd.read_csv(io.BytesIO(file_bytes))
42
 
 
43
  model_output = run_prediction(df)
44
 
45
  res_df = model_output['results_table']
46
  fig1 = model_output.get('donut_plot')
47
  fig2 = model_output.get('distribution_plot')
48
 
 
49
  plot_json1 = pio.to_json(fig1) if fig1 else None
50
  plot_json2 = pio.to_json(fig2) if fig2 else None
51
 
52
  return {
53
  "results_table": res_df.to_dict('records'),
54
+ "plot_json1": plot_json1,
55
+ "plot_json2": plot_json2
56
  }
57
 
58
+ # Mount Dash to the root. Because our API paths now start with /api,
59
+ # FastAPI will handle them first and send everything else to Dash!
60
+ app.mount("/", WSGIMiddleware(dash_app.server))