Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,18 +29,33 @@ def process_document(contents: str, filename: str) -> str:
|
|
| 29 |
content_type, content_string = contents.split(',')
|
| 30 |
decoded = base64.b64decode(content_string)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
else:
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return text
|
| 44 |
|
| 45 |
def generate_outline(text: str, instructions: str) -> str:
|
| 46 |
prompt = f"""
|
|
@@ -612,18 +627,41 @@ def download_g_review(n_clicks, g_review_output):
|
|
| 612 |
State('loe-output', 'children'),
|
| 613 |
prevent_initial_call=True,
|
| 614 |
)
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
|
| 628 |
if __name__ == '__main__':
|
| 629 |
print("Starting the Dash application...")
|
|
|
|
| 29 |
content_type, content_string = contents.split(',')
|
| 30 |
decoded = base64.b64decode(content_string)
|
| 31 |
|
| 32 |
+
try:
|
| 33 |
+
if filename.lower().endswith('.pdf'):
|
| 34 |
+
pdf = PdfReader(io.BytesIO(decoded))
|
| 35 |
+
text = ""
|
| 36 |
+
for page in pdf.pages:
|
| 37 |
+
text += page.extract_text()
|
| 38 |
+
elif filename.lower().endswith('.docx'):
|
| 39 |
+
doc = Document(io.BytesIO(decoded))
|
| 40 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 41 |
+
else:
|
| 42 |
+
return f"Unsupported file format: {filename}. Please upload a PDF or DOCX file."
|
| 43 |
+
|
| 44 |
+
if not text.strip():
|
| 45 |
+
return "The document appears to be empty. Please check the file and try again."
|
| 46 |
+
|
| 47 |
+
return text
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error processing document: {str(e)}"
|
| 50 |
+
|
| 51 |
+
def generate_loe(document: str, is_file: bool = False, filename: str = "") -> Tuple[str, pd.DataFrame]:
|
| 52 |
+
if is_file:
|
| 53 |
+
# Process the uploaded document
|
| 54 |
+
document_text = process_document(document, filename)
|
| 55 |
+
if document_text.startswith("Unsupported file format") or document_text.startswith("Error processing document"):
|
| 56 |
+
return document_text, pd.DataFrame()
|
| 57 |
else:
|
| 58 |
+
document_text = document
|
|
|
|
|
|
|
| 59 |
|
| 60 |
def generate_outline(text: str, instructions: str) -> str:
|
| 61 |
prompt = f"""
|
|
|
|
| 627 |
State('loe-output', 'children'),
|
| 628 |
prevent_initial_call=True,
|
| 629 |
)
|
| 630 |
+
|
| 631 |
+
@app.callback(
|
| 632 |
+
Output('loe-output', 'children'),
|
| 633 |
+
Input('generate-loe', 'n_clicks'),
|
| 634 |
+
State('upload-loe', 'contents'),
|
| 635 |
+
State('upload-loe', 'filename'),
|
| 636 |
+
State('shred-output', 'children')
|
| 637 |
+
)
|
| 638 |
+
def update_loe_output(n_clicks, upload_contents, upload_filename, shred_output):
|
| 639 |
+
if n_clicks is None:
|
| 640 |
+
return "Click 'Generate LOE' to begin."
|
| 641 |
+
|
| 642 |
+
try:
|
| 643 |
+
if upload_contents:
|
| 644 |
+
loe_text, loe_df = generate_loe(upload_contents, is_file=True, filename=upload_filename)
|
| 645 |
+
elif shred_output:
|
| 646 |
+
loe_text, loe_df = generate_loe(shred_output)
|
| 647 |
+
else:
|
| 648 |
+
return "Please upload a document or complete the Shred tab first."
|
| 649 |
+
|
| 650 |
+
if isinstance(loe_text, str) and loe_text.startswith(("Unsupported file format", "Error processing document", "The document appears to be empty")):
|
| 651 |
+
return loe_text
|
| 652 |
+
|
| 653 |
+
return [
|
| 654 |
+
dcc.Markdown(loe_text),
|
| 655 |
+
dash_table.DataTable(
|
| 656 |
+
data=loe_df.to_dict('records'),
|
| 657 |
+
columns=[{'name': i, 'id': i} for i in loe_df.columns],
|
| 658 |
+
style_table={'overflowX': 'auto'},
|
| 659 |
+
style_cell={'textAlign': 'left', 'padding': '5px'},
|
| 660 |
+
style_header={'backgroundColor': 'rgb(230, 230, 230)', 'fontWeight': 'bold'}
|
| 661 |
+
)
|
| 662 |
+
]
|
| 663 |
+
except Exception as e:
|
| 664 |
+
return f"An error occurred: {str(e)}"
|
| 665 |
|
| 666 |
if __name__ == '__main__':
|
| 667 |
print("Starting the Dash application...")
|