bluenevus commited on
Commit
5ec2a94
·
verified ·
1 Parent(s): 8e99391

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -23
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
- if filename.endswith('.pdf'):
33
- pdf = PdfReader(io.BytesIO(decoded))
34
- text = ""
35
- for page in pdf.pages:
36
- text += page.extract_text()
37
- elif filename.endswith('.docx'):
38
- doc = Document(io.BytesIO(decoded))
39
- text = "\n".join([para.text for para in doc.paragraphs])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  else:
41
- return "Unsupported file format. Please upload a PDF or DOCX file."
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
- def download_loe(n_clicks, loe_output):
616
- if loe_output is None:
617
- return dash.no_update
618
- if isinstance(loe_output, list) and len(loe_output) > 0:
619
- content = loe_output[0]['props']['children'] if isinstance(loe_output[0], dict) else str(loe_output[0])
620
- else:
621
- content = str(loe_output)
622
- doc = create_docx(content)
623
- buffer = BytesIO()
624
- doc.save(buffer)
625
- buffer.seek(0)
626
- return dcc.send_bytes(buffer.getvalue(), "loe_report.docx")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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...")