updating frontend and backend files
Browse files- backend/main.py +5 -5
- frontend/app.py +44 -18
backend/main.py
CHANGED
|
@@ -6,9 +6,6 @@ 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(
|
|
@@ -19,9 +16,13 @@ app.add_middleware(
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
|
|
|
| 22 |
storage = {}
|
| 23 |
|
| 24 |
-
# ───
|
|
|
|
|
|
|
|
|
|
| 25 |
@app.post("/api/upload-data")
|
| 26 |
async def upload_data(file: UploadFile = File(...)):
|
| 27 |
contents = await file.read()
|
|
@@ -29,7 +30,6 @@ async def upload_data(file: UploadFile = File(...)):
|
|
| 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")
|
|
|
|
| 6 |
import plotly.io as pio
|
| 7 |
import uvicorn
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
app.add_middleware(
|
|
|
|
| 16 |
allow_headers=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
+
# Shared memory dictionary accessible by both FastAPI endpoints and Dash callbacks
|
| 20 |
storage = {}
|
| 21 |
|
| 22 |
+
# ─── INLINE IMPORTS TO PREVENT PYTHONS CIRCULAR DEPENDENCY SNAGS ───
|
| 23 |
+
from backend.PvDeepSetNet.src.DeepSets import run_prediction
|
| 24 |
+
from frontend.app import app as dash_app
|
| 25 |
+
|
| 26 |
@app.post("/api/upload-data")
|
| 27 |
async def upload_data(file: UploadFile = File(...)):
|
| 28 |
contents = await file.read()
|
|
|
|
| 30 |
storage[dataset_id] = contents
|
| 31 |
return {"dataset_id": dataset_id}
|
| 32 |
|
|
|
|
| 33 |
@app.post("/api/predict")
|
| 34 |
async def predict(data: dict):
|
| 35 |
dataset_id = data.get("dataset_id")
|
frontend/app.py
CHANGED
|
@@ -5,10 +5,12 @@ import pandas as pd
|
|
| 5 |
import json
|
| 6 |
import base64
|
| 7 |
import io
|
| 8 |
-
import os
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
from
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# 1. Initialize App with Hugging Face proxy configurations
|
| 14 |
app = dash.Dash(
|
|
@@ -183,10 +185,20 @@ def render_main_app():
|
|
| 183 |
prevent_initial_call=True,
|
| 184 |
)
|
| 185 |
def download_sample_data(n_clicks):
|
| 186 |
-
|
| 187 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
sample_path = os.path.join(base_dir, 'backend', 'PvDeepSetNet', 'datasets', 'GUI_sample_dataset.csv')
|
| 189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
sample_df = pd.read_csv(sample_path)
|
| 191 |
return dcc.send_data_frame(sample_df.to_csv, "pvrecnet_sample_data.csv", index=False)
|
| 192 |
|
|
@@ -223,10 +235,14 @@ def handle_upload(contents, filename):
|
|
| 223 |
decoded = base64.b64decode(contents.split(',')[1])
|
| 224 |
df = pd.read_csv(io.BytesIO(decoded))
|
| 225 |
cols = [{"name": i, "id": i, "editable": True, "type": 'numeric' if pd.api.types.is_numeric_dtype(df[i]) else 'text'} for i in df.columns]
|
| 226 |
-
|
| 227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
except Exception as e:
|
| 229 |
-
return None, dbc.Alert(f"Error: {str(e)}", color="danger"), True, [], [], {'headers': False}
|
| 230 |
|
| 231 |
@app.callback(
|
| 232 |
Output('prediction-output', 'children'),
|
|
@@ -236,20 +252,29 @@ def handle_upload(contents, filename):
|
|
| 236 |
)
|
| 237 |
def run_predict(n_clicks, dataset_id):
|
| 238 |
try:
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
-
|
| 244 |
prob_cols = ['C_marg', 'L_marg', 'I_marg']
|
| 245 |
|
| 246 |
for col in prob_cols:
|
| 247 |
-
if col in
|
| 248 |
-
|
| 249 |
|
| 250 |
table = dash_table.DataTable(
|
| 251 |
-
data=
|
| 252 |
-
columns=[{"name": i, "id": i} for i in
|
| 253 |
export_format='csv',
|
| 254 |
page_size=10,
|
| 255 |
style_table={'height': '400px', 'overflowY': 'auto'},
|
|
@@ -258,12 +283,13 @@ def run_predict(n_clicks, dataset_id):
|
|
| 258 |
|
| 259 |
return html.Div([
|
| 260 |
dbc.Row([
|
| 261 |
-
dbc.Col([dcc.Graph(figure=fig1)
|
|
|
|
| 262 |
dbc.Col(table, md=7)
|
| 263 |
])
|
| 264 |
])
|
| 265 |
except Exception as e:
|
| 266 |
-
return dbc.Alert(f"Error: {str(e)}", color="danger")
|
| 267 |
|
| 268 |
if __name__ == '__main__':
|
| 269 |
# Local fallback runner
|
|
|
|
| 5 |
import json
|
| 6 |
import base64
|
| 7 |
import io
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
# --- IN-MEMORY BACKEND HOOKS ---
|
| 11 |
+
# Importing directly from backend skips the internal network interface barriers
|
| 12 |
+
from backend.main import storage
|
| 13 |
+
from backend.PvDeepSetNet.src.DeepSets import run_prediction
|
| 14 |
|
| 15 |
# 1. Initialize App with Hugging Face proxy configurations
|
| 16 |
app = dash.Dash(
|
|
|
|
| 185 |
prevent_initial_call=True,
|
| 186 |
)
|
| 187 |
def download_sample_data(n_clicks):
|
| 188 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 189 |
+
|
| 190 |
+
# Adapt structural finding context for either local execution or Docker volumes
|
| 191 |
+
if os.path.basename(current_dir) == 'frontend':
|
| 192 |
+
base_dir = os.path.dirname(current_dir)
|
| 193 |
+
else:
|
| 194 |
+
base_dir = current_dir
|
| 195 |
+
|
| 196 |
sample_path = os.path.join(base_dir, 'backend', 'PvDeepSetNet', 'datasets', 'GUI_sample_dataset.csv')
|
| 197 |
|
| 198 |
+
# Fallback to internal module package tracking if paths nested differently
|
| 199 |
+
if not os.path.exists(sample_path):
|
| 200 |
+
sample_path = os.path.join(base_dir, 'backend', 'PvDeepSetNet', 'src', 'DeepSets', 'datasets', 'GUI_sample_dataset.csv')
|
| 201 |
+
|
| 202 |
sample_df = pd.read_csv(sample_path)
|
| 203 |
return dcc.send_data_frame(sample_df.to_csv, "pvrecnet_sample_data.csv", index=False)
|
| 204 |
|
|
|
|
| 235 |
decoded = base64.b64decode(contents.split(',')[1])
|
| 236 |
df = pd.read_csv(io.BytesIO(decoded))
|
| 237 |
cols = [{"name": i, "id": i, "editable": True, "type": 'numeric' if pd.api.types.is_numeric_dtype(df[i]) else 'text'} for i in df.columns]
|
| 238 |
+
|
| 239 |
+
# Save file contents straight into the global shared storage dictionary
|
| 240 |
+
dataset_id = "ds_001"
|
| 241 |
+
storage[dataset_id] = decoded
|
| 242 |
+
|
| 243 |
+
return dataset_id, dbc.Alert(f"Ready: {filename}", color="success"), False, df.to_dict('records'), cols, {'headers': True}
|
| 244 |
except Exception as e:
|
| 245 |
+
return None, dbc.Alert(f"Error parsing file: {str(e)}", color="danger"), True, [], [], {'headers': False}
|
| 246 |
|
| 247 |
@app.callback(
|
| 248 |
Output('prediction-output', 'children'),
|
|
|
|
| 252 |
)
|
| 253 |
def run_predict(n_clicks, dataset_id):
|
| 254 |
try:
|
| 255 |
+
# Retrieve the dataset straight from memory
|
| 256 |
+
file_bytes = storage.get(dataset_id)
|
| 257 |
+
if not file_bytes:
|
| 258 |
+
return dbc.Alert("Error: Dataset context could not be located in application memory storage.", color="danger")
|
| 259 |
+
|
| 260 |
+
df = pd.read_csv(io.BytesIO(file_bytes))
|
| 261 |
+
|
| 262 |
+
# Run the imported machine learning predictor natively
|
| 263 |
+
model_output = run_prediction(df)
|
| 264 |
+
|
| 265 |
+
fig1 = model_output.get('donut_plot')
|
| 266 |
+
fig2 = model_output.get('distribution_plot')
|
| 267 |
|
| 268 |
+
df_res = pd.DataFrame(model_output['results_table'])
|
| 269 |
prob_cols = ['C_marg', 'L_marg', 'I_marg']
|
| 270 |
|
| 271 |
for col in prob_cols:
|
| 272 |
+
if col in df_res.columns:
|
| 273 |
+
df_res[col] = df_res[col].map(lambda x: f"{x:.4f}" if pd.notnull(x) else x)
|
| 274 |
|
| 275 |
table = dash_table.DataTable(
|
| 276 |
+
data=df_res.to_dict('records'),
|
| 277 |
+
columns=[{"name": i, "id": i} for i in df_res.columns],
|
| 278 |
export_format='csv',
|
| 279 |
page_size=10,
|
| 280 |
style_table={'height': '400px', 'overflowY': 'auto'},
|
|
|
|
| 283 |
|
| 284 |
return html.Div([
|
| 285 |
dbc.Row([
|
| 286 |
+
dbc.Col([dcc.Graph(figure=fig1) if fig1 else html.Div(),
|
| 287 |
+
dcc.Graph(figure=fig2) if fig2 else html.Div()], md=5),
|
| 288 |
dbc.Col(table, md=7)
|
| 289 |
])
|
| 290 |
])
|
| 291 |
except Exception as e:
|
| 292 |
+
return dbc.Alert(f"Prediction Pipeline Error: {str(e)}", color="danger")
|
| 293 |
|
| 294 |
if __name__ == '__main__':
|
| 295 |
# Local fallback runner
|