Update app.py
Browse files
app.py
CHANGED
|
@@ -56,7 +56,7 @@ model_v = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
|
| 56 |
torch_dtype=torch.float16
|
| 57 |
).to(device).eval()
|
| 58 |
|
| 59 |
-
# Load
|
| 60 |
MODEL_ID_A = "CohereForAI/aya-vision-8b"
|
| 61 |
processor_a = AutoProcessor.from_pretrained(MODEL_ID_A, trust_remote_code=True)
|
| 62 |
model_a = AutoModelForImageTextToText.from_pretrained(
|
|
@@ -95,6 +95,7 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
| 95 |
repetition_penalty: float = 1.2):
|
| 96 |
"""
|
| 97 |
Generates responses using the selected model for image input.
|
|
|
|
| 98 |
"""
|
| 99 |
if model_name == "RolmOCR":
|
| 100 |
processor = processor_m
|
|
@@ -109,11 +110,11 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
| 109 |
processor = processor_a
|
| 110 |
model = model_a
|
| 111 |
else:
|
| 112 |
-
yield "Invalid model selected."
|
| 113 |
return
|
| 114 |
|
| 115 |
if image is None:
|
| 116 |
-
yield "Please upload an image."
|
| 117 |
return
|
| 118 |
|
| 119 |
messages = [{
|
|
@@ -141,7 +142,7 @@ def generate_image(model_name: str, text: str, image: Image.Image,
|
|
| 141 |
buffer += new_text
|
| 142 |
buffer = buffer.replace("<|im_end|>", "")
|
| 143 |
time.sleep(0.01)
|
| 144 |
-
yield buffer
|
| 145 |
|
| 146 |
@spaces.GPU
|
| 147 |
def generate_video(model_name: str, text: str, video_path: str,
|
|
@@ -152,6 +153,7 @@ def generate_video(model_name: str, text: str, video_path: str,
|
|
| 152 |
repetition_penalty: float = 1.2):
|
| 153 |
"""
|
| 154 |
Generates responses using the selected model for video input.
|
|
|
|
| 155 |
"""
|
| 156 |
if model_name == "RolmOCR":
|
| 157 |
processor = processor_m
|
|
@@ -166,11 +168,11 @@ def generate_video(model_name: str, text: str, video_path: str,
|
|
| 166 |
processor = processor_a
|
| 167 |
model = model_a
|
| 168 |
else:
|
| 169 |
-
yield "Invalid model selected."
|
| 170 |
return
|
| 171 |
|
| 172 |
if video_path is None:
|
| 173 |
-
yield "Please upload a video."
|
| 174 |
return
|
| 175 |
|
| 176 |
frames = downsample_video(video_path)
|
|
@@ -209,7 +211,7 @@ def generate_video(model_name: str, text: str, video_path: str,
|
|
| 209 |
buffer += new_text
|
| 210 |
buffer = buffer.replace("<|im_end|>", "")
|
| 211 |
time.sleep(0.01)
|
| 212 |
-
yield buffer
|
| 213 |
|
| 214 |
# Define examples for image and video inference
|
| 215 |
image_examples = [
|
|
@@ -261,28 +263,32 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
|
|
| 261 |
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 262 |
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 263 |
with gr.Column():
|
| 264 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
model_choice = gr.Radio(
|
| 266 |
choices=["Nanonets-OCR-s", "Qwen2-VL-OCR-2B-Instruct", "RolmOCR", "Aya-Vision"],
|
| 267 |
label="Select Model",
|
| 268 |
value="Nanonets-OCR-s"
|
| 269 |
)
|
| 270 |
-
|
| 271 |
-
gr.Markdown("**Model Info**")
|
| 272 |
gr.Markdown("> [Qwen2-VL-OCR-2B-Instruct](https://huggingface.co/prithivMLmods/Qwen2-VL-OCR-2B-Instruct): qwen2-vl-ocr-2b-instruct model is a fine-tuned version of qwen2-vl-2b-instruct, tailored for tasks that involve [messy] optical character recognition (ocr), image-to-text conversion, and math problem solving with latex formatting.")
|
| 273 |
gr.Markdown("> [Nanonets-OCR-s](https://huggingface.co/nanonets/Nanonets-OCR-s): nanonets-ocr-s is a powerful, state-of-the-art image-to-markdown ocr model that goes far beyond traditional text extraction. it transforms documents into structured markdown with intelligent content recognition and semantic tagging.")
|
| 274 |
-
gr.Markdown("> [RolmOCR](https://huggingface.co/reducto/RolmOCR): rolmocr, high-quality, openly available approach to parsing pdfs and other complex documents
|
| 275 |
gr.Markdown("> [Aya-Vision](https://huggingface.co/CohereLabs/aya-vision-8b): cohere labs aya vision 8b is an open weights research release of an 8-billion parameter model with advanced capabilities optimized for a variety of vision-language use cases, including ocr, captioning, visual reasoning, summarization, question answering, code, and more.")
|
| 276 |
-
|
|
|
|
| 277 |
image_submit.click(
|
| 278 |
fn=generate_image,
|
| 279 |
inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
| 280 |
-
outputs=output
|
| 281 |
)
|
| 282 |
video_submit.click(
|
| 283 |
fn=generate_video,
|
| 284 |
inputs=[model_choice, video_query, video_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
| 285 |
-
outputs=output
|
| 286 |
)
|
| 287 |
|
| 288 |
if __name__ == "__main__":
|
|
|
|
| 56 |
torch_dtype=torch.float16
|
| 57 |
).to(device).eval()
|
| 58 |
|
| 59 |
+
# Load Aya-Vision-8b
|
| 60 |
MODEL_ID_A = "CohereForAI/aya-vision-8b"
|
| 61 |
processor_a = AutoProcessor.from_pretrained(MODEL_ID_A, trust_remote_code=True)
|
| 62 |
model_a = AutoModelForImageTextToText.from_pretrained(
|
|
|
|
| 95 |
repetition_penalty: float = 1.2):
|
| 96 |
"""
|
| 97 |
Generates responses using the selected model for image input.
|
| 98 |
+
Yields raw text and Markdown-formatted text.
|
| 99 |
"""
|
| 100 |
if model_name == "RolmOCR":
|
| 101 |
processor = processor_m
|
|
|
|
| 110 |
processor = processor_a
|
| 111 |
model = model_a
|
| 112 |
else:
|
| 113 |
+
yield "Invalid model selected.", "Invalid model selected."
|
| 114 |
return
|
| 115 |
|
| 116 |
if image is None:
|
| 117 |
+
yield "Please upload an image.", "Please upload an image."
|
| 118 |
return
|
| 119 |
|
| 120 |
messages = [{
|
|
|
|
| 142 |
buffer += new_text
|
| 143 |
buffer = buffer.replace("<|im_end|>", "")
|
| 144 |
time.sleep(0.01)
|
| 145 |
+
yield buffer, buffer
|
| 146 |
|
| 147 |
@spaces.GPU
|
| 148 |
def generate_video(model_name: str, text: str, video_path: str,
|
|
|
|
| 153 |
repetition_penalty: float = 1.2):
|
| 154 |
"""
|
| 155 |
Generates responses using the selected model for video input.
|
| 156 |
+
Yields raw text and Markdown-formatted text.
|
| 157 |
"""
|
| 158 |
if model_name == "RolmOCR":
|
| 159 |
processor = processor_m
|
|
|
|
| 168 |
processor = processor_a
|
| 169 |
model = model_a
|
| 170 |
else:
|
| 171 |
+
yield "Invalid model selected.", "Invalid model selected."
|
| 172 |
return
|
| 173 |
|
| 174 |
if video_path is None:
|
| 175 |
+
yield "Please upload a video.", "Please upload a video."
|
| 176 |
return
|
| 177 |
|
| 178 |
frames = downsample_video(video_path)
|
|
|
|
| 211 |
buffer += new_text
|
| 212 |
buffer = buffer.replace("<|im_end|>", "")
|
| 213 |
time.sleep(0.01)
|
| 214 |
+
yield buffer, buffer
|
| 215 |
|
| 216 |
# Define examples for image and video inference
|
| 217 |
image_examples = [
|
|
|
|
| 263 |
top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
|
| 264 |
repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
|
| 265 |
with gr.Column():
|
| 266 |
+
gr.Markdown("## Result.Md")
|
| 267 |
+
output = gr.Textbox(label="Raw Output Stream", interactive=False, lines=2)
|
| 268 |
+
#format[ft.md]
|
| 269 |
+
with gr.Accordion("Formatted Result (Result.md)", open=False):
|
| 270 |
+
markdown_output = gr.Markdown(label="Formatted Result (Result.Md)")
|
| 271 |
model_choice = gr.Radio(
|
| 272 |
choices=["Nanonets-OCR-s", "Qwen2-VL-OCR-2B-Instruct", "RolmOCR", "Aya-Vision"],
|
| 273 |
label="Select Model",
|
| 274 |
value="Nanonets-OCR-s"
|
| 275 |
)
|
| 276 |
+
gr.Markdown("**Model Info 💻** | [Report Bug](https://huggingface.co/spaces/prithivMLmods/Multimodal-OCR/discussions)")
|
|
|
|
| 277 |
gr.Markdown("> [Qwen2-VL-OCR-2B-Instruct](https://huggingface.co/prithivMLmods/Qwen2-VL-OCR-2B-Instruct): qwen2-vl-ocr-2b-instruct model is a fine-tuned version of qwen2-vl-2b-instruct, tailored for tasks that involve [messy] optical character recognition (ocr), image-to-text conversion, and math problem solving with latex formatting.")
|
| 278 |
gr.Markdown("> [Nanonets-OCR-s](https://huggingface.co/nanonets/Nanonets-OCR-s): nanonets-ocr-s is a powerful, state-of-the-art image-to-markdown ocr model that goes far beyond traditional text extraction. it transforms documents into structured markdown with intelligent content recognition and semantic tagging.")
|
| 279 |
+
gr.Markdown("> [RolmOCR](https://huggingface.co/reducto/RolmOCR): rolmocr, high-quality, openly available approach to parsing pdfs and other complex documents optical character recognition. it is designed to handle a wide range of document types, including scanned documents, handwritten text, and complex layouts.")
|
| 280 |
gr.Markdown("> [Aya-Vision](https://huggingface.co/CohereLabs/aya-vision-8b): cohere labs aya vision 8b is an open weights research release of an 8-billion parameter model with advanced capabilities optimized for a variety of vision-language use cases, including ocr, captioning, visual reasoning, summarization, question answering, code, and more.")
|
| 281 |
+
gr.Markdown(">⚠️note: all the models in space are not guaranteed to perform well in video inference use cases.")
|
| 282 |
+
|
| 283 |
image_submit.click(
|
| 284 |
fn=generate_image,
|
| 285 |
inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
| 286 |
+
outputs=[output, markdown_output]
|
| 287 |
)
|
| 288 |
video_submit.click(
|
| 289 |
fn=generate_video,
|
| 290 |
inputs=[model_choice, video_query, video_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
|
| 291 |
+
outputs=[output, markdown_output]
|
| 292 |
)
|
| 293 |
|
| 294 |
if __name__ == "__main__":
|