OmidSakaki commited on
Commit
f5ea811
·
verified ·
1 Parent(s): a3b6ec8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -23
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import easyocr
3
- from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
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
- "text-generation",
31
- model="HooshvareLab/gpt2-fa",
32
- tokenizer="HooshvareLab/gpt2-fa"
33
  )
34
  except Exception as e:
35
- print("خطا در بارگذاری مدل زبانی:", e)
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
- """بازنویسی یا بهبود متن با LLM فارسی"""
48
  if not text or not self.llm:
49
  return text
50
- prompt = f"متن زیر را بازنویسی کن و به صورت روان و صحیح برگردان:\n{text}\nبازنویسی:"
51
  try:
52
- output = self.llm(
53
- prompt,
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(gen_text) < 8:
67
  return text
68
- return gen_text
69
  except Exception as e:
70
- print("خطا در بازنویسی با LLM:", e)
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. پایپلاین اصلی