Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
import easyocr
|
3 |
import numpy as np
|
4 |
-
from
|
5 |
-
|
6 |
|
7 |
class OCRProcessor:
|
8 |
def __init__(self):
|
@@ -13,66 +13,69 @@ class OCRProcessor:
|
|
13 |
results = self.reader.readtext(image, detail=0, paragraph=True)
|
14 |
return "\n".join(results) if results else ""
|
15 |
except Exception as e:
|
16 |
-
|
17 |
|
18 |
-
class
|
19 |
def __init__(self):
|
20 |
-
model_name = "
|
21 |
try:
|
22 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
-
self.model =
|
24 |
except Exception as e:
|
25 |
-
raise RuntimeError(f"خطا در بارگذاری مدل
|
26 |
|
27 |
-
def
|
28 |
-
if not
|
29 |
-
return
|
30 |
-
|
31 |
try:
|
32 |
-
|
33 |
-
|
34 |
-
prompt,
|
35 |
-
return_tensors="pt",
|
36 |
-
max_length=512,
|
37 |
-
truncation=True
|
38 |
)
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
44 |
)
|
45 |
-
|
|
|
|
|
|
|
|
|
46 |
except Exception as e:
|
47 |
-
|
48 |
-
return text
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
ocr_text = OCRProcessor().extract_text(image)
|
53 |
-
corrected_text = TextCorrector().correct(ocr_text)
|
54 |
-
return ocr_text, corrected_text
|
55 |
-
except Exception as e:
|
56 |
-
error_msg = f"خطا: {str(e)}"
|
57 |
-
return error_msg, error_msg
|
58 |
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
gr.Markdown("""
|
61 |
-
# سیستم
|
|
|
|
|
|
|
62 |
""")
|
63 |
-
|
64 |
with gr.Row():
|
65 |
with gr.Column():
|
66 |
img_input = gr.Image(label="تصویر ورودی", type="numpy")
|
67 |
-
|
|
|
68 |
with gr.Column():
|
69 |
-
|
70 |
-
|
71 |
|
72 |
process_btn.click(
|
73 |
-
fn=
|
74 |
-
inputs=img_input,
|
75 |
-
outputs=[
|
76 |
)
|
77 |
|
78 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import easyocr
|
3 |
import numpy as np
|
4 |
+
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
|
5 |
+
import torch
|
6 |
|
7 |
class OCRProcessor:
|
8 |
def __init__(self):
|
|
|
13 |
results = self.reader.readtext(image, detail=0, paragraph=True)
|
14 |
return "\n".join(results) if results else ""
|
15 |
except Exception as e:
|
16 |
+
return f"خطا در پردازش OCR: {str(e)}"
|
17 |
|
18 |
+
class PersianQAModel:
|
19 |
def __init__(self):
|
20 |
+
model_name = "OmidSakaki/roberta_Persian_QA"
|
21 |
try:
|
22 |
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
23 |
+
self.model = AutoModelForQuestionAnswering.from_pretrained(model_name)
|
24 |
except Exception as e:
|
25 |
+
raise RuntimeError(f"خطا در بارگذاری مدل پرسش و پاسخ: {str(e)}")
|
26 |
|
27 |
+
def answer_question(self, context: str, question: str) -> str:
|
28 |
+
if not context.strip() or not question.strip():
|
29 |
+
return "متن یا سوال وارد نشده است."
|
|
|
30 |
try:
|
31 |
+
inputs = self.tokenizer.encode_plus(
|
32 |
+
question, context, return_tensors='pt', truncation=True, max_length=512
|
|
|
|
|
|
|
|
|
33 |
)
|
34 |
+
input_ids = inputs["input_ids"].tolist()[0]
|
35 |
+
outputs = self.model(**inputs)
|
36 |
+
answer_start = torch.argmax(outputs.start_logits)
|
37 |
+
answer_end = torch.argmax(outputs.end_logits) + 1
|
38 |
+
answer = self.tokenizer.convert_tokens_to_string(
|
39 |
+
self.tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end])
|
40 |
)
|
41 |
+
# حذف توکنهای اضافی یا فاصله
|
42 |
+
answer = answer.strip()
|
43 |
+
if not answer or answer in ['[CLS]', '[SEP]', '[PAD]']:
|
44 |
+
return "جوابی یافت نشد."
|
45 |
+
return answer
|
46 |
except Exception as e:
|
47 |
+
return f"خطا در مدل پرسش و پاسخ: {str(e)}"
|
|
|
48 |
|
49 |
+
ocr_processor = OCRProcessor()
|
50 |
+
qa_model = PersianQAModel()
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
+
def pipeline(image, question):
|
53 |
+
# استخراج متن از تصویر
|
54 |
+
context = ocr_processor.extract_text(image)
|
55 |
+
# پاسخ به سوال بر اساس متن
|
56 |
+
answer = qa_model.answer_question(context, question)
|
57 |
+
return context, answer
|
58 |
+
|
59 |
+
with gr.Blocks(title="استخراج متن و پاسخ به سوال از تصویر فارسی") as app:
|
60 |
gr.Markdown("""
|
61 |
+
# سیستم هوشمند پرسش و پاسخ از روی تصویر فارسی
|
62 |
+
1. تصویر را بارگذاری کنید تا متن استخراج شود.
|
63 |
+
2. سوال خود را به فارسی تایپ کنید.
|
64 |
+
3. دکمه «پاسخ» را بزنید.
|
65 |
""")
|
|
|
66 |
with gr.Row():
|
67 |
with gr.Column():
|
68 |
img_input = gr.Image(label="تصویر ورودی", type="numpy")
|
69 |
+
question_input = gr.Textbox(label="سوال شما به فارسی", placeholder="مثلاً: نویسنده این متن کیست؟", lines=1)
|
70 |
+
process_btn = gr.Button("پاسخ")
|
71 |
with gr.Column():
|
72 |
+
context_output = gr.Textbox(label="متن استخراج شده", lines=10, max_lines=None, interactive=False)
|
73 |
+
answer_output = gr.Textbox(label="پاسخ مدل", lines=3, max_lines=None, interactive=False)
|
74 |
|
75 |
process_btn.click(
|
76 |
+
fn=pipeline,
|
77 |
+
inputs=[img_input, question_input],
|
78 |
+
outputs=[context_output, answer_output]
|
79 |
)
|
80 |
|
81 |
if __name__ == "__main__":
|