Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -30,33 +30,70 @@ def process_document(contents: str, filename: str) -> str:
|
|
| 30 |
decoded = base64.b64decode(content_string)
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
if filename.lower().endswith('.
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
text = ""
|
| 36 |
for page in pdf.pages:
|
| 37 |
text += page.extract_text()
|
| 38 |
-
|
| 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"""
|
| 62 |
Analyze the following Project Work Statement (PWS) and create an outline
|
|
@@ -659,21 +696,19 @@ def update_loe_output(n_clicks, upload_contents, upload_filename, shred_output):
|
|
| 659 |
else:
|
| 660 |
return "Please upload a document or complete the Shred tab first."
|
| 661 |
|
| 662 |
-
|
| 663 |
-
|
| 664 |
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
else:
|
| 676 |
-
return dash.no_update
|
| 677 |
except Exception as e:
|
| 678 |
return f"An error occurred: {str(e)}"
|
| 679 |
|
|
|
|
| 30 |
decoded = base64.b64decode(content_string)
|
| 31 |
|
| 32 |
try:
|
| 33 |
+
if filename.lower().endswith('.docx'):
|
| 34 |
+
doc = Document(BytesIO(decoded))
|
| 35 |
+
text = "\n".join([para.text for para in doc.paragraphs])
|
| 36 |
+
return text
|
| 37 |
+
elif filename.lower().endswith('.pdf'):
|
| 38 |
+
pdf = PdfReader(BytesIO(decoded))
|
| 39 |
text = ""
|
| 40 |
for page in pdf.pages:
|
| 41 |
text += page.extract_text()
|
| 42 |
+
return text
|
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
return f"Unsupported file format: {filename}. Please upload a PDF or DOCX file."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error processing document: {str(e)}"
|
| 47 |
|
| 48 |
def generate_loe(document: str, is_file: bool = False, filename: str = "") -> Tuple[str, pd.DataFrame]:
|
| 49 |
if is_file:
|
|
|
|
| 50 |
document_text = process_document(document, filename)
|
| 51 |
if document_text.startswith("Unsupported file format") or document_text.startswith("Error processing document"):
|
| 52 |
return document_text, pd.DataFrame()
|
| 53 |
else:
|
| 54 |
document_text = document
|
| 55 |
|
| 56 |
+
prompt = f"""
|
| 57 |
+
Analyze the following document and provide a Level of Effort (LOE) breakdown:
|
| 58 |
+
|
| 59 |
+
Document:
|
| 60 |
+
{document_text}
|
| 61 |
+
|
| 62 |
+
For each section header in the document:
|
| 63 |
+
1. Identify the tasks to be completed
|
| 64 |
+
2. Determine the appropriate labor categories for each task
|
| 65 |
+
3. Estimate the number of hours required for each labor category to complete the task
|
| 66 |
+
|
| 67 |
+
Provide a detailed breakdown and then summarize the information in a tabular format with the following columns:
|
| 68 |
+
- Task Summary
|
| 69 |
+
- Labor Categories
|
| 70 |
+
- Hours per Labor Category
|
| 71 |
+
- Total Hours
|
| 72 |
+
|
| 73 |
+
Present the detailed breakdown first, followed by the summary table.
|
| 74 |
+
Ensure the table is properly formatted with | as column separators and a header row.
|
| 75 |
+
"""
|
| 76 |
+
response = model.generate_content(prompt)
|
| 77 |
+
response_text = response.text
|
| 78 |
+
|
| 79 |
+
# Extract the table from the response
|
| 80 |
+
table_start = response_text.find("| Task Summary |")
|
| 81 |
+
table_end = response_text.find("\n\n", table_start)
|
| 82 |
+
table_text = response_text[table_start:table_end]
|
| 83 |
+
|
| 84 |
+
# Convert the table to a pandas DataFrame
|
| 85 |
+
try:
|
| 86 |
+
if not table_text.strip():
|
| 87 |
+
raise pd.errors.EmptyDataError("No table found in the response")
|
| 88 |
+
df = pd.read_csv(StringIO(table_text), sep='|', skipinitialspace=True).dropna(axis=1, how='all')
|
| 89 |
+
df.columns = df.columns.str.strip()
|
| 90 |
+
except pd.errors.EmptyDataError:
|
| 91 |
+
# If no table is found or it's empty, create a default DataFrame
|
| 92 |
+
df = pd.DataFrame(columns=['Task Summary', 'Labor Categories', 'Hours per Labor Category', 'Total Hours'])
|
| 93 |
+
response_text += "\n\nNote: No detailed LOE table could be generated from the AI response."
|
| 94 |
+
|
| 95 |
+
return response_text, df
|
| 96 |
+
|
| 97 |
def generate_outline(text: str, instructions: str) -> str:
|
| 98 |
prompt = f"""
|
| 99 |
Analyze the following Project Work Statement (PWS) and create an outline
|
|
|
|
| 696 |
else:
|
| 697 |
return "Please upload a document or complete the Shred tab first."
|
| 698 |
|
| 699 |
+
if isinstance(loe_text, str) and loe_text.startswith(("Unsupported file format", "Error processing document")):
|
| 700 |
+
return loe_text
|
| 701 |
|
| 702 |
+
return [
|
| 703 |
+
dcc.Markdown(loe_text),
|
| 704 |
+
dash_table.DataTable(
|
| 705 |
+
data=loe_df.to_dict('records'),
|
| 706 |
+
columns=[{'name': i, 'id': i} for i in loe_df.columns],
|
| 707 |
+
style_table={'overflowX': 'auto'},
|
| 708 |
+
style_cell={'textAlign': 'left', 'padding': '5px'},
|
| 709 |
+
style_header={'backgroundColor': 'rgb(230, 230, 230)', 'fontWeight': 'bold'}
|
| 710 |
+
)
|
| 711 |
+
]
|
|
|
|
|
|
|
| 712 |
except Exception as e:
|
| 713 |
return f"An error occurred: {str(e)}"
|
| 714 |
|