heerjtdev commited on
Commit
0a214f3
Β·
verified Β·
1 Parent(s): af33c93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -148
app.py CHANGED
@@ -1,150 +1,3 @@
1
- # import gradio as gr
2
- # import json
3
- # import os
4
- # import tempfile
5
- # import img2pdf
6
- # from pathlib import Path
7
-
8
- # # ==============================
9
- # # PIPELINE IMPORT
10
- # # ==============================
11
- # try:
12
- # from working_yolo_pipeline import run_document_pipeline, DEFAULT_LAYOUTLMV3_MODEL_PATH, WEIGHTS_PATH
13
- # except ImportError:
14
- # print("Warning: 'working_yolo_pipeline.py' not found. Using dummy paths.")
15
- # def run_document_pipeline(*args):
16
- # return {"error": "Placeholder pipeline function called."}
17
- # DEFAULT_LAYOUTLMV3_MODEL_PATH = "./models/layoutlmv3_model"
18
- # WEIGHTS_PATH = "./weights/yolo_weights.pt"
19
-
20
- # def process_file(uploaded_files, layoutlmv3_model_path=None):
21
- # """
22
- # Converts multiple images into a single PDF (if necessary) and routes
23
- # the result to the YOLO/OCR pipeline as a single entity.
24
- # """
25
- # if not uploaded_files:
26
- # return "❌ Error: No files uploaded.", None
27
-
28
- # # Ensure we are working with a list of files (Gradio file_count="multiple" returns a list)
29
- # if not isinstance(uploaded_files, list):
30
- # uploaded_files = [uploaded_files]
31
-
32
- # # 1. Resolve all file paths
33
- # resolved_paths = []
34
- # for f in uploaded_files:
35
- # if hasattr(f, 'path'):
36
- # resolved_paths.append(f.path)
37
- # elif isinstance(f, dict):
38
- # resolved_paths.append(f.get("path"))
39
- # else:
40
- # resolved_paths.append(str(f))
41
-
42
- # # 2. Determine if we should merge into a single PDF
43
- # # We merge if there are multiple files OR if the single file is an image
44
- # first_file = Path(resolved_paths[0])
45
- # is_image = first_file.suffix.lower() in ['.jpg', '.jpeg', '.png', '.bmp', '.webp', '.tiff']
46
-
47
- # processing_path = None
48
-
49
- # try:
50
- # if len(resolved_paths) > 1 or (len(resolved_paths) == 1 and is_image):
51
- # print(f"πŸ“¦ Converting {len(resolved_paths)} image(s) to a single PDF entity...")
52
- # temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
53
- # # img2pdf.convert converts a list of image paths into PDF bytes
54
- # with open(temp_pdf.name, "wb") as f:
55
- # f.write(img2pdf.convert(resolved_paths))
56
- # processing_path = temp_pdf.name
57
- # else:
58
- # # It's a single PDF, process directly
59
- # processing_path = resolved_paths[0]
60
-
61
- # # 3. Standard Pipeline Checks
62
- # if not layoutlmv3_model_path:
63
- # layoutlmv3_model_path = DEFAULT_LAYOUTLMV3_MODEL_PATH
64
-
65
- # if not os.path.exists(layoutlmv3_model_path):
66
- # return f"❌ Error: LayoutLMv3 model not found at {layoutlmv3_model_path}", None
67
-
68
- # if not os.path.exists(WEIGHTS_PATH):
69
- # return f"❌ Error: YOLO weights not found at {WEIGHTS_PATH}", None
70
-
71
- # print(f"πŸš€ Starting pipeline for merged entity: {processing_path}")
72
-
73
- # # 4. Call the pipeline
74
- # result = run_document_pipeline(processing_path, layoutlmv3_model_path)
75
-
76
- # if result is None:
77
- # return "❌ Error: Pipeline failed to process the document.", None
78
-
79
- # # 5. Prepare output
80
- # temp_output = tempfile.NamedTemporaryFile(mode='w', delete=False, suffix='.json', prefix='analysis_')
81
- # with open(temp_output.name, 'w', encoding='utf-8') as f:
82
- # json.dump(result, f, indent=2, ensure_ascii=False)
83
-
84
- # json_display = json.dumps(result, indent=2, ensure_ascii=False)
85
- # return json_display, temp_output.name
86
-
87
- # except Exception as e:
88
- # import traceback
89
- # traceback.print_exc()
90
- # return f"❌ Error during processing: {str(e)}", None
91
-
92
- # # ==============================
93
- # # GRADIO INTERFACE
94
- # # ==============================
95
- # with gr.Blocks(title="Document Analysis Pipeline") as demo:
96
-
97
- # gr.Markdown("""
98
- # # πŸ“„ Document & Image Analysis Pipeline
99
- # Upload **multiple images** or a **PDF**. Multiple images will be processed together as a single continuous document.
100
- # """)
101
-
102
- # with gr.Row():
103
- # with gr.Column(scale=1):
104
- # file_input = gr.File(
105
- # label="Upload PDFs or Images",
106
- # file_types=[".pdf", ".jpg", ".jpeg", ".png", ".bmp", ".webp", ".tiff"],
107
- # type="filepath",
108
- # file_count="multiple" # ALLOWS MULTIPLE FILES
109
- # )
110
-
111
- # model_path_input = gr.Textbox(
112
- # label="LayoutLMv3 Model Path (optional)",
113
- # placeholder=DEFAULT_LAYOUTLMV3_MODEL_PATH,
114
- # value=DEFAULT_LAYOUTLMV3_MODEL_PATH,
115
- # interactive=True
116
- # )
117
-
118
- # process_btn = gr.Button("πŸš€ Process Files", variant="primary", size="lg")
119
-
120
- # with gr.Column(scale=2):
121
- # json_output = gr.Code(
122
- # label="Combined Structured JSON Output",
123
- # language="json",
124
- # lines=25
125
- # )
126
-
127
- # download_output = gr.File(
128
- # label="Download Full JSON",
129
- # interactive=False
130
- # )
131
-
132
- # # UI Logic
133
- # process_btn.click(
134
- # fn=process_file,
135
- # inputs=[file_input, model_path_input],
136
- # outputs=[json_output, download_output],
137
- # api_name="process_document"
138
- # )
139
-
140
- # if __name__ == "__main__":
141
- # demo.launch(
142
- # server_name="0.0.0.0",
143
- # server_port=7860,
144
- # share=False,
145
- # show_error=True
146
- # )
147
-
148
 
