makhdoomnaeem commited on
Commit
e185033
·
verified ·
1 Parent(s): 8dc9200

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
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 a template PDF and provide an input file containing data for the meal request.")
11
 
12
- # Input Fields
13
- input_file = st.file_uploader("Upload the Input File (PDF or Text)", type=["pdf", "txt"])
 
 
14
  template_file = st.file_uploader("Upload the Meal Request Template PDF", type=["pdf"])
15
 
16
- # Function to extract data from the uploaded input file
17
- def extract_data_from_file(file):
18
- if file.type == "application/pdf":
19
- # Extract text from PDF
20
- reader = PdfReader(file)
21
- text = " ".join([page.extract_text() for page in reader.pages if page.extract_text()])
22
- return text
23
- elif file.type == "text/plain":
24
- # If the file is a text file, just read the content
25
- return file.read().decode("utf-8")
26
- else:
27
- st.warning("Unsupported file format. Please upload a PDF or text file.")
28
- return None
 
 
 
 
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 input_file or not template_file:
70
- st.error("Please provide both the input file and the template PDF.")
71
  else:
72
- # Step 1: Extract data from the uploaded input file
73
- data_from_file = extract_data_from_file(input_file)
74
 
75
- if data_from_file:
76
- # Simulating data extraction from the uploaded file
77
- # For demonstration purposes, assuming extracted data is text with specific info
78
  extracted_data = {
79
- "name": "Alice", # Normally, extract from the input text
80
- "meal_type": "Vegan", # Normally, extract from the input text
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