bluenevus commited on
Commit
01022d3
·
verified ·
1 Parent(s): 8fa7273

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -90
app.py CHANGED
@@ -25,8 +25,6 @@ uploaded_files = {}
25
  current_document = None
26
  document_type = None
27
  shredded_document = None
28
- pink_review_document = None
29
- red_review_document = None
30
  generated_documents = {}
31
 
32
  # Document types and their descriptions
@@ -83,14 +81,10 @@ app.layout = dbc.Container([
83
  type="dot",
84
  children=[html.Div(id="loading-output")]
85
  ),
86
- html.Div(id='document-preview', className="border p-3 mb-3"),
87
- dbc.Button("Download Document", id="btn-download", color="success", className="mt-3"),
88
- dcc.Download(id="download-document"),
89
- html.Hr(),
90
- html.Div(id='review-upload', style={'display': 'none'}, children=[
91
  dcc.Upload(
92
- id='upload-review',
93
- children=html.Div(['Drag and Drop or ', html.A('Select Review File')]),
94
  style={
95
  'width': '100%',
96
  'height': '60px',
@@ -103,8 +97,23 @@ app.layout = dbc.Container([
103
  },
104
  multiple=False
105
  ),
106
- html.Div(id='review-file-name')
107
  ]),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  dcc.Loading(
109
  id="chat-loading",
110
  type="dot",
@@ -158,8 +167,8 @@ def update_output(list_of_contents, list_of_names, existing_files):
158
  if existing_files is None:
159
  existing_files = []
160
  shredded_document = None # Reset shredded document when new files are uploaded
161
- return existing_files + new_files, "Document uploaded. Please click 'Shred' to proceed."
162
- return existing_files, "Please upload a document and click 'Shred' to begin."
163
 
164
  @app.callback(
165
  Output('file-list', 'children', allow_duplicate=True),
@@ -176,108 +185,91 @@ def remove_file(n_clicks, existing_files):
176
  removed_file = ctx.triggered[0]['prop_id'].split(',')[0].split(':')[-1].strip('}')
177
  uploaded_files.pop(removed_file, None)
178
  shredded_document = None # Reset shredded document when a file is removed
179
- return [file for file in existing_files if file['props']['children'][1]['props']['children'] != removed_file], "Document removed. Please upload a document and click 'Shred' to begin."
180
 
181
- def generate_document(document_type, file_contents):
182
- prompt = f"""Generate a {document_type} based on the following project artifacts:
183
- {' '.join(file_contents)}
184
- Instructions:
185
- 1. Create the {document_type} as a detailed document.
186
- 2. Use proper formatting and structure.
187
- 3. Include all necessary sections and details.
188
- 4. Start the output immediately with the document content.
189
- 5. IMPORTANT: If the document type is Pink, Red, Gold and not review type, loe or board
190
- then your goal is to be compliant and compelling based on the
191
- requirements, write in paragraph in active voice as
192
- MicroHealth, limit bullets, answer the
193
- requirement with what MicroHealth will do
194
- to satisfy the requirement, the technical
195
- approach with innovation for efficiency,
196
- productivity, quality and measurable
197
- outcomes, the industry standard that
198
- methodology is based on if applicable,
199
- detail the workflow or steps to accomplish
200
- the requirement with labor categories that
201
- will do those tasks in that workflow,
202
- reference reputable research like gartner,
203
- forrester, IDC, Deloitte, Accenture etc
204
- with measures of success and substantiation
205
- of MicroHealth's approach. Never use soft words
206
- like maybe, could be, should, possible be definitive in your language and confident.
207
- 6. you must also take into account section L&M of the document which is the evaluation criteria
208
- to be sure we address them.
209
- Now, generate the {document_type}:
210
- """
211
-
212
- response = model.generate_content(prompt)
213
- return response.text
214
 
215
  @app.callback(
216
  Output('document-preview', 'children'),
217
  Output('loading-output', 'children'),
218
  Output('status-bar', 'children', allow_duplicate=True),
219
- Output('review-upload', 'style'),
220
- [Input(f'btn-{doc_type.lower().replace("_", "-")}', 'n_clicks') for doc_type in document_types.keys()],
221
- State('review-file-name', 'children'),
 
222
  prevent_initial_call=True
223
  )
224
- def generate_document_preview(*args):
225
  global current_document, document_type, shredded_document, generated_documents
226
  ctx = dash.callback_context
227
  if not ctx.triggered:
228
  raise dash.exceptions.PreventUpdate
229
- button_id = ctx.triggered[0]['prop_id'].split('.')[0]
230
- document_type = button_id.replace('btn-', '').replace('-', '_').title()
231
- review_file = args[-1]
232
-
 
 
 
233
  if not uploaded_files and document_type != "Shred":
234
- return html.Div("Please upload and shred a document first."), "", "Please upload and shred a document first.", {'display': 'none'}
235
 
236
- if document_type == "Shred":
237
- if not uploaded_files:
238
- return html.Div("Please upload a document before shredding."), "", "Please upload a document before shredding.", {'display': 'none'}
239
- file_contents = list(uploaded_files.values())
240
- try:
241
  shredded_document = generate_document(document_type, file_contents)
242
  generated_documents['Shred'] = shredded_document
243
- return dcc.Markdown(shredded_document), f"{document_type} generated", "Document shredded. You can now proceed with other operations.", {'display': 'none'}
244
- except Exception as e:
245
- print(f"Error generating document: {str(e)}")
246
- return html.Div(f"Error generating document: {str(e)}"), "Error", "An error occurred while shredding the document.", {'display': 'none'}
247
-
248
- if shredded_document is None:
249
- return html.Div("Please shred a document first."), "", "Please shred a document first.", {'display': 'none'}
250
-
251
- if document_type in ["Pink Review", "Red Review", "Gold Review"]:
252
- return html.Div(f"Please upload a {document_type.split()[0]} Team document or use the generated one."), "", f"Please upload a {document_type.split()[0]} Team document or use the generated one.", {'display': 'block'}
253
-
254
- try:
255
- if document_type == "Pink":
256
- current_document = generate_document(document_type, [shredded_document])
257
- elif document_type == "Red":
258
- if 'Pink Review' not in generated_documents:
259
- return html.Div("Please complete Pink Review first."), "", "Please complete Pink Review first.", {'display': 'none'}
260
- current_document = generate_document(document_type, [generated_documents['Pink Review'], shredded_document])
261
- elif document_type == "Gold":
262
- if 'Red Review' not in generated_documents:
263
- return html.Div("Please complete Red Review first."), "", "Please complete Red Review first.", {'display': 'none'}
264
- current_document = generate_document(document_type, [generated_documents['Red Review'], shredded_document])
265
  else:
266
- current_document = generate_document(document_type, [shredded_document])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
 
268
  generated_documents[document_type] = current_document
269
 
270
- return dcc.Markdown(current_document), f"{document_type} generated", f"{document_type} document generated successfully.", {'display': 'none'}
271
  except Exception as e:
272
  print(f"Error generating document: {str(e)}")
273
- return html.Div(f"Error generating document: {str(e)}"), "Error", "An error occurred while generating the document.", {'display': 'none'}
274
 
275
  @app.callback(
276
- Output('review-file-name', 'children'),
277
- Input('upload-review', 'contents'),
278
- State('upload-review', 'filename')
279
  )
280
- def update_review_filename(contents, filename):
281
  if contents is not None:
282
  return filename
283
  return ""
@@ -319,7 +311,26 @@ def download_document(n_clicks):
319
  if current_document is None:
320
  raise dash.exceptions.PreventUpdate
321
 
322
- if document_type in ["LOE", "Pink Review", "Red Review", "Gold Review", "Virtual Board"]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  # Create a pandas DataFrame for spreadsheet-type documents
324
  df = pd.read_csv(StringIO(current_document))
325
 
 
25
  current_document = None
26
  document_type = None
27
  shredded_document = None
 
 
28
  generated_documents = {}
29
 
30
  # Document types and their descriptions
 
81
  type="dot",
82
  children=[html.Div(id="loading-output")]
83
  ),
84
+ html.Div(id='document-upload-area', style={'display': 'none'}, children=[
 
 
 
 
85
  dcc.Upload(
86
+ id='upload-specific-document',
87
+ children=html.Div(['Drag and Drop or ', html.A('Select Document')]),
88
  style={
89
  'width': '100%',
90
  'height': '60px',
 
97
  },
98
  multiple=False
99
  ),
100
+ html.Div(id='specific-document-name')
101
  ]),
102
+ html.Div(id='document-choice', style={'display': 'none'}, children=[
103
+ dcc.RadioItems(
104
+ id='document-source',
105
+ options=[
106
+ {'label': 'Use Generated Document', 'value': 'generated'},
107
+ {'label': 'Use Uploaded Document', 'value': 'uploaded'}
108
+ ],
109
+ value='generated'
110
+ )
111
+ ]),
112
+ dbc.Button("Process Document", id="btn-process-document", color="primary", className="mt-3", style={'display': 'none'}),
113
+ html.Div(id='document-preview', className="border p-3 mb-3"),
114
+ dbc.Button("Download Document", id="btn-download", color="success", className="mt-3"),
115
+ dcc.Download(id="download-document"),
116
+ html.Hr(),
117
  dcc.Loading(
118
  id="chat-loading",
119
  type="dot",
 
167
  if existing_files is None:
168
  existing_files = []
169
  shredded_document = None # Reset shredded document when new files are uploaded
170
+ return existing_files + new_files, "Document uploaded. Please select a document type to proceed."
171
+ return existing_files, "Please upload a document to begin."
172
 
173
  @app.callback(
174
  Output('file-list', 'children', allow_duplicate=True),
 
185
  removed_file = ctx.triggered[0]['prop_id'].split(',')[0].split(':')[-1].strip('}')
186
  uploaded_files.pop(removed_file, None)
187
  shredded_document = None # Reset shredded document when a file is removed
188
+ return [file for file in existing_files if file['props']['children'][1]['props']['children'] != removed_file], "Document removed. Please upload a document to begin."
189
 
190
+ @app.callback(
191
+ Output('document-upload-area', 'style'),
192
+ Output('document-choice', 'style'),
193
+ Output('btn-process-document', 'style'),
194
+ [Input(f'btn-{doc_type.lower().replace("_", "-")}', 'n_clicks') for doc_type in document_types.keys()],
195
+ prevent_initial_call=True
196
+ )
197
+ def show_document_options(*args):
198
+ ctx = dash.callback_context
199
+ if not ctx.triggered:
200
+ raise dash.exceptions.PreventUpdate
201
+ button_id = ctx.triggered[0]['prop_id'].split('.')[0]
202
+ document_type = button_id.replace('btn-', '').replace('-', '_').title()
203
+
204
+ if document_type == "Shred":
205
+ return {'display': 'none'}, {'display': 'none'}, {'display': 'block'}
206
+ else:
207
+ return {'display': 'block'}, {'display': 'block'}, {'display': 'block'}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
  @app.callback(
210
  Output('document-preview', 'children'),
211
  Output('loading-output', 'children'),
212
  Output('status-bar', 'children', allow_duplicate=True),
213
+ Input('btn-process-document', 'n_clicks'),
214
+ State('document-source', 'value'),
215
+ State('specific-document-name', 'children'),
216
+ [State(f'btn-{doc_type.lower().replace("_", "-")}', 'n_clicks') for doc_type in document_types.keys()],
217
  prevent_initial_call=True
218
  )
219
+ def generate_document_preview(n_clicks, doc_source, uploaded_doc_name, *args):
220
  global current_document, document_type, shredded_document, generated_documents
221
  ctx = dash.callback_context
222
  if not ctx.triggered:
223
  raise dash.exceptions.PreventUpdate
224
+
225
+ # Determine which document type button was last clicked
226
+ for i, arg in enumerate(args):
227
+ if arg is not None and arg > 0:
228
+ document_type = list(document_types.keys())[i]
229
+ break
230
+
231
  if not uploaded_files and document_type != "Shred":
232
+ return html.Div("Please upload a document first."), "", "Please upload a document first."
233
 
234
+ try:
235
+ if document_type == "Shred":
236
+ file_contents = list(uploaded_files.values())
 
 
237
  shredded_document = generate_document(document_type, file_contents)
238
  generated_documents['Shred'] = shredded_document
239
+ current_document = shredded_document
240
+ elif doc_source == 'uploaded' and uploaded_doc_name:
241
+ current_document = process_document(uploaded_doc_name, uploaded_doc_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  else:
243
+ if document_type == "Pink":
244
+ current_document = generate_document(document_type, [shredded_document])
245
+ elif document_type == "Red":
246
+ if 'Pink Review' not in generated_documents:
247
+ return html.Div("Please complete Pink Review first."), "", "Please complete Pink Review first."
248
+ current_document = generate_document(document_type, [generated_documents['Pink Review'], shredded_document])
249
+ elif document_type == "Gold":
250
+ if 'Red Review' not in generated_documents:
251
+ return html.Div("Please complete Red Review first."), "", "Please complete Red Review first."
252
+ current_document = generate_document(document_type, [generated_documents['Red Review'], shredded_document])
253
+ elif document_type in ["Virtual Board", "LOE"]:
254
+ if 'Gold' not in generated_documents:
255
+ return html.Div("Please complete Gold document first."), "", "Please complete Gold document first."
256
+ current_document = generate_document(document_type, [generated_documents['Gold'], shredded_document])
257
+ else:
258
+ current_document = generate_document(document_type, [shredded_document])
259
 
260
  generated_documents[document_type] = current_document
261
 
262
+ return dcc.Markdown(current_document), f"{document_type} generated", f"{document_type} document generated successfully."
263
  except Exception as e:
264
  print(f"Error generating document: {str(e)}")
265
+ return html.Div(f"Error generating document: {str(e)}"), "Error", "An error occurred while generating the document."
266
 
267
  @app.callback(
268
+ Output('specific-document-name', 'children'),
269
+ Input('upload-specific-document', 'contents'),
270
+ State('upload-specific-document', 'filename')
271
  )
272
+ def update_specific_document_name(contents, filename):
273
  if contents is not None:
274
  return filename
275
  return ""
 
311
  if current_document is None:
312
  raise dash.exceptions.PreventUpdate
313
 
314
+ if document_type in ["LOE", "Pink Review", "Red Review", "Gold Review", "Virtual Board", "Shred"]:
315
+ # Create a pandas DataFrame for spreadsheet-type documents
316
+ df = pd.read_csv(StringIO(current_document))
317
+
318
+ # Save the DataFrame to an Excel file
319
+ output = BytesIO()
320
+ with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
321
+ df.to_excel
322
+
323
+ @app.callback(
324
+ Output("download-document", "data"),
325
+ Input("btn-download", "n_clicks"),
326
+ prevent_initial_call=True
327
+ )
328
+ def download_document(n_clicks):
329
+ global current_document, document_type
330
+ if current_document is None:
331
+ raise dash.exceptions.PreventUpdate
332
+
333
+ if document_type in ["LOE", "Pink Review", "Red Review", "Gold Review", "Virtual Board", "Shred"]:
334
  # Create a pandas DataFrame for spreadsheet-type documents
335
  df = pd.read_csv(StringIO(current_document))
336