Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
| 6 |
-
import os
|
| 7 |
from typing import Tuple
|
| 8 |
|
| 9 |
## 1. تنظیمات اولیه و مدلها
|
|
@@ -29,17 +28,21 @@ class TextPostProcessor:
|
|
| 29 |
'۵':'5', '۶':'6', '۷':'7', '۸':'8', '۹':'9'
|
| 30 |
}
|
| 31 |
|
| 32 |
-
# بارگذاری مدل زبانی
|
| 33 |
try:
|
| 34 |
-
self.llm = pipeline(
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
self.llm = None
|
|
|
|
| 37 |
|
| 38 |
def preprocess(self, text: str) -> str:
|
| 39 |
"""پیشپردازش متن استخراج شده"""
|
| 40 |
if not text:
|
| 41 |
return ""
|
| 42 |
-
|
| 43 |
# نرمالسازی متن
|
| 44 |
for old, new in self.replacements.items():
|
| 45 |
text = text.replace(old, new)
|
|
@@ -49,15 +52,21 @@ class TextPostProcessor:
|
|
| 49 |
"""بهبود متن با مدل زبانی"""
|
| 50 |
if not text or not self.llm:
|
| 51 |
return text
|
| 52 |
-
|
| 53 |
try:
|
|
|
|
| 54 |
enhanced = self.llm(
|
| 55 |
-
|
| 56 |
-
max_length=
|
| 57 |
-
num_return_sequences=1
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
return text
|
| 62 |
|
| 63 |
## 2. پردازش اصلی
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import easyocr
|
| 3 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
from typing import Tuple
|
| 7 |
|
| 8 |
## 1. تنظیمات اولیه و مدلها
|
|
|
|
| 28 |
'۵':'5', '۶':'6', '۷':'7', '۸':'8', '۹':'9'
|
| 29 |
}
|
| 30 |
|
| 31 |
+
# بارگذاری مدل زبانی فارسی HooshvareLab/gpt2-fa
|
| 32 |
try:
|
| 33 |
+
self.llm = pipeline(
|
| 34 |
+
"text-generation",
|
| 35 |
+
model="HooshvareLab/gpt2-fa",
|
| 36 |
+
tokenizer="HooshvareLab/gpt2-fa"
|
| 37 |
+
)
|
| 38 |
+
except Exception as e:
|
| 39 |
self.llm = None
|
| 40 |
+
print(f"خطا در بارگذاری مدل HooshvareLab/gpt2-fa: {str(e)}")
|
| 41 |
|
| 42 |
def preprocess(self, text: str) -> str:
|
| 43 |
"""پیشپردازش متن استخراج شده"""
|
| 44 |
if not text:
|
| 45 |
return ""
|
|
|
|
| 46 |
# نرمالسازی متن
|
| 47 |
for old, new in self.replacements.items():
|
| 48 |
text = text.replace(old, new)
|
|
|
|
| 52 |
"""بهبود متن با مدل زبانی"""
|
| 53 |
if not text or not self.llm:
|
| 54 |
return text
|
|
|
|
| 55 |
try:
|
| 56 |
+
prompt = f"متن زیر را ویرایش و روانتر کن:\n{text}\n"
|
| 57 |
enhanced = self.llm(
|
| 58 |
+
prompt,
|
| 59 |
+
max_length=len(prompt) + 60,
|
| 60 |
+
num_return_sequences=1,
|
| 61 |
+
do_sample=True,
|
| 62 |
+
temperature=0.8,
|
| 63 |
+
pad_token_id=0
|
| 64 |
)
|
| 65 |
+
# فقط متن تولیدی پس از پرامپت را برگردان
|
| 66 |
+
gen_text = enhanced[0]['generated_text']
|
| 67 |
+
return gen_text[len(prompt):].strip() if gen_text.startswith(prompt) else gen_text.strip()
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"خطا در بهبود متن با LLM: {str(e)}")
|
| 70 |
return text
|
| 71 |
|
| 72 |
## 2. پردازش اصلی
|