Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import easyocr
|
3 |
-
from transformers import pipeline
|
4 |
import numpy as np
|
5 |
from typing import Tuple
|
6 |
|
@@ -26,13 +26,14 @@ class TextPostProcessor:
|
|
26 |
'۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9'
|
27 |
}
|
28 |
try:
|
|
|
29 |
self.llm = pipeline(
|
30 |
-
"
|
31 |
-
model="
|
32 |
-
tokenizer="
|
33 |
)
|
34 |
except Exception as e:
|
35 |
-
print("خطا در بارگذاری مدل
|
36 |
self.llm = None
|
37 |
|
38 |
def preprocess(self, text: str) -> str:
|
@@ -44,30 +45,19 @@ class TextPostProcessor:
|
|
44 |
return " ".join(text.split())
|
45 |
|
46 |
def enhance_with_llm(self, text: str) -> str:
|
47 |
-
"""بازنویسی
|
48 |
if not text or not self.llm:
|
49 |
return text
|
50 |
-
prompt = f"متن زیر را بازنویسی
|
51 |
try:
|
52 |
-
|
53 |
-
|
54 |
-
max_length=len(prompt) + len(text) + 60,
|
55 |
-
num_return_sequences=1,
|
56 |
-
do_sample=True,
|
57 |
-
temperature=0.9,
|
58 |
-
pad_token_id=0,
|
59 |
-
eos_token_id=2
|
60 |
-
)
|
61 |
-
gen_text = output[0]['generated_text']
|
62 |
-
# فقط بخش بازنویسی شده را جدا کن
|
63 |
-
if "بازنویسی:" in gen_text:
|
64 |
-
gen_text = gen_text.split("بازنویسی:")[-1].strip()
|
65 |
# اگر بازنویسی مدل بیمعنا یا کوتاه بود، همان متن را برگردان
|
66 |
-
if len(
|
67 |
return text
|
68 |
-
return
|
69 |
except Exception as e:
|
70 |
-
print("خطا در
|
71 |
return text
|
72 |
|
73 |
## 2. پایپلاین اصلی
|
|
|
1 |
import gradio as gr
|
2 |
import easyocr
|
3 |
+
from transformers import pipeline
|
4 |
import numpy as np
|
5 |
from typing import Tuple
|
6 |
|
|
|
26 |
'۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9'
|
27 |
}
|
28 |
try:
|
29 |
+
# استفاده از مدل instruction-tuned مناسب بازنویسی
|
30 |
self.llm = pipeline(
|
31 |
+
"text2text-generation",
|
32 |
+
model="ParsiAI/gpt2-medium-fa-instruction",
|
33 |
+
tokenizer="ParsiAI/gpt2-medium-fa-instruction"
|
34 |
)
|
35 |
except Exception as e:
|
36 |
+
print("خطا در بارگذاری مدل بازنویسی:", e)
|
37 |
self.llm = None
|
38 |
|
39 |
def preprocess(self, text: str) -> str:
|
|
|
45 |
return " ".join(text.split())
|
46 |
|
47 |
def enhance_with_llm(self, text: str) -> str:
|
48 |
+
"""بازنویسی متن با مدل instruction-tuned"""
|
49 |
if not text or not self.llm:
|
50 |
return text
|
51 |
+
prompt = f"متن زیر را بازنویسی کن:\n{text}"
|
52 |
try:
|
53 |
+
result = self.llm(prompt, max_length=256, num_return_sequences=1)
|
54 |
+
enhanced_text = result[0]['generated_text'].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
# اگر بازنویسی مدل بیمعنا یا کوتاه بود، همان متن را برگردان
|
56 |
+
if len(enhanced_text) < 8:
|
57 |
return text
|
58 |
+
return enhanced_text
|
59 |
except Exception as e:
|
60 |
+
print("خطا در بازنویسی:", e)
|
61 |
return text
|
62 |
|
63 |
## 2. پایپلاین اصلی
|