frendyrachman commited on
Commit
f7daf2e
Β·
verified Β·
1 Parent(s): 17485f9

Update ZIP input feature

Browse files
Files changed (1) hide show
  1. app.py +38 -13
app.py CHANGED
@@ -115,38 +115,63 @@ def gemini_analysis(images):
115
  return response.text
116
 
117
 
118
- # full flow process
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  def main_process(files):
120
  all_images = []
121
-
122
- for file in files:
123
- file_path = file.name if hasattr(file, 'name') else file # handle Gradio file objects
124
 
125
- if file_path.lower().endswith('.pdf'):
126
- images = process_pdfs([file_path]) # Pass as list
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  all_images.extend(images)
128
  elif file_path.lower().endswith(('.jpg', '.jpeg', '.png')):
129
  image = Image.open(file_path)
130
  all_images.append(image)
131
  else:
132
- raise ValueError(f"File {file_path} is not a valid image or PDF.")
133
-
134
- # Perform analysis using Gemini AI
135
  summary = gemini_analysis(all_images)
136
  return summary
137
 
138
 
139
- # Gradio UI
140
  with gr.Blocks() as demo:
141
  gr.Markdown("## 🧠 Summarize Documents with Gemini AI (Image Analysis Only)")
142
  with gr.Row():
143
  file_input = gr.File(
144
- label="Upload PDFs or Images (Multiple Supported)",
145
- file_types=[".pdf", ".jpg", ".jpeg", ".png"],
146
  file_count="multiple"
147
  )
148
  output = gr.Textbox(label="πŸ“ Summary Result", lines=20)
149
  run_btn = gr.Button("πŸ” Run Analysis")
150
  run_btn.click(fn=main_process, inputs=[file_input], outputs=output)
151
 
152
- demo.launch()
 
115
  return response.text
116
 
117
 
118
+ def extract_zip_and_collect_files(zip_file_path):
119
+ """
120
+ Extract zip file to a temp directory and return list of pdf/image file paths inside.
121
+ """
122
+ temp_dir = tempfile.mkdtemp()
123
+ with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
124
+ zip_ref.extractall(temp_dir)
125
+
126
+ # Collect all pdf and image files in extracted folder recursively
127
+ collected_files = []
128
+ for root, _, files in os.walk(temp_dir):
129
+ for f in files:
130
+ if f.lower().endswith(('.pdf', '.jpg', '.jpeg', '.png')):
131
+ collected_files.append(os.path.join(root, f))
132
+ return collected_files
133
+
134
+
135
  def main_process(files):
136
  all_images = []
 
 
 
137
 
138
+ for file in files:
139
+ file_path = file.name if hasattr(file, 'name') else file
140
+
141
+ if file_path.lower().endswith('.zip'):
142
+ # Extract zip and process contained files
143
+ extracted_files = extract_zip_and_collect_files(file_path)
144
+ for extracted_file in extracted_files:
145
+ if extracted_file.lower().endswith('.pdf'):
146
+ images = process_pdfs([extracted_file])
147
+ all_images.extend(images)
148
+ elif extracted_file.lower().endswith(('.jpg', '.jpeg', '.png')):
149
+ image = Image.open(extracted_file)
150
+ all_images.append(image)
151
+ elif file_path.lower().endswith('.pdf'):
152
+ images = process_pdfs([file_path])
153
  all_images.extend(images)
154
  elif file_path.lower().endswith(('.jpg', '.jpeg', '.png')):
155
  image = Image.open(file_path)
156
  all_images.append(image)
157
  else:
158
+ raise ValueError(f"File {file_path} is not a valid image, PDF, or ZIP.")
159
+
 
160
  summary = gemini_analysis(all_images)
161
  return summary
162
 
163
 
164
+ # Gradio UI update: add ".zip" to accepted file types
165
  with gr.Blocks() as demo:
166
  gr.Markdown("## 🧠 Summarize Documents with Gemini AI (Image Analysis Only)")
167
  with gr.Row():
168
  file_input = gr.File(
169
+ label="Upload PDFs, Images or ZIP files (Multiple Supported)",
170
+ file_types=[".pdf", ".jpg", ".jpeg", ".png", ".zip"],
171
  file_count="multiple"
172
  )
173
  output = gr.Textbox(label="πŸ“ Summary Result", lines=20)
174
  run_btn = gr.Button("πŸ” Run Analysis")
175
  run_btn.click(fn=main_process, inputs=[file_input], outputs=output)
176
 
177
+ demo.launch()