Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdd DPI slider feature
app.py
CHANGED
|
@@ -32,7 +32,7 @@ def extract_zip_and_collect_files(zip_file_path):
|
|
| 32 |
return collected_files
|
| 33 |
|
| 34 |
# Function to process a list of PDF files and convert them to images
|
| 35 |
-
def process_pdfs(pdf_files):
|
| 36 |
"""
|
| 37 |
Process a list of PDF files, convert each to images, and return all images.
|
| 38 |
"""
|
|
@@ -42,7 +42,7 @@ def process_pdfs(pdf_files):
|
|
| 42 |
if not os.path.isfile(pdf_file):
|
| 43 |
raise ValueError(f"File {pdf_file} does not exist.")
|
| 44 |
|
| 45 |
-
images = convert_from_path(pdf_file, dpi=
|
| 46 |
all_images.extend(images)
|
| 47 |
|
| 48 |
return all_images
|
|
@@ -259,7 +259,7 @@ Output:
|
|
| 259 |
zipf.write(pdf_path, arcname=f"{doc_name}.pdf")
|
| 260 |
return zip_path
|
| 261 |
|
| 262 |
-
def main_process(files, tanggal_berangkat, tanggal_pulang, api_key):
|
| 263 |
all_images = []
|
| 264 |
image_paths_for_zip = []
|
| 265 |
|
|
@@ -270,13 +270,13 @@ def main_process(files, tanggal_berangkat, tanggal_pulang, api_key):
|
|
| 270 |
extracted_files = extract_zip_and_collect_files(file_path)
|
| 271 |
for extracted_file in extracted_files:
|
| 272 |
if extracted_file.lower().endswith('.pdf'):
|
| 273 |
-
images = process_pdfs([extracted_file])
|
| 274 |
all_images.extend(images)
|
| 275 |
elif extracted_file.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 276 |
image = Image.open(extracted_file)
|
| 277 |
all_images.append(image)
|
| 278 |
elif file_path.lower().endswith('.pdf'):
|
| 279 |
-
images = process_pdfs([file_path])
|
| 280 |
all_images.extend(images)
|
| 281 |
elif file_path.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 282 |
image = Image.open(file_path)
|
|
@@ -338,6 +338,14 @@ with gr.Blocks() as demo:
|
|
| 338 |
placeholder="Masukan Kode API",
|
| 339 |
type="text"
|
| 340 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
| 342 |
run_btn = gr.Button("🏃 Run Analysis")
|
| 343 |
|
|
@@ -358,8 +366,8 @@ with gr.Blocks() as demo:
|
|
| 358 |
|
| 359 |
run_btn.click(
|
| 360 |
fn=main_process,
|
| 361 |
-
inputs=[file_input, tanggal_berangkat, tanggal_pulang, api_key],
|
| 362 |
outputs=[download_output_docx, form_filling_output, download_valid_zip, invalid_list_output, raw_output, summary_output, notice_msg]
|
| 363 |
)
|
| 364 |
|
| 365 |
-
demo.launch(debug=True)
|
|
|
|
| 32 |
return collected_files
|
| 33 |
|
| 34 |
# Function to process a list of PDF files and convert them to images
|
| 35 |
+
def process_pdfs(pdf_files, dpi):
|
| 36 |
"""
|
| 37 |
Process a list of PDF files, convert each to images, and return all images.
|
| 38 |
"""
|
|
|
|
| 42 |
if not os.path.isfile(pdf_file):
|
| 43 |
raise ValueError(f"File {pdf_file} does not exist.")
|
| 44 |
|
| 45 |
+
images = convert_from_path(pdf_file, dpi=dpi)
|
| 46 |
all_images.extend(images)
|
| 47 |
|
| 48 |
return all_images
|
|
|
|
| 259 |
zipf.write(pdf_path, arcname=f"{doc_name}.pdf")
|
| 260 |
return zip_path
|
| 261 |
|
| 262 |
+
def main_process(files, tanggal_berangkat, tanggal_pulang, api_key, dpi):
|
| 263 |
all_images = []
|
| 264 |
image_paths_for_zip = []
|
| 265 |
|
|
|
|
| 270 |
extracted_files = extract_zip_and_collect_files(file_path)
|
| 271 |
for extracted_file in extracted_files:
|
| 272 |
if extracted_file.lower().endswith('.pdf'):
|
| 273 |
+
images = process_pdfs([extracted_file], dpi=dpi_input)
|
| 274 |
all_images.extend(images)
|
| 275 |
elif extracted_file.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 276 |
image = Image.open(extracted_file)
|
| 277 |
all_images.append(image)
|
| 278 |
elif file_path.lower().endswith('.pdf'):
|
| 279 |
+
images = process_pdfs([file_path], dpi=dpi_input)
|
| 280 |
all_images.extend(images)
|
| 281 |
elif file_path.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 282 |
image = Image.open(file_path)
|
|
|
|
| 338 |
placeholder="Masukan Kode API",
|
| 339 |
type="text"
|
| 340 |
)
|
| 341 |
+
dpi_slider = gr.Slider(
|
| 342 |
+
minimum=100,
|
| 343 |
+
maximum=500,
|
| 344 |
+
step=25,
|
| 345 |
+
label="Adjust DPI (100 - 500, Δ=25)",
|
| 346 |
+
value=300 # default value
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
|
| 350 |
run_btn = gr.Button("🏃 Run Analysis")
|
| 351 |
|
|
|
|
| 366 |
|
| 367 |
run_btn.click(
|
| 368 |
fn=main_process,
|
| 369 |
+
inputs=[file_input, tanggal_berangkat, tanggal_pulang, api_key, dpi_slider],
|
| 370 |
outputs=[download_output_docx, form_filling_output, download_valid_zip, invalid_list_output, raw_output, summary_output, notice_msg]
|
| 371 |
)
|
| 372 |
|
| 373 |
+
demo.launch(debug=True)
|