149
 
150
 
@@ -154,6 +7,7 @@ import json
154
  import os
155
  import tempfile
156
  import img2pdf
 
157
  from pathlib import Path
158
 
159
  # ==============================
@@ -213,7 +67,9 @@ def process_file(uploaded_files, layoutlmv3_model_path=None):
213
  print(f"πŸ“¦ Converting {len(resolved_paths)} image(s) to a single PDF...")
214
  temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
215
  with open(temp_pdf.name, "wb") as f_out:
216
- f_out.write(img2pdf.convert(resolved_paths))
 
 
217
  processing_path = temp_pdf.name
218
  else:
219
  # It's a single PDF
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
 
3
 
 
7
  import os
8
  import tempfile
9
  import img2pdf
10
+ from img2pdf import Rotation
11
  from pathlib import Path
12
 
13
  # ==============================
 
67
  print(f"πŸ“¦ Converting {len(resolved_paths)} image(s) to a single PDF...")
68
  temp_pdf = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
69
  with open(temp_pdf.name, "wb") as f_out:
70
+ # f_out.write(img2pdf.convert(resolved_paths))
71
+ f_out.write(img2pdf.convert(resolved_paths, rotation=Rotation.ifvalid))
72
+
73
  processing_path = temp_pdf.name
74
  else:
75
  # It's a single PDF