Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,11 @@ import sys
|
|
| 3 |
import tempfile
|
| 4 |
from pathlib import Path
|
| 5 |
import pandas as pd
|
| 6 |
-
|
| 7 |
-
# Keep Gradio on client-side rendering. In hosted Gradio 6 runs, SSR starts a
|
| 8 |
-
# Node proxy that can emit noisy asyncio cleanup tracebacks at startup/shutdown.
|
| 9 |
-
os.environ.setdefault("GRADIO_SSR_MODE", "false")
|
| 10 |
-
|
| 11 |
import gradio as gr
|
| 12 |
import time
|
| 13 |
|
|
@@ -35,41 +35,41 @@ def gpu_task(duration=120):
|
|
| 35 |
|
| 36 |
# Initialize Extractor
|
| 37 |
print("Initializing Passport Extractor...")
|
| 38 |
-
EXTRACTOR = PassportExtractor(use_gpu=
|
| 39 |
print("Extractor initialized.")
|
| 40 |
|
| 41 |
|
| 42 |
MAX_FILE_SIZE_MB = 100
|
| 43 |
ALLOWED_FILE_TYPES = [
|
| 44 |
-
".png",
|
| 45 |
-
".jpg",
|
| 46 |
-
".jpeg",
|
| 47 |
-
".pdf",
|
| 48 |
-
".avif",
|
| 49 |
-
".webp",
|
| 50 |
-
".bmp",
|
| 51 |
-
".tiff",
|
| 52 |
-
]
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
def _format_data(good_results, airline):
|
| 56 |
-
if airline == "Fly Dubai":
|
| 57 |
-
return format_fly_dubai(good_results)
|
| 58 |
-
if airline == "Iraqi":
|
| 59 |
-
return format_iraqi(good_results)
|
| 60 |
-
if airline == "Fly Baghdad":
|
| 61 |
-
return format_fly_baghdad(good_results)
|
| 62 |
-
|
| 63 |
-
# Default view
|
| 64 |
-
df = pd.DataFrame(good_results)
|
| 65 |
-
if not df.empty:
|
| 66 |
-
from extractor import format_date
|
| 67 |
-
if "date_of_birth" in df.columns:
|
| 68 |
-
df["date_of_birth"] = df["date_of_birth"].apply(lambda x: format_date(x, "%d/%m/%Y", is_dob=True))
|
| 69 |
-
if "expiration_date" in df.columns:
|
| 70 |
-
df["expiration_date"] = df["expiration_date"].apply(lambda x: format_date(x, "%d/%m/%Y", is_dob=False))
|
| 71 |
-
return df
|
| 72 |
-
|
| 73 |
|
| 74 |
@gpu_task(duration=300)
|
| 75 |
def process_files(files, airline, export_all_files, progress=gr.Progress()):
|
|
@@ -146,7 +146,7 @@ def process_files(files, airline, export_all_files, progress=gr.Progress()):
|
|
| 146 |
return file_results, file_problem
|
| 147 |
|
| 148 |
completed_files = 0
|
| 149 |
-
with ThreadPoolExecutor(max_workers=
|
| 150 |
future_to_file = {executor.submit(process_single_file, f): f for f in files}
|
| 151 |
for future in as_completed(future_to_file):
|
| 152 |
completed_files += 1
|
|
@@ -167,14 +167,21 @@ def process_files(files, airline, export_all_files, progress=gr.Progress()):
|
|
| 167 |
end_time = time.time()
|
| 168 |
duration = round(end_time - start_time, 2)
|
| 169 |
|
| 170 |
-
if not good_results:
|
| 171 |
-
status_msg = f"
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
)
|
| 176 |
return None, status_msg, None, None
|
| 177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
df = _format_data(good_results, airline)
|
| 179 |
status_msg = f"Extracted {len(good_results)} passport(s) from {total_files} file(s) in {duration} seconds."
|
| 180 |
if problematic_files:
|
|
@@ -189,9 +196,9 @@ def process_files(files, airline, export_all_files, progress=gr.Progress()):
|
|
| 189 |
return df, status_msg, csv_path, excel_path
|
| 190 |
|
| 191 |
|
| 192 |
-
def
|
| 193 |
with gr.Blocks(title="Passport OCR Extractor") as demo:
|
| 194 |
-
gr.Markdown("#
|
| 195 |
with gr.Row():
|
| 196 |
with gr.Column(scale=1):
|
| 197 |
airline = gr.Dropdown(
|
|
@@ -216,13 +223,6 @@ def create_demo():
|
|
| 216 |
inputs=[file_input, airline, gr.State(False)],
|
| 217 |
outputs=[table_output, status_output, csv_download, excel_download],
|
| 218 |
)
|
| 219 |
-
return demo
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
demo = create_demo()
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
def main():
|
| 226 |
port = int(os.environ.get("PORT", "7860"))
|
| 227 |
demo.launch(server_name="0.0.0.0", server_port=port, show_error=True, ssr_mode=False)
|
| 228 |
|
|
|
|
| 3 |
import tempfile
|
| 4 |
from pathlib import Path
|
| 5 |
import pandas as pd
|
| 6 |
+
|
| 7 |
+
# Keep Gradio on client-side rendering. In hosted Gradio 6 runs, SSR starts a
|
| 8 |
+
# Node proxy that can emit noisy asyncio cleanup tracebacks at startup/shutdown.
|
| 9 |
+
os.environ.setdefault("GRADIO_SSR_MODE", "false")
|
| 10 |
+
|
| 11 |
import gradio as gr
|
| 12 |
import time
|
| 13 |
|
|
|
|
| 35 |
|
| 36 |
# Initialize Extractor
|
| 37 |
print("Initializing Passport Extractor...")
|
| 38 |
+
EXTRACTOR = PassportExtractor(use_gpu=False)
|
| 39 |
print("Extractor initialized.")
|
| 40 |
|
| 41 |
|
| 42 |
MAX_FILE_SIZE_MB = 100
|
| 43 |
ALLOWED_FILE_TYPES = [
|
| 44 |
+
".png",
|
| 45 |
+
".jpg",
|
| 46 |
+
".jpeg",
|
| 47 |
+
".pdf",
|
| 48 |
+
".avif",
|
| 49 |
+
".webp",
|
| 50 |
+
".bmp",
|
| 51 |
+
".tiff",
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def _format_data(good_results, airline):
|
| 56 |
+
if airline == "Fly Dubai":
|
| 57 |
+
return format_fly_dubai(good_results)
|
| 58 |
+
if airline == "Iraqi":
|
| 59 |
+
return format_iraqi(good_results)
|
| 60 |
+
if airline == "Fly Baghdad":
|
| 61 |
+
return format_fly_baghdad(good_results)
|
| 62 |
+
|
| 63 |
+
# Default view
|
| 64 |
+
df = pd.DataFrame(good_results)
|
| 65 |
+
if not df.empty:
|
| 66 |
+
from extractor import format_date
|
| 67 |
+
if "date_of_birth" in df.columns:
|
| 68 |
+
df["date_of_birth"] = df["date_of_birth"].apply(lambda x: format_date(x, "%d/%m/%Y", is_dob=True))
|
| 69 |
+
if "expiration_date" in df.columns:
|
| 70 |
+
df["expiration_date"] = df["expiration_date"].apply(lambda x: format_date(x, "%d/%m/%Y", is_dob=False))
|
| 71 |
+
return df
|
| 72 |
+
|
| 73 |
|
| 74 |
@gpu_task(duration=300)
|
| 75 |
def process_files(files, airline, export_all_files, progress=gr.Progress()):
|
|
|
|
| 146 |
return file_results, file_problem
|
| 147 |
|
| 148 |
completed_files = 0
|
| 149 |
+
with ThreadPoolExecutor(max_workers=2) as executor:
|
| 150 |
future_to_file = {executor.submit(process_single_file, f): f for f in files}
|
| 151 |
for future in as_completed(future_to_file):
|
| 152 |
completed_files += 1
|
|
|
|
| 167 |
end_time = time.time()
|
| 168 |
duration = round(end_time - start_time, 2)
|
| 169 |
|
| 170 |
+
if not good_results and problematic_files:
|
| 171 |
+
status_msg = f"No data could be extracted. (Time: {duration}s)"
|
| 172 |
+
status_msg += "\n\nProblems:\n" + "\n".join(
|
| 173 |
+
[f"- {p['file_name']}: {p['reason']}" for p in problematic_files]
|
| 174 |
+
)
|
|
|
|
| 175 |
return None, status_msg, None, None
|
| 176 |
|
| 177 |
+
if not good_results:
|
| 178 |
+
return (
|
| 179 |
+
None,
|
| 180 |
+
f"âš ï¸ No data could be extracted. (Time: {duration}s)",
|
| 181 |
+
None,
|
| 182 |
+
None,
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
df = _format_data(good_results, airline)
|
| 186 |
status_msg = f"Extracted {len(good_results)} passport(s) from {total_files} file(s) in {duration} seconds."
|
| 187 |
if problematic_files:
|
|
|
|
| 196 |
return df, status_msg, csv_path, excel_path
|
| 197 |
|
| 198 |
|
| 199 |
+
def main():
|
| 200 |
with gr.Blocks(title="Passport OCR Extractor") as demo:
|
| 201 |
+
gr.Markdown("#Passport OCR Extractor")
|
| 202 |
with gr.Row():
|
| 203 |
with gr.Column(scale=1):
|
| 204 |
airline = gr.Dropdown(
|
|
|
|
| 223 |
inputs=[file_input, airline, gr.State(False)],
|
| 224 |
outputs=[table_output, status_output, csv_download, excel_download],
|
| 225 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
port = int(os.environ.get("PORT", "7860"))
|
| 227 |
demo.launch(server_name="0.0.0.0", server_port=port, show_error=True, ssr_mode=False)
|
| 228 |
|