Spaces:
Paused
Paused
Update app.py
Browse files
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-
|
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-
|
93 |
-
children=html.Div(['Drag and Drop or ', html.A('Select
|
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='
|
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
|
162 |
-
return existing_files, "Please upload a document
|
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
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
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 |
-
|
220 |
-
|
221 |
-
State('
|
|
|
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 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
233 |
if not uploaded_files and document_type != "Shred":
|
234 |
-
return html.Div("Please upload
|
235 |
|
236 |
-
|
237 |
-
if
|
238 |
-
|
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 |
-
|
244 |
-
|
245 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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."
|
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."
|
274 |
|
275 |
@app.callback(
|
276 |
-
Output('
|
277 |
-
Input('upload-
|
278 |
-
State('upload-
|
279 |
)
|
280 |
-
def
|
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 |
|