Spaces:
Sleeping
Sleeping
Download utils.py from Youssef1F/AI_Study_Companion: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/Youssef1F/AI_Study_Companion/resolve/main/utils.py
- Command line
-
hf download hf://spaces/Youssef1F/AI_Study_Companion/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/Youssef1F/AI_Study_Companion/resolve/main/utils.py
1.1 kB
| # utils.py | |
| import gc | |
| import torch | |
| import re | |
| from langdetect import detect, LangDetectException | |
| DEVICE = "cuda" if torch.cuda.is_available() else "cpu" | |
| def detect_language(text: str) -> str: | |
| """يكشف لغة النص لتوجيهه للموديل المناسب (AR/EN)""" | |
| try: | |
| lang = detect(text) | |
| return "ar" if lang == "ar" else "en" | |
| except LangDetectException: | |
| return "en" | |
| def clean_ocr_output(raw_text: str) -> str: | |
| """تنظيف ذكي لمخرجات OCR دون الاعتماد على Regex عشوائي""" | |
| text = re.sub(r"\n\s*\n", "\n", raw_text) # إزالة الأسطر الفارغة المتكررة | |
| text = re.sub(r"[^\w\s\u0600-\u06FF\u0750-\u077F.,;:?!()-]", "", text) # إزالة رموز غريبة | |
| text = re.sub(r"\s+", " ", text).strip() # توحيد المسافات | |
| return text | |
| def free_gpu_memory(): | |
| """تفريغ ذاكرة GPU بعد كل مرحلة لمنع CUDA OOM على Colab""" | |
| gc.collect() | |
| if DEVICE == "cuda": | |
| torch.cuda.empty_cache() |