Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ import PyPDF2
|
|
| 12 |
import docx
|
| 13 |
import chardet
|
| 14 |
import json
|
|
|
|
| 15 |
|
| 16 |
# Initialize the Dash app
|
| 17 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
|
@@ -147,17 +148,17 @@ def update_output(list_of_contents, list_of_names, existing_files):
|
|
| 147 |
global uploaded_files
|
| 148 |
if list_of_contents is not None:
|
| 149 |
new_files = []
|
| 150 |
-
for
|
| 151 |
file_content = parse_file_content(content, name)
|
| 152 |
uploaded_files[name] = file_content
|
| 153 |
-
new_files.append(
|
| 154 |
-
|
|
|
|
| 155 |
html.Span(name)
|
| 156 |
]))
|
| 157 |
-
|
| 158 |
-
existing_files = []
|
| 159 |
return existing_files + new_files
|
| 160 |
-
return existing_files
|
| 161 |
|
| 162 |
@app.callback(
|
| 163 |
Output('file-list', 'children', allow_duplicate=True),
|
|
@@ -170,9 +171,12 @@ def remove_file(n_clicks, existing_files):
|
|
| 170 |
ctx = dash.callback_context
|
| 171 |
if not ctx.triggered:
|
| 172 |
raise PreventUpdate
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
| 174 |
uploaded_files.pop(removed_file, None)
|
| 175 |
-
return [file for file in existing_files if file['props']['children'][1]['
|
| 176 |
|
| 177 |
def generate_matrix_with_gpt(matrix_type, file_contents):
|
| 178 |
prompt = f"""Generate a {matrix_type} based on the following project artifacts:
|
|
@@ -212,6 +216,14 @@ Now, generate the {matrix_type}:
|
|
| 212 |
|
| 213 |
return pd.DataFrame(data, columns=headers)
|
| 214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
@app.callback(
|
| 216 |
Output('matrix-preview', 'children'),
|
| 217 |
Output('loading-output', 'children'),
|
|
@@ -231,10 +243,13 @@ def generate_matrix(n_clicks, uploaded_files_json):
|
|
| 231 |
if not uploaded_files_json:
|
| 232 |
return html.Div("Please upload project artifacts before generating a matrix."), ""
|
| 233 |
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
try:
|
| 237 |
-
current_matrix = generate_matrix_with_gpt(matrix_type, uploaded_files)
|
| 238 |
return dbc.Table.from_dataframe(current_matrix, striped=True, bordered=True, hover=True), f"{matrix_type} generated"
|
| 239 |
except Exception as e:
|
| 240 |
print(f"Error generating matrix: {str(e)}")
|
|
|
|
| 12 |
import docx
|
| 13 |
import chardet
|
| 14 |
import json
|
| 15 |
+
from dash.exceptions import PreventUpdate
|
| 16 |
|
| 17 |
# Initialize the Dash app
|
| 18 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
|
|
|
| 148 |
global uploaded_files
|
| 149 |
if list_of_contents is not None:
|
| 150 |
new_files = []
|
| 151 |
+
for content, name in zip(list_of_contents, list_of_names):
|
| 152 |
file_content = parse_file_content(content, name)
|
| 153 |
uploaded_files[name] = file_content
|
| 154 |
+
new_files.append(dbc.ListGroupItem([
|
| 155 |
+
dbc.Button("×", id={'type': 'remove-file', 'index': name},
|
| 156 |
+
color="danger", size="sm", className="me-2"),
|
| 157 |
html.Span(name)
|
| 158 |
]))
|
| 159 |
+
existing_files = existing_files or []
|
|
|
|
| 160 |
return existing_files + new_files
|
| 161 |
+
return existing_files or []
|
| 162 |
|
| 163 |
@app.callback(
|
| 164 |
Output('file-list', 'children', allow_duplicate=True),
|
|
|
|
| 171 |
ctx = dash.callback_context
|
| 172 |
if not ctx.triggered:
|
| 173 |
raise PreventUpdate
|
| 174 |
+
|
| 175 |
+
triggered_id = ctx.triggered[0]['prop_id']
|
| 176 |
+
removed_file = json.loads(triggered_id.split('.')[0])['index']
|
| 177 |
+
|
| 178 |
uploaded_files.pop(removed_file, None)
|
| 179 |
+
return [file for file in existing_files if file['props']['children'][1]['children'] != removed_file]
|
| 180 |
|
| 181 |
def generate_matrix_with_gpt(matrix_type, file_contents):
|
| 182 |
prompt = f"""Generate a {matrix_type} based on the following project artifacts:
|
|
|
|
| 216 |
|
| 217 |
return pd.DataFrame(data, columns=headers)
|
| 218 |
|
| 219 |
+
@app.callback(
|
| 220 |
+
Output('uploaded-files-state', 'children'),
|
| 221 |
+
Input('file-list', 'children')
|
| 222 |
+
)
|
| 223 |
+
def update_uploaded_files_state(file_list):
|
| 224 |
+
global uploaded_files
|
| 225 |
+
return json.dumps(list(uploaded_files.keys()))
|
| 226 |
+
|
| 227 |
@app.callback(
|
| 228 |
Output('matrix-preview', 'children'),
|
| 229 |
Output('loading-output', 'children'),
|
|
|
|
| 243 |
if not uploaded_files_json:
|
| 244 |
return html.Div("Please upload project artifacts before generating a matrix."), ""
|
| 245 |
|
| 246 |
+
uploaded_files_list = json.loads(uploaded_files_json)
|
| 247 |
+
|
| 248 |
+
if not uploaded_files_list:
|
| 249 |
+
return html.Div("Please upload project artifacts before generating a matrix."), ""
|
| 250 |
|
| 251 |
try:
|
| 252 |
+
current_matrix = generate_matrix_with_gpt(matrix_type, [uploaded_files[file] for file in uploaded_files_list])
|
| 253 |
return dbc.Table.from_dataframe(current_matrix, striped=True, bordered=True, hover=True), f"{matrix_type} generated"
|
| 254 |
except Exception as e:
|
| 255 |
print(f"Error generating matrix: {str(e)}")
|