Update app.py
Browse files
app.py
CHANGED
|
@@ -1,199 +1,199 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import spaces
|
| 3 |
-
from
|
| 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 |
)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
from main_T4 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 |
)
|