Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,25 +7,31 @@ from datetime import datetime
|
|
| 7 |
|
| 8 |
# Streamlit UI
|
| 9 |
st.title("Generate Meal Request PDF")
|
| 10 |
-
st.write("Upload
|
| 11 |
|
| 12 |
-
# Input Fields
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
template_file = st.file_uploader("Upload the Meal Request Template PDF", type=["pdf"])
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Function to populate the template with extracted data
|
| 31 |
def generate_meal_request_pdf(template_pdf, data):
|
|
@@ -66,18 +72,17 @@ def generate_meal_request_pdf(template_pdf, data):
|
|
| 66 |
|
| 67 |
# Process when the button is pressed
|
| 68 |
if st.button("Generate Meal Request PDF"):
|
| 69 |
-
if not
|
| 70 |
-
st.error("Please provide
|
| 71 |
else:
|
| 72 |
-
# Step 1: Extract data from the uploaded
|
| 73 |
-
|
| 74 |
|
| 75 |
-
if
|
| 76 |
-
# Simulating data
|
| 77 |
-
# For demonstration purposes, assuming extracted data is text with specific info
|
| 78 |
extracted_data = {
|
| 79 |
-
"name": "Alice", # Normally
|
| 80 |
-
"meal_type": "Vegan", # Normally
|
| 81 |
"date": datetime.today().strftime("%B %d, %Y"),
|
| 82 |
}
|
| 83 |
|
|
|
|
| 7 |
|
| 8 |
# Streamlit UI
|
| 9 |
st.title("Generate Meal Request PDF")
|
| 10 |
+
st.write("Upload two PDF files and provide a Meal Request template.")
|
| 11 |
|
| 12 |
+
# Input Fields: Allow uploading multiple PDFs
|
| 13 |
+
uploaded_files = st.file_uploader("Upload Two PDF Files", type=["pdf"], accept_multiple_files=True)
|
| 14 |
+
|
| 15 |
+
# Template PDF: Upload template PDF
|
| 16 |
template_file = st.file_uploader("Upload the Meal Request Template PDF", type=["pdf"])
|
| 17 |
|
| 18 |
+
# Ensure two PDFs are uploaded
|
| 19 |
+
if len(uploaded_files) != 2:
|
| 20 |
+
st.error("Please upload exactly two PDF files.")
|
| 21 |
+
else:
|
| 22 |
+
st.success("Two PDF files uploaded successfully!")
|
| 23 |
+
|
| 24 |
+
# Function to extract data from the uploaded input files
|
| 25 |
+
def extract_data_from_files(files):
|
| 26 |
+
extracted_text = []
|
| 27 |
+
|
| 28 |
+
for file in files:
|
| 29 |
+
if file.type == "application/pdf":
|
| 30 |
+
reader = PdfReader(file)
|
| 31 |
+
text = " ".join([page.extract_text() for page in reader.pages if page.extract_text()])
|
| 32 |
+
extracted_text.append(text)
|
| 33 |
+
|
| 34 |
+
return extracted_text
|
| 35 |
|
| 36 |
# Function to populate the template with extracted data
|
| 37 |
def generate_meal_request_pdf(template_pdf, data):
|
|
|
|
| 72 |
|
| 73 |
# Process when the button is pressed
|
| 74 |
if st.button("Generate Meal Request PDF"):
|
| 75 |
+
if not template_file:
|
| 76 |
+
st.error("Please provide the template PDF.")
|
| 77 |
else:
|
| 78 |
+
# Step 1: Extract data from the uploaded files
|
| 79 |
+
data_from_files = extract_data_from_files(uploaded_files)
|
| 80 |
|
| 81 |
+
if data_from_files:
|
| 82 |
+
# Simulating extraction of relevant data (replace this with actual logic)
|
|
|
|
| 83 |
extracted_data = {
|
| 84 |
+
"name": "Alice", # Normally extracted from the PDF
|
| 85 |
+
"meal_type": "Vegan", # Normally extracted from the PDF
|
| 86 |
"date": datetime.today().strftime("%B %d, %Y"),
|
| 87 |
}
|
| 88 |
|