desinez commited on
Commit
fa2f208
·
1 Parent(s): b1ed007

initializing PvRecNet

Browse files
.env ADDED
File without changes
__pycache__/app.cpython-312.pyc ADDED
Binary file (6.38 kB). View file
 
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import dash
2
+ from dash import dcc, html, Input, Output, State, dash_table
3
+ import dash_bootstrap_components as dbc
4
+ import plotly.express as px
5
+ import pandas as pd
6
+ import base64
7
+ from services.api import PvRecNetAPI
8
+
9
+ # Initialize App with a professional Bootstrap theme (CYBORG or FLATLY)
10
+ app = dash.Dash(__name__, external_stylesheets=[dbc.themes.FLATLY])
11
+
12
+ app.layout = dbc.Container([
13
+ dcc.Store(id='dataset-id-store'),
14
+
15
+ dbc.Row(dbc.Col(html.H1("PvRecNet", className="text-center my-4"))),
16
+
17
+ dbc.Tabs([
18
+ # TAB 1: UPLOAD & PREDICT
19
+ dbc.Tab(label="Prediction Workflow", children=[
20
+ dbc.Row([
21
+ dbc.Col([
22
+ dbc.Card([
23
+ dbc.CardHeader("Step 1: Upload Data"),
24
+ dbc.CardBody([
25
+ dcc.Upload(
26
+ id='upload-data',
27
+ children=html.Div(['Drag/Drop or ', html.A('Select CSV')]),
28
+ style={'width': '100%', 'height': '60px', 'lineHeight': '60px',
29
+ 'borderWidth': '1px', 'borderStyle': 'dashed', 'textAlign': 'center'}
30
+ ),
31
+ html.Div(id='upload-status', className="mt-2"),
32
+ dbc.Button("Run Prediction", id="predict-btn", color="primary",
33
+ disabled=True, className="mt-3 w-100")
34
+ ])
35
+ ])
36
+ ], md=4),
37
+ dbc.Col([
38
+ dbc.Card([
39
+ dbc.CardHeader("Step 2: Results"),
40
+ dbc.CardBody(dcc.Loading(id="prediction-output"))
41
+ ])
42
+ ], md=8)
43
+ ], className="mt-4")
44
+ ]),
45
+
46
+ # TAB 2: ANALYTICS
47
+ dbc.Tab(label="Analytics Tools", children=[
48
+ dbc.Row([
49
+ dbc.Col([
50
+ dbc.Button("Compute MOI", id="moi-btn", color="info", className="m-2"),
51
+ dbc.Button("Compute Allele Freq", id="freq-btn", color="secondary", className="m-2"),
52
+ html.Hr(),
53
+ dcc.Loading(id="analytics-output")
54
+ ], md=12)
55
+ ], className="mt-4")
56
+ ])
57
+ ])
58
+ ], fluid=True)
59
+
60
+ # --- Callbacks ---
61
+
62
+ @app.callback(
63
+ Output('dataset-id-store', 'data'),
64
+ Output('upload-status', 'children'),
65
+ Output('predict-btn', 'disabled'),
66
+ Input('upload-data', 'contents'),
67
+ State('upload-data', 'filename')
68
+ )
69
+ def handle_upload(contents, filename):
70
+ if not contents: return dash.no_update
71
+ decoded = base64.b64decode(contents.split(',')[1])
72
+ try:
73
+ res = PvRecNetAPI.upload_dataset(decoded, filename)
74
+ return res['dataset_id'], dbc.Alert(f"Ready: {filename}", color="success"), False
75
+ except Exception as e:
76
+ return None, dbc.Alert(f"Error: {e}", color="danger"), True
77
+
78
+ @app.callback(
79
+ Output('prediction-output', 'children'),
80
+ Input('predict-btn', 'n_clicks'),
81
+ State('dataset-id-store', 'data'),
82
+ prevent_initial_call=True
83
+ )
84
+ def run_predict(n, ds_id):
85
+ try:
86
+ res = PvRecNetAPI.run_prediction(ds_id)
87
+ fig = px.bar(x=list(res['probabilities'].keys()), y=list(res['probabilities'].values()), title="Probability")
88
+ return html.Div([
89
+ html.H3(f"Prediction: {res['prediction']}"),
90
+ dcc.Graph(figure=fig)
91
+ ])
92
+ except Exception as e: return dbc.Alert(f"Error: {e}", color="danger")
93
+
94
+ @app.callback(
95
+ Output('analytics-output', 'children'),
96
+ Input('moi-btn', 'n_clicks'),
97
+ Input('freq-btn', 'n_clicks'),
98
+ State('dataset-id-store', 'data'),
99
+ prevent_initial_call=True
100
+ )
101
+ def run_analytics(m_n, f_n, ds_id):
102
+ if not ds_id: return dbc.Alert("No dataset found.", color="warning")
103
+ trigger = dash.callback_context.triggered[0]['prop_id']
104
+ try:
105
+ if "moi-btn" in trigger:
106
+ res = PvRecNetAPI.get_moi(ds_id)
107
+ return dash_table.DataTable(res, page_size=10)
108
+ else:
109
+ res = PvRecNetAPI.get_allele_freq(ds_id)
110
+ return dcc.Graph(figure=px.pie(res, names='allele', values='frequency'))
111
+ except Exception as e: return f"Error: {e}"
112
+
113
+ if __name__ == '__main__':
114
+ app.run(debug=True)
assets/custom.css ADDED
File without changes
components/__init__.py ADDED
File without changes
components/layouts.py ADDED
File without changes
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ dash>=2.14.0
2
+ dash-bootstrap-components>=1.5.0
3
+ pandas>=2.0.0
4
+ requests>=2.31.0
5
+ plotly>=5.18.0
6
+ python-dotenv>=1.0.0
services/__init__.py ADDED
File without changes
services/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (137 Bytes). View file
 
services/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (141 Bytes). View file
 
services/__pycache__/api.cpython-310.pyc ADDED
Binary file (1.2 kB). View file
 
services/__pycache__/api.cpython-312.pyc ADDED
Binary file (1.95 kB). View file
 
services/api.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+
4
+ BASE_URL = "http://127.0.0.1:8000" # Update this to your FastAPI port
5
+
6
+ class PvRecNetAPI:
7
+ @staticmethod
8
+ def upload_dataset(file_bytes, filename):
9
+ files = {'file': (filename, file_bytes, 'text/csv')}
10
+ response = requests.post(f"{BASE_URL}/upload-data", files=files)
11
+ response.raise_for_status()
12
+ return response.json()
13
+
14
+ @staticmethod
15
+ def run_prediction(dataset_id):
16
+ response = requests.post(f"{BASE_URL}/predict", json={"dataset_id": dataset_id})
17
+ response.raise_for_status()
18
+ return response.json()
19
+
20
+ @staticmethod
21
+ def get_moi(dataset_id):
22
+ response = requests.post(f"{BASE_URL}/compute-moi", json={"dataset_id": dataset_id})
23
+ response.raise_for_status()
24
+ return response.json()
25
+
26
+ @staticmethod
27
+ def get_allele_freq(dataset_id):
28
+ response = requests.post(f"{BASE_URL}/allele-frequency", json={"dataset_id": dataset_id})
29
+ response.raise_for_status()
30
+ return response.json()