prithivMLmods commited on
Commit
9a28cdf
·
verified ·
1 Parent(s): bdcf08a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +200 -0
README.md CHANGED
@@ -13,3 +13,203 @@ Last updated: 4:00 AM (IST), October 25, 2025.
13
  > [!note]
14
  The latest transformers version used as of the above date is `transformers==4.57.1` and the torch version `2.8.0+cu126`
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  > [!note]
14
  The latest transformers version used as of the above date is `transformers==4.57.1` and the torch version `2.8.0+cu126`
15
 
16
+ ## Quick Start with Transformers
17
+
18
+ > #### Install the required packages
19
+
20
+ ```py
21
+ !pip install transformers torch torchvision gradio hf_xet \
22
+ huggingface_hub pillow accelerate peft \
23
+ matplotlib requests einops av sentencepiece\
24
+ transformers-stream-generator
25
+ ```
26
+
27
+ > ### notebook login
28
+
29
+ ```py
30
+ from huggingface_hub import notebook_login, HfApi
31
+ notebook_login()
32
+ ```
33
+
34
+ > ### Run [app.py]
35
+
36
+ ```py
37
+ import os
38
+ import sys
39
+ from threading import Thread
40
+ from typing import Iterable
41
+
42
+ import gradio as gr
43
+ import torch
44
+ from PIL import Image
45
+ from transformers import (
46
+ AutoModelForCausalLM,
47
+ AutoProcessor,
48
+ TextIteratorStreamer,
49
+ )
50
+
51
+ from gradio.themes import Soft
52
+ from gradio.themes.utils import colors, fonts, sizes
53
+
54
+ # --- Theme and CSS Setup ---
55
+ colors.steel_blue = colors.Color(
56
+ name="steel_blue",
57
+ c50="#EBF3F8",
58
+ c100="#D3E5F0",
59
+ c200="#A8CCE1",
60
+ c300="#7DB3D2",
61
+ c400="#529AC3",
62
+ c500="#4682B4",
63
+ c600="#3E72A0",
64
+ c700="#36638C",
65
+ c800="#2E5378",
66
+ c900="#264364",
67
+ c950="#1E3450",
68
+ )
69
+
70
+ class SteelBlueTheme(Soft):
71
+ def __init__(
72
+ self,
73
+ *,
74
+ primary_hue: colors.Color | str = colors.gray,
75
+ secondary_hue: colors.Color | str = colors.steel_blue,
76
+ neutral_hue: colors.Color | str = colors.slate,
77
+ text_size: sizes.Size | str = sizes.text_lg,
78
+ font: fonts.Font | str | Iterable[fonts.Font | str] = (
79
+ fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
80
+ ),
81
+ font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
82
+ fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
83
+ ),
84
+ ):
85
+ super().__init__(
86
+ primary_hue=primary_hue,
87
+ secondary_hue=secondary_hue,
88
+ neutral_hue=neutral_hue,
89
+ text_size=text_size,
90
+ font=font,
91
+ font_mono=font_mono,
92
+ )
93
+ super().set(
94
+ background_fill_primary="*primary_50",
95
+ background_fill_primary_dark="*primary_900",
96
+ body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
97
+ body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
98
+ button_primary_text_color="white",
99
+ button_primary_text_color_hover="white",
100
+ button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
101
+ button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
102
+ button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_700)",
103
+ button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_600)",
104
+ slider_color="*secondary_500",
105
+ slider_color_dark="*secondary_600",
106
+ block_title_text_weight="600",
107
+ block_border_width="3px",
108
+ block_shadow="*shadow_drop_lg",
109
+ button_primary_shadow="*shadow_drop_lg",
110
+ button_large_padding="11px",
111
+ color_accent_soft="*primary_100",
112
+ block_label_background_fill="*primary_200",
113
+ )
114
+
115
+ steel_blue_theme = SteelBlueTheme()
116
+
117
+ css = """
118
+ #main-title h1 {
119
+ font-size: 2.3em !important;
120
+ }
121
+ #output-title h2 {
122
+ font-size: 2.1em !important;
123
+ }
124
+ """
125
+
126
+ # --- Model Configuration and Loading ---
127
+ MAX_MAX_NEW_TOKENS = 4096
128
+ DEFAULT_MAX_NEW_TOKENS = 2048
129
+ MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
130
+
131
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
132
+
133
+ # Load PaddleOCR
134
+ MODEL_ID_P = "strangervisionhf/paddle.ocr_path_expose" # -> Original model: https://huggingface.co/PaddlePaddle/PaddleOCR-VL
135
+ processor = AutoProcessor.from_pretrained(MODEL_ID_P, trust_remote_code=True)
136
+ model = AutoModelForCausalLM.from_pretrained(
137
+ MODEL_ID_P,
138
+ trust_remote_code=True,
139
+ torch_dtype=torch.bfloat16
140
+ ).to(device).eval()
141
+
142
+ # --- Generation Function ---
143
+ def generate_image(text: str, image: Image.Image,
144
+ max_new_tokens: int = 1024,
145
+ temperature: float = 0.6,
146
+ top_p: float = 0.9,
147
+ top_k: int = 50,
148
+ repetition_penalty: float = 1.2):
149
+ """Generate responses for image input using the PaddleOCR model."""
150
+ if image is None:
151
+ yield "Please upload an image.", "Please upload an image."
152
+ return
153
+
154
+ images = [image.convert("RGB")]
155
+
156
+ # PaddleOCR has a specific message format
157
+ messages = [
158
+ {"role": "user", "content": text}
159
+ ]
160
+
161
+ prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
162
+ inputs = processor(text=prompt, images=images, return_tensors="pt").to(device)
163
+
164
+ streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
165
+ generation_kwargs = {
166
+ **inputs,
167
+ "streamer": streamer,
168
+ "max_new_tokens": max_new_tokens,
169
+ "temperature": temperature,
170
+ "top_p": top_p,
171
+ "top_k": top_k,
172
+ "repetition_penalty": repetition_penalty,
173
+ "do_sample": True
174
+ }
175
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
176
+ thread.start()
177
+
178
+ buffer = ""
179
+ for new_text in streamer:
180
+ buffer += new_text.replace("<|im_end|>", "").replace("<end_of_utterance>", "")
181
+ yield buffer, buffer
182
+
183
+
184
+ with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
185
+ gr.Markdown("# **Paddle OCR Only**", elem_id="main-title")
186
+ with gr.Row():
187
+ with gr.Column(scale=2):
188
+ image_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
189
+ image_upload = gr.Image(type="pil", label="Upload Image", height=320)
190
+ image_submit = gr.Button("Submit", variant="primary")
191
+
192
+ with gr.Accordion("Advanced options", open=False):
193
+ max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
194
+ temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
195
+ top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
196
+ top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
197
+ repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
198
+
199
+ with gr.Column(scale=3):
200
+ gr.Markdown("## Output", elem_id="output-title")
201
+ raw_output = gr.Textbox(label="Raw Output Stream", interactive=False, lines=11, show_copy_button=True)
202
+ with gr.Accordion("[Result.md]", open=False):
203
+ formatted_output = gr.Markdown(label="Formatted Result")
204
+
205
+ gr.Markdown("Note: Currently, PaddleOCR VL only supports OCR inference. Structured OCR document parsing transformer inference is coming soon. [Report – Bug/Issue](https://huggingface.co/spaces/prithivMLmods/Multimodal-OCR3/discussions/1)")
206
+
207
+ image_submit.click(
208
+ fn=generate_image,
209
+ inputs=[image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
210
+ outputs=[raw_output, formatted_output]
211
+ )
212
+
213
+ if __name__ == "__main__":
214
+ demo.queue(max_size=50).launch(mcp_server=True, ssr_mode=False, show_error=True)
215
+ ```