prithivMLmods commited on
Commit
ff82d30
·
verified ·
1 Parent(s): 1d278fb

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -307
app.py DELETED
@@ -1,307 +0,0 @@
1
- import spaces
2
- import json
3
- import math
4
- import os
5
- import traceback
6
- from io import BytesIO
7
- from typing import Any, Dict, List, Optional, Tuple
8
- import re
9
- import time
10
- from threading import Thread
11
- from io import BytesIO
12
- import uuid
13
- import tempfile
14
-
15
- import gradio as gr
16
- import requests
17
- import torch
18
- from PIL import Image
19
- import fitz
20
-
21
- from transformers import (
22
- Qwen2VLForConditionalGeneration,
23
- AutoModelForVision2Seq,
24
- AutoModelForImageTextToText,
25
- AutoModel,
26
- AutoProcessor,
27
- TextIteratorStreamer,
28
- )
29
-
30
- from transformers.image_utils import load_image
31
-
32
- from reportlab.lib.pagesizes import A4
33
- from reportlab.lib.styles import getSampleStyleSheet
34
- from reportlab.platypus import SimpleDocTemplate, Image as RLImage, Paragraph, Spacer
35
- from reportlab.lib.units import inch
36
-
37
- # --- Constants and Model Setup ---
38
- MAX_INPUT_TOKEN_LENGTH = 4096
39
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
40
-
41
- print("CUDA_VISIBLE_DEVICES=", os.environ.get("CUDA_VISIBLE_DEVICES"))
42
- print("torch.__version__ =", torch.__version__)
43
- print("torch.version.cuda =", torch.version.cuda)
44
- print("cuda available:", torch.cuda.is_available())
45
- print("cuda device count:", torch.cuda.device_count())
46
- if torch.cuda.is_available():
47
- print("current device:", torch.cuda.current_device())
48
- print("device name:", torch.cuda.get_device_name(torch.cuda.current_device()))
49
-
50
- print("Using device:", device)
51
-
52
- # --- Model Loading ---
53
- MODEL_ID_M = "LiquidAI/LFM2-VL-450M"
54
- processor_m = AutoProcessor.from_pretrained(MODEL_ID_M, trust_remote_code=True)
55
- model_m = AutoModelForImageTextToText.from_pretrained(
56
- MODEL_ID_M, trust_remote_code=True, torch_dtype=torch.float16
57
- ).to(device).eval()
58
-
59
- MODEL_ID_T = "LiquidAI/LFM2-VL-1.6B"
60
- processor_t = AutoProcessor.from_pretrained(MODEL_ID_T, trust_remote_code=True)
61
- model_t = AutoModelForImageTextToText.from_pretrained(
62
- MODEL_ID_T, trust_remote_code=True, torch_dtype=torch.float16
63
- ).to(device).eval()
64
-
65
- MODEL_ID_C = "HuggingFaceTB/SmolVLM-Instruct-250M"
66
- processor_c = AutoProcessor.from_pretrained(MODEL_ID_C, trust_remote_code=True)
67
- model_c = AutoModelForVision2Seq.from_pretrained(
68
- MODEL_ID_C, trust_remote_code=True, torch_dtype=torch.float16, _attn_implementation="flash_attention_2"
69
- ).to(device).eval()
70
-
71
- MODEL_ID_G = "echo840/MonkeyOCR-pro-1.2B"
72
- SUBFOLDER = "Recognition"
73
- processor_g = AutoProcessor.from_pretrained(
74
- MODEL_ID_G, trust_remote_code=True, subfolder=SUBFOLDER
75
- )
76
- model_g = Qwen2VLForConditionalGeneration.from_pretrained(
77
- MODEL_ID_G, trust_remote_code=True, subfolder=SUBFOLDER, torch_dtype=torch.float16
78
- ).to(device).eval()
79
-
80
- MODEL_ID_I = "HuggingFaceTB/SmolVLM2-2.2B-Instruct"
81
- processor_i = AutoProcessor.from_pretrained(MODEL_ID_I, trust_remote_code=True)
82
- model_i = AutoModelForImageTextToText.from_pretrained(
83
- MODEL_ID_I, trust_remote_code=True, torch_dtype=torch.bfloat16, _attn_implementation="flash_attention_2"
84
- ).to(device).eval()
85
-
86
-
87
- # --- PDF Generation and Preview Utility Function ---
88
- def generate_and_preview_pdf(image: Image.Image, text_content: str, font_size: int, line_spacing: float, alignment: str, image_size: str):
89
- """
90
- Generates a PDF, saves it, and then creates image previews of its pages.
91
- Returns the path to the PDF and a list of paths to the preview images.
92
- """
93
- if image is None or not text_content or not text_content.strip():
94
- raise gr.Error("Cannot generate PDF. Image or text content is missing.")
95
-
96
- # --- 1. Generate the PDF ---
97
- temp_dir = tempfile.gettempdir()
98
- pdf_filename = os.path.join(temp_dir, f"output_{uuid.uuid4()}.pdf")
99
- doc = SimpleDocTemplate(
100
- pdf_filename,
101
- pagesize=A4,
102
- rightMargin=inch, leftMargin=inch,
103
- topMargin=inch, bottomMargin=inch
104
- )
105
- styles = getSampleStyleSheet()
106
- style_normal = styles["Normal"]
107
- style_normal.fontSize = int(font_size)
108
- style_normal.leading = int(font_size) * line_spacing
109
- style_normal.alignment = {"Left": 0, "Center": 1, "Right": 2, "Justified": 4}[alignment]
110
-
111
- story = []
112
-
113
- img_buffer = BytesIO()
114
- image.save(img_buffer, format='PNG')
115
- img_buffer.seek(0)
116
-
117
- page_width, _ = A4
118
- available_width = page_width - 2 * inch
119
- image_widths = {
120
- "Small": available_width * 0.3,
121
- "Medium": available_width * 0.6,
122
- "Large": available_width * 0.9,
123
- }
124
- img_width = image_widths[image_size]
125
- img = RLImage(img_buffer, width=img_width, height=image.height * (img_width / image.width))
126
- story.append(img)
127
- story.append(Spacer(1, 12))
128
-
129
- cleaned_text = re.sub(r'#+\s*', '', text_content).replace("*", "")
130
- text_paragraphs = cleaned_text.split('\n')
131
-
132
- for para in text_paragraphs:
133
- if para.strip():
134
- story.append(Paragraph(para, style_normal))
135
-
136
- doc.build(story)
137
-
138
- # --- 2. Render PDF pages as images for preview ---
139
- preview_images = []
140
- try:
141
- pdf_doc = fitz.open(pdf_filename)
142
- for page_num in range(len(pdf_doc)):
143
- page = pdf_doc.load_page(page_num)
144
- pix = page.get_pixmap(dpi=150)
145
- preview_img_path = os.path.join(temp_dir, f"preview_{uuid.uuid4()}_p{page_num}.png")
146
- pix.save(preview_img_path)
147
- preview_images.append(preview_img_path)
148
- pdf_doc.close()
149
- except Exception as e:
150
- print(f"Error generating PDF preview: {e}")
151
-
152
- return pdf_filename, preview_images
153
-
154
-
155
- # --- Core Application Logic ---
156
- @spaces.GPU
157
- def process_document_stream(
158
- model_name: str,
159
- image: Image.Image,
160
- prompt_input: str,
161
- max_new_tokens: int,
162
- temperature: float,
163
- top_p: float,
164
- top_k: int,
165
- repetition_penalty: float
166
- ):
167
- """
168
- Main generator function that handles model inference tasks with advanced generation parameters.
169
- """
170
- if image is None:
171
- yield "Please upload an image.", ""
172
- return
173
- if not prompt_input or not prompt_input.strip():
174
- yield "Please enter a prompt.", ""
175
- return
176
-
177
- if model_name == "LFM2-VL-450M": processor, model = processor_m, model_m
178
- elif model_name == "LFM2-VL-1.6B": processor, model = processor_t, model_t
179
- elif model_name == "SmolVLM-Instruct-250M": processor, model = processor_c, model_c
180
- elif model_name == "MonkeyOCR-pro-1.2B": processor, model = processor_g, model_g
181
- elif model_name == "SmolVLM2-2.2B-Instruct": processor, model = processor_i, model_i
182
- else:
183
- yield "Invalid model selected.", ""
184
- return
185
-
186
- messages = [{"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": prompt_input}]}]
187
- prompt_full = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
188
- inputs = processor(text=[prompt_full], images=[image], return_tensors="pt", padding=True, truncation=True, max_length=MAX_INPUT_TOKEN_LENGTH).to(device)
189
- # Convert floating point tensors to the model's dtype
190
- inputs = {k: v.to(dtype=model.dtype) if isinstance(v, torch.Tensor) and v.dtype.is_floating_point else v for k, v in inputs.items()}
191
- streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
192
-
193
- generation_kwargs = {
194
- **inputs,
195
- "streamer": streamer,
196
- "max_new_tokens": max_new_tokens,
197
- "temperature": temperature,
198
- "top_p": top_p,
199
- "top_k": top_k,
200
- "repetition_penalty": repetition_penalty,
201
- "do_sample": True
202
- }
203
-
204
- thread = Thread(target=model.generate, kwargs=generation_kwargs)
205
- thread.start()
206
-
207
- buffer = ""
208
- for new_text in streamer:
209
- buffer += new_text
210
- buffer = buffer.replace("<|im_end|>", "")
211
- time.sleep(0.01)
212
- yield buffer , buffer
213
-
214
- yield buffer, buffer
215
-
216
-
217
- # --- Gradio UI Definition ---
218
- def create_gradio_interface():
219
- """Builds and returns the Gradio web interface."""
220
- css = """
221
- .main-container { max-width: 1400px; margin: 0 auto; }
222
- .process-button { border: none !important; color: white !important; font-weight: bold !important; background-color: blue !important;}
223
- .process-button:hover { background-color: darkblue !important; transform: translateY(-2px) !important; box-shadow: 0 4px 8px rgba(0,0,0,0.2) !important; }
224
- #gallery { min-height: 400px; }
225
- """
226
- with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
227
- gr.HTML("""
228
- <div class="title" style="text-align: center">
229
- <h1>Tiny VLMs Lab🧪</h1>
230
- <p style="font-size: 1.1em; color: #6b7280; margin-bottom: 0.6em;">
231
- Advanced Vision-Language Model for Image Content and Layout Extraction
232
- </p>
233
- </div>
234
- """)
235
-
236
- with gr.Row():
237
- # Left Column (Inputs)
238
- with gr.Column(scale=1):
239
- model_choice = gr.Dropdown(
240
- choices=["LFM2-VL-1.6B", "LFM2-VL-450M", "SmolVLM-Instruct-250M", "SmolVLM2-2.2B-Instruct", "MonkeyOCR-pro-1.2B"],
241
- label="Select Model", value="LFM2-VL-1.6B"
242
- )
243
- prompt_input = gr.Textbox(label="Query Input", placeholder="✦︎ Enter your query")
244
- image_input = gr.Image(label="Upload Image", type="pil", sources=['upload'])
245
-
246
- with gr.Accordion("Advanced Settings", open=False):
247
- max_new_tokens = gr.Slider(minimum=512, maximum=8192, value=4096, step=256, label="Max New Tokens")
248
- temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
249
- top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
250
- top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
251
- repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
252
-
253
- gr.Markdown("### PDF Export Settings")
254
- font_size = gr.Dropdown(choices=["8", "10", "12", "14", "16", "18"], value="12", label="Font Size")
255
- line_spacing = gr.Dropdown(choices=[1.0, 1.15, 1.5, 2.0], value=1.15, label="Line Spacing")
256
- alignment = gr.Dropdown(choices=["Left", "Center", "Right", "Justified"], value="Justified", label="Text Alignment")
257
- image_size = gr.Dropdown(choices=["Small", "Medium", "Large"], value="Medium", label="Image Size in PDF")
258
-
259
- process_btn = gr.Button("🚀 Process Image", variant="primary", elem_classes=["process-button"], size="lg")
260
- clear_btn = gr.Button("🗑️ Clear All", variant="secondary")
261
-
262
- # Right Column (Outputs)
263
- with gr.Column(scale=2):
264
- with gr.Tabs() as tabs:
265
- with gr.Tab("📝 Extracted Content"):
266
- raw_output_stream = gr.Textbox(label="Raw Model Output Stream", interactive=False, lines=15, show_copy_button=True)
267
- with gr.Row():
268
- examples = gr.Examples(
269
- examples=["examples/1.png", "examples/2.png", "examples/3.png", "examples/4.png", "examples/5.png"],
270
- inputs=image_input, label="Examples"
271
- )
272
- gr.Markdown("[Report-Bug💻](https://huggingface.co/spaces/prithivMLmods/OCR-Comparator/discussions)")
273
-
274
- with gr.Tab("📰 README.md"):
275
- with gr.Accordion("(Result.md)", open=True):
276
- markdown_output = gr.Markdown()
277
-
278
- with gr.Tab("📋 PDF Preview"):
279
- generate_pdf_btn = gr.Button("📄 Generate PDF & Render", variant="primary")
280
- pdf_output_file = gr.File(label="Download Generated PDF", interactive=False)
281
- pdf_preview_gallery = gr.Gallery(label="PDF Page Preview", show_label=True, elem_id="gallery", columns=2, object_fit="contain", height="auto")
282
-
283
- # Event Handlers
284
- def clear_all_outputs():
285
- return None, "", "Raw output will appear here.", "", None, None
286
-
287
- process_btn.click(
288
- fn=process_document_stream,
289
- inputs=[model_choice, image_input, prompt_input, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
290
- outputs=[raw_output_stream, markdown_output]
291
- )
292
-
293
- generate_pdf_btn.click(
294
- fn=generate_and_preview_pdf,
295
- inputs=[image_input, raw_output_stream, font_size, line_spacing, alignment, image_size],
296
- outputs=[pdf_output_file, pdf_preview_gallery]
297
- )
298
-
299
- clear_btn.click(
300
- clear_all_outputs,
301
- outputs=[image_input, prompt_input, raw_output_stream, markdown_output, pdf_output_file, pdf_preview_gallery]
302
- )
303
- return demo
304
-
305
- if __name__ == "__main__":
306
- demo = create_gradio_interface()
307
- demo.queue(max_size=50).launch(share=True, ssr_mode=False, show_error=True)