desinez commited on
Commit
a1bffb9
·
1 Parent(s): 7f62ff3

updating main.py

Browse files
Files changed (1) hide show
  1. backend/main.py +24 -9
backend/main.py CHANGED
@@ -1,19 +1,33 @@
1
  from fastapi import FastAPI, UploadFile, File
 
 
2
  import pandas as pd
3
  import io
4
  import plotly.io as pio # For converting Plotly figures to JSON
5
- from PvDeepSetNet.src.DeepSets import run_prediction # Your packaged model
6
  import uvicorn
7
 
 
 
 
 
8
  app = FastAPI()
9
 
 
 
 
 
 
 
 
 
 
10
  # Temporary storage for demonstration
11
  storage = {}
12
 
13
  @app.post("/upload-data")
14
  async def upload_data(file: UploadFile = File(...)):
15
  contents = await file.read()
16
- dataset_id = "ds_001" # In a real app, use: str(uuid.uuid4())
17
  storage[dataset_id] = contents
18
  return {"dataset_id": dataset_id}
19
 
@@ -29,25 +43,26 @@ async def predict(data: dict):
29
  df = pd.read_csv(io.BytesIO(file_bytes))
30
 
31
  # 2. Run the real PvDeepSetNet model
32
- # Note: run_prediction now returns 3 items: table, text, and a plotly figure
33
  model_output = run_prediction(df)
34
 
35
  res_df = model_output['results_table']
36
- # The figure object generated inside your model processing file
37
  fig1 = model_output.get('donut_plot')
38
  fig2 = model_output.get('distribution_plot')
39
 
40
  # 3. Serialize the Plotly Figure to JSON
41
- # This allows the plot to be sent over the internet as a string
42
  plot_json1 = pio.to_json(fig1) if fig1 else None
43
  plot_json2 = pio.to_json(fig2) if fig2 else None
44
-
45
 
46
  return {
47
  "results_table": res_df.to_dict('records'),
48
  "plot_json1": plot_json1, # The serialized figure
49
- "plot_json2": plot_json2 # The serialized figure
50
  }
51
 
52
- if __name__ == "__main__":
53
- uvicorn.run(app, host="127.0.0.1", port=8000)
 
 
 
 
 
 
1
  from fastapi import FastAPI, UploadFile, File
2
+ 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=["*"],
19
+ allow_credentials=True,
20
+ allow_methods=["*"],
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
 
 
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)