Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,14 @@ MAX_CHUNK_SIZE = 350
|
|
| 24 |
MAX_WORKERS = 3
|
| 25 |
CLEANUP_INTERVAL = 300 # 5 دقیقه
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# نگاشت زبانها
|
| 28 |
LANGUAGE_MAP = {
|
| 29 |
"English": "en",
|
|
@@ -290,14 +298,47 @@ class MultilingualTranslator:
|
|
| 290 |
def __init__(self, cache_expiry_minutes: int = 60):
|
| 291 |
print("در حال بارگذاری مدل M2M100...")
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
# اجزای کمکی
|
| 303 |
self.cache = TranslationCache(cache_expiry_minutes)
|
|
@@ -598,7 +639,8 @@ def cleanup_old_data():
|
|
| 598 |
for session_id in expired_sessions:
|
| 599 |
translator.translation_sessions.pop(session_id, None)
|
| 600 |
|
| 601 |
-
|
|
|
|
| 602 |
|
| 603 |
except Exception as e:
|
| 604 |
print(f"خطا در پاکسازی: {str(e)}")
|
|
|
|
| 24 |
MAX_WORKERS = 3
|
| 25 |
CLEANUP_INTERVAL = 300 # 5 دقیقه
|
| 26 |
|
| 27 |
+
# تنظیم مسیر کش برای Hugging Face Spaces
|
| 28 |
+
os.environ["HF_HOME"] = "/app/.cache"
|
| 29 |
+
os.environ["TRANSFORMERS_CACHE"] = "/app/.cache"
|
| 30 |
+
|
| 31 |
+
# ایجاد پوشه کش در صورت عدم وجود
|
| 32 |
+
cache_dir = "/app/.cache"
|
| 33 |
+
os.makedirs(cache_dir, mode=0o755, exist_ok=True)
|
| 34 |
+
|
| 35 |
# نگاشت زبانها
|
| 36 |
LANGUAGE_MAP = {
|
| 37 |
"English": "en",
|
|
|
|
| 298 |
def __init__(self, cache_expiry_minutes: int = 60):
|
| 299 |
print("در حال بارگذاری مدل M2M100...")
|
| 300 |
|
| 301 |
+
try:
|
| 302 |
+
# تلاش برای بارگذاری مدل و توکنایزر
|
| 303 |
+
self.tokenizer = M2M100Tokenizer.from_pretrained(
|
| 304 |
+
MODEL_NAME,
|
| 305 |
+
cache_dir=cache_dir,
|
| 306 |
+
local_files_only=False
|
| 307 |
+
)
|
| 308 |
+
|
| 309 |
+
self.model = M2M100ForConditionalGeneration.from_pretrained(
|
| 310 |
+
MODEL_NAME,
|
| 311 |
+
cache_dir=cache_dir,
|
| 312 |
+
local_files_only=False
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
# تشخیص GPU
|
| 316 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 317 |
+
self.model.to(self.device)
|
| 318 |
+
print(f"مدل روی {self.device} بارگذاری شد")
|
| 319 |
+
|
| 320 |
+
except Exception as e:
|
| 321 |
+
print(f"خطا در بارگذاری مدل: {str(e)}")
|
| 322 |
+
print("تلاش مجدد با تنظیمات مختلف...")
|
| 323 |
+
|
| 324 |
+
# تلاش با تنظیمات مختلف
|
| 325 |
+
try:
|
| 326 |
+
# پاک کردن کش موجود
|
| 327 |
+
import shutil
|
| 328 |
+
if os.path.exists(cache_dir):
|
| 329 |
+
shutil.rmtree(cache_dir, ignore_errors=True)
|
| 330 |
+
os.makedirs(cache_dir, mode=0o777, exist_ok=True)
|
| 331 |
+
|
| 332 |
+
self.tokenizer = M2M100Tokenizer.from_pretrained(MODEL_NAME)
|
| 333 |
+
self.model = M2M100ForConditionalGeneration.from_pretrained(MODEL_NAME)
|
| 334 |
+
|
| 335 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 336 |
+
self.model.to(self.device)
|
| 337 |
+
print(f"مدل با تلاش مجدد روی {self.device} بارگذاری شد")
|
| 338 |
+
|
| 339 |
+
except Exception as e2:
|
| 340 |
+
print(f"خطای نهایی در بارگذاری مدل: {str(e2)}")
|
| 341 |
+
raise e2
|
| 342 |
|
| 343 |
# اجزای کمکی
|
| 344 |
self.cache = TranslationCache(cache_expiry_minutes)
|
|
|
|
| 639 |
for session_id in expired_sessions:
|
| 640 |
translator.translation_sessions.pop(session_id, None)
|
| 641 |
|
| 642 |
+
if expired_requests or expired_sessions:
|
| 643 |
+
print(f"پاکسازی انجام شد: {len(expired_requests)} درخواست و {len(expired_sessions)} session حذف شد")
|
| 644 |
|
| 645 |
except Exception as e:
|
| 646 |
print(f"خطا در پاکسازی: {str(e)}")
|