Upload 5 files
Browse files- .gitattributes +2 -0
- app.py +199 -0
- example1.png +3 -0
- example2.png +3 -0
- main_ocr_T4.py +105 -0
- requirements.txt +17 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
example1.png filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
example2.png filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from main import process_image_ocr
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# CSS for better Persian styling
|
| 7 |
+
custom_css = """
|
| 8 |
+
.persian-text {
|
| 9 |
+
font-family: "Vazirmatn", "Tahoma", "Arial", sans-serif;
|
| 10 |
+
direction: rtl;
|
| 11 |
+
}
|
| 12 |
+
.rtl-direction {
|
| 13 |
+
direction: rtl;
|
| 14 |
+
text-align: right;
|
| 15 |
+
}
|
| 16 |
+
.center-content {
|
| 17 |
+
display: flex;
|
| 18 |
+
justify-content: center;
|
| 19 |
+
align-items: center;
|
| 20 |
+
}
|
| 21 |
+
.progress-text {
|
| 22 |
+
text-align: center;
|
| 23 |
+
font-weight: bold;
|
| 24 |
+
margin: 10px 0;
|
| 25 |
+
}
|
| 26 |
+
.markdown-output {
|
| 27 |
+
min-height: 400px;
|
| 28 |
+
border: 1px solid #e0e0e0;
|
| 29 |
+
padding: 15px;
|
| 30 |
+
border-radius: 8px;
|
| 31 |
+
}
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
def process_image_with_progress(image, model_size, task_type):
|
| 35 |
+
"""
|
| 36 |
+
تابع پردازش تصویر با نوار پیشرفت
|
| 37 |
+
"""
|
| 38 |
+
progress = gr.Progress()
|
| 39 |
+
|
| 40 |
+
# شبیهسازی مراحل پیشرفت
|
| 41 |
+
progress(0, desc="در حال آمادهسازی مدل...")
|
| 42 |
+
time.sleep(0.5)
|
| 43 |
+
|
| 44 |
+
progress(0.3, desc="در حال پردازش تصویر...")
|
| 45 |
+
time.sleep(0.5)
|
| 46 |
+
|
| 47 |
+
progress(0.6, desc="در حال استخراج متن...")
|
| 48 |
+
time.sleep(0.5)
|
| 49 |
+
|
| 50 |
+
progress(0.8, desc="در حال تولید خروجی...")
|
| 51 |
+
|
| 52 |
+
# پردازش اصلی
|
| 53 |
+
result_image, markdown_content, text_result = process_image_ocr(
|
| 54 |
+
image, model_size, task_type, is_eval_mode=False
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
progress(1.0, desc="پردازش کامل شد!")
|
| 58 |
+
|
| 59 |
+
return markdown_content, text_result
|
| 60 |
+
|
| 61 |
+
# ایجاد رابط Gradio بهبود یافته
|
| 62 |
+
with gr.Blocks(
|
| 63 |
+
title=" OCR استخراج متن از تصویر",
|
| 64 |
+
theme=gr.themes.Soft(primary_hue="blue", secondary_hue="teal"),
|
| 65 |
+
css=custom_css
|
| 66 |
+
) as demo:
|
| 67 |
+
|
| 68 |
+
# هدر اصلی
|
| 69 |
+
with gr.Row():
|
| 70 |
+
with gr.Column(scale=1):
|
| 71 |
+
gr.HTML(
|
| 72 |
+
"""
|
| 73 |
+
<div class="persian-text" style="text-align: center;">
|
| 74 |
+
<h1>🧠 پردازش هوشمند تصویر-OCR</h1>
|
| 75 |
+
<h3>استخراج هوشمند متن از تصاویر</h3>
|
| 76 |
+
<p>تصویر خود را آپلود کنید تا متن آن به صورت خودکار استخراج شود</p>
|
| 77 |
+
</div>
|
| 78 |
+
"""
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
# پنل ورودیها
|
| 83 |
+
with gr.Column(scale=1, min_width=400):
|
| 84 |
+
with gr.Group():
|
| 85 |
+
gr.Markdown("### ⚙️ تنظیمات پردازش", elem_classes="persian-text")
|
| 86 |
+
|
| 87 |
+
image_input = gr.Image(
|
| 88 |
+
type="pil",
|
| 89 |
+
label="📷 تصویر ورودی",
|
| 90 |
+
sources=["upload", "clipboard"],
|
| 91 |
+
height=300,
|
| 92 |
+
elem_classes="rtl-direction"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
model_size = gr.Dropdown(
|
| 96 |
+
choices=["کوچک", "پایه (توصیه شده)", "بزرگ"],
|
| 97 |
+
value="پایه (توصیه شده)",
|
| 98 |
+
label="📊 اندازه مدل",
|
| 99 |
+
info="مدل بزرگتر دقت بهتر اما سرعت کمتر",
|
| 100 |
+
elem_classes="rtl-direction"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
task_type = gr.Dropdown(
|
| 104 |
+
choices=["OCR", "تبدیل به Markdown"],
|
| 105 |
+
value="OCR",
|
| 106 |
+
label="🎯 نوع وظیفه",
|
| 107 |
+
info="OCR: فقط استخراج متن | Markdown: ساختاردهی پیشرفته",
|
| 108 |
+
elem_classes="rtl-direction"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
with gr.Row():
|
| 112 |
+
clear_btn = gr.Button("🗑️ پاک کردن", size="sm")
|
| 113 |
+
submit_btn = gr.Button("🚀 شروع پردازش", variant="primary", size="lg")
|
| 114 |
+
|
| 115 |
+
# پنل خروجیها
|
| 116 |
+
with gr.Column(scale=2, min_width=600):
|
| 117 |
+
with gr.Tabs() as tabs:
|
| 118 |
+
# تب پیشنمایش Markdown
|
| 119 |
+
with gr.TabItem("📝 پیش نمایش", id=1):
|
| 120 |
+
gr.Markdown("**خروجی قالب بندی شده:**", elem_classes="persian-text")
|
| 121 |
+
output_markdown = gr.Markdown(
|
| 122 |
+
elem_classes=["persian-text", "markdown-output"],
|
| 123 |
+
value="خروجی اینجا نمایش داده میشود..."
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# تب متن خام
|
| 127 |
+
with gr.TabItem("📄 متن خام", id=2):
|
| 128 |
+
output_text = gr.Textbox(
|
| 129 |
+
lines=20,
|
| 130 |
+
show_copy_button=True,
|
| 131 |
+
label="متن استخراج شده",
|
| 132 |
+
elem_classes="rtl-direction",
|
| 133 |
+
value="متن استخراج شده در اینجا نمایش داده میشود..."
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
# بخش اطلاعات و راهنما
|
| 137 |
+
with gr.Accordion("ℹ️ راهنمای استفاده", open=False):
|
| 138 |
+
gr.Markdown("""
|
| 139 |
+
**راهنمای سریع:**
|
| 140 |
+
|
| 141 |
+
- **تصویر با کیفیت بالا** آپلود کنید
|
| 142 |
+
- برای **اسناد متنی** از حالت 'پایه' استفاده کنید
|
| 143 |
+
- برای **تصاویر پیچیده** از حالت 'بزرگ' استفاده کنید
|
| 144 |
+
- حالت **Markdown** برای اسناد ساختاریافته مناسب است
|
| 145 |
+
|
| 146 |
+
**نکات:**
|
| 147 |
+
- فرمتهای پشتیبانی شده: JPG, PNG, WebP
|
| 148 |
+
- حداکثر حجم تصویر: 10MB
|
| 149 |
+
- پردازش ممکن است 10-30 ثانیه زمان ببرد
|
| 150 |
+
""", elem_classes="persian-text")
|
| 151 |
+
|
| 152 |
+
# بخش مثالها
|
| 153 |
+
with gr.Row():
|
| 154 |
+
with gr.Column():
|
| 155 |
+
gr.Markdown("### 📁 مثالهای آماده", elem_classes="persian-text")
|
| 156 |
+
gr.Examples(
|
| 157 |
+
examples=[
|
| 158 |
+
["example1.png", "پایه (توصیه شده)", "OCR"],
|
| 159 |
+
["example2.png", "پایه (توصیه شده)", "تبدیل به Markdown"],
|
| 160 |
+
],
|
| 161 |
+
inputs=[image_input, model_size, task_type],
|
| 162 |
+
outputs=[output_markdown, output_text],
|
| 163 |
+
fn=process_image_with_progress,
|
| 164 |
+
cache_examples=False,
|
| 165 |
+
label="برای تست سریع روی یکی از مثالها کلیک کنید",
|
| 166 |
+
examples_per_page=3
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
# وضعیت سیستم
|
| 170 |
+
with gr.Row():
|
| 171 |
+
gr.HTML("""
|
| 172 |
+
<div class="persian-text" style="text-align: center; color: #666; font-size: 0.9em; margin-top: 20px;">
|
| 173 |
+
<p>ساخته شده توسط *سامان زیتونیان* | OCR | پردازش تصویر هوشمند</p>
|
| 174 |
+
</div>
|
| 175 |
+
""")
|
| 176 |
+
|
| 177 |
+
# مدیریت رویدادها
|
| 178 |
+
def clear_all():
|
| 179 |
+
return None, "خروجی اینجا نمایش داده میشود...", "متن استخراج شده در اینجا نمایش داده میشود..."
|
| 180 |
+
|
| 181 |
+
# اتصال دکمهها
|
| 182 |
+
submit_btn.click(
|
| 183 |
+
fn=process_image_with_progress,
|
| 184 |
+
inputs=[image_input, model_size, task_type],
|
| 185 |
+
outputs=[output_markdown, output_text],
|
| 186 |
+
show_progress="minimal"
|
| 187 |
+
)
|
| 188 |
+
|
| 189 |
+
clear_btn.click(
|
| 190 |
+
fn=clear_all,
|
| 191 |
+
outputs=[image_input, output_markdown, output_text]
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
# راهاندازی برنامه
|
| 195 |
+
if __name__ == "__main__":
|
| 196 |
+
demo.launch(
|
| 197 |
+
share=True,
|
| 198 |
+
show_error=True
|
| 199 |
+
)
|
example1.png
ADDED
|
Git LFS Details
|
example2.png
ADDED
|
Git LFS Details
|
main_ocr_T4.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModel, AutoTokenizer
|
| 3 |
+
import os
|
| 4 |
+
import tempfile
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Load model and tokenizer
|
| 9 |
+
model_name = "deepseek-ai/DeepSeek-OCR"
|
| 10 |
+
try:
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 12 |
+
model = AutoModel.from_pretrained(
|
| 13 |
+
model_name,
|
| 14 |
+
_attn_implementation="sdpa",
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
+
device_map="auto",
|
| 17 |
+
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
| 18 |
+
)
|
| 19 |
+
model = model.eval()
|
| 20 |
+
print("✅ مدل با موفقیت بارگذاری شد!")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
print(f"❌ خطا در بارگذاری مدل: {e}")
|
| 23 |
+
raise
|
| 24 |
+
|
| 25 |
+
def process_image_ocr(image, model_size, task_type, is_eval_mode=False, progress=gr.Progress()):
|
| 26 |
+
"""
|
| 27 |
+
پردازش تصاویر برای وظایف OCR و Markdown با پشتیبانی از پیشرفت
|
| 28 |
+
"""
|
| 29 |
+
if image is None:
|
| 30 |
+
return None, "لطفا ابتدا یک تصویر آپلود کنید.", "لطفا ابتدا یک تصویر آپلود کنید."
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# بهروزرسانی پیشرفت
|
| 34 |
+
if progress is not None:
|
| 35 |
+
progress(0.1, desc="در حال آمادهسازی محیط پردازش...")
|
| 36 |
+
|
| 37 |
+
# ایجاد دایرکتوری موقت برای خروجی
|
| 38 |
+
with tempfile.TemporaryDirectory() as output_path:
|
| 39 |
+
# تنظیم prompt بر اساس نوع وظیفه
|
| 40 |
+
if task_type == "OCR":
|
| 41 |
+
prompt = "<image>\nFree OCR. "
|
| 42 |
+
elif task_type == "تبدیل به Markdown":
|
| 43 |
+
prompt = "<image>\n<|grounding|>Convert the document to markdown. "
|
| 44 |
+
else:
|
| 45 |
+
prompt = "<image>\nFree OCR. "
|
| 46 |
+
|
| 47 |
+
# ذخیره تصویر آپلود شده به صورت موقت
|
| 48 |
+
temp_image_path = os.path.join(output_path, "temp_image.jpg")
|
| 49 |
+
image.save(temp_image_path, quality=95)
|
| 50 |
+
|
| 51 |
+
if progress is not None:
|
| 52 |
+
progress(0.3, desc="در حال بارگذاری و تنظیم تصویر...")
|
| 53 |
+
|
| 54 |
+
# پیکربندی پارامترهای اندازه مدل
|
| 55 |
+
size_configs = {
|
| 56 |
+
"کوچک": {"base_size": 640, "image_size": 640, "crop_mode": False},
|
| 57 |
+
"پایه (توصیه شده)": {"base_size": 1024, "image_size": 1024, "crop_mode": True},
|
| 58 |
+
"بزرگ": {"base_size": 1280, "image_size": 1280, "crop_mode": False},
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
config = size_configs.get(model_size, size_configs["پایه (توصیه شده)"])
|
| 62 |
+
|
| 63 |
+
if progress is not None:
|
| 64 |
+
progress(0.5, desc="در حال اجرای مدل هوشمند...")
|
| 65 |
+
|
| 66 |
+
# اجرای استنتاج
|
| 67 |
+
plain_text_result = model.infer(
|
| 68 |
+
tokenizer,
|
| 69 |
+
prompt=prompt,
|
| 70 |
+
image_file=temp_image_path,
|
| 71 |
+
output_path=output_path,
|
| 72 |
+
base_size=config["base_size"],
|
| 73 |
+
image_size=config["image_size"],
|
| 74 |
+
crop_mode=config["crop_mode"],
|
| 75 |
+
save_results=True,
|
| 76 |
+
test_compress=True,
|
| 77 |
+
eval_mode=is_eval_mode,
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
if progress is not None:
|
| 81 |
+
progress(0.8, desc="در حال پردازش نتایج...")
|
| 82 |
+
|
| 83 |
+
# تعریف مسیرهای فایلهای تولید شده
|
| 84 |
+
image_result_path = os.path.join(output_path, "result_with_boxes.jpg")
|
| 85 |
+
markdown_result_path = os.path.join(output_path, "result.mmd")
|
| 86 |
+
|
| 87 |
+
# خواندن محتوای فایل markdown در صورت وجود
|
| 88 |
+
markdown_content = ""
|
| 89 |
+
if os.path.exists(markdown_result_path):
|
| 90 |
+
with open(markdown_result_path, "r", encoding="utf-8") as f:
|
| 91 |
+
markdown_content = f.read()
|
| 92 |
+
else:
|
| 93 |
+
markdown_content = plain_text_result if plain_text_result else "نتیجهای تولید نشد."
|
| 94 |
+
|
| 95 |
+
if progress is not None:
|
| 96 |
+
progress(1.0, desc="پردازش کامل شد!")
|
| 97 |
+
|
| 98 |
+
# بازگرداندن نتایج
|
| 99 |
+
text_result = plain_text_result if plain_text_result else markdown_content
|
| 100 |
+
return None, markdown_content, text_result
|
| 101 |
+
|
| 102 |
+
except Exception as e:
|
| 103 |
+
error_msg = f"خطا در پردازش تصویر: {str(e)}"
|
| 104 |
+
print(f"❌ {error_msg}")
|
| 105 |
+
return None, error_msg, error_msg
|
requirements.txt
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.6.0
|
| 2 |
+
torchvision
|
| 3 |
+
transformers==4.46.3
|
| 4 |
+
tokenizers==0.20.3
|
| 5 |
+
safetensors>=0.4.0
|
| 6 |
+
accelerate>=0.26.0
|
| 7 |
+
protobuf>=3.20.0
|
| 8 |
+
spaces>=0.20.0
|
| 9 |
+
Pillow>=10.0.0
|
| 10 |
+
einops
|
| 11 |
+
addict
|
| 12 |
+
easydict
|
| 13 |
+
gradio
|
| 14 |
+
einops
|
| 15 |
+
addict
|
| 16 |
+
easydict
|
| 17 |
+
# pip install flash-attn==2.7.3 --no-build-isolation (only support 3xxx or higher RTX GPU with Ampere architecture)
|