Update app.py
Browse files
app.py
CHANGED
|
@@ -10,27 +10,26 @@ MODEL_ID = "MohamedRashad/Arabic-Whisper-CodeSwitching-Edition"
|
|
| 10 |
device = 0 if torch.cuda.is_available() else "cpu"
|
| 11 |
print(f"Device set to use: {device}")
|
| 12 |
|
| 13 |
-
# 2. تحميل النموذج عبر الـ Pipeline (مع تحديد
|
| 14 |
try:
|
| 15 |
print("Loading ASR pipeline (Whisper) with chunking parameters...")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
pipeline_kwargs = {
|
| 19 |
"chunk_length_s": 30, # طول المقطع الأقصى (30 ثانية)
|
| 20 |
-
|
| 21 |
}
|
| 22 |
|
| 23 |
asr_pipeline = pipeline(
|
| 24 |
"automatic-speech-recognition",
|
| 25 |
model=MODEL_ID,
|
| 26 |
device=device,
|
| 27 |
-
**pipeline_kwargs # تمرير
|
| 28 |
)
|
| 29 |
-
print("Pipeline loaded successfully with chunking enabled.")
|
| 30 |
|
| 31 |
except Exception as e:
|
| 32 |
print(f"Error loading pipeline: {e}.")
|
| 33 |
-
# إذا فشل التحميل، نستخدم pipeline فارغ لمنع تعطل التطبيق
|
| 34 |
asr_pipeline = None
|
| 35 |
|
| 36 |
|
|
@@ -46,14 +45,13 @@ def transcribe_audio(audio_path):
|
|
| 46 |
print(f"Processing audio file: {audio_path}")
|
| 47 |
|
| 48 |
try:
|
| 49 |
-
# 💡
|
| 50 |
-
# وسائط التقسيم تم تحديدها بالفعل في خطوة التحميل (Initialisation)
|
| 51 |
result = asr_pipeline(
|
| 52 |
audio_path,
|
| 53 |
-
return_timestamps=True
|
| 54 |
)
|
| 55 |
|
| 56 |
-
# استخراج النص من النتيجة (
|
| 57 |
if 'chunks' in result and isinstance(result['chunks'], list):
|
| 58 |
segments_text = [seg['text'] for seg in result['chunks']]
|
| 59 |
final_text = " ".join(segments_text).strip()
|
|
@@ -69,7 +67,7 @@ def transcribe_audio(audio_path):
|
|
| 69 |
|
| 70 |
|
| 71 |
# 4. إعداد واجهة Gradio
|
| 72 |
-
title = "
|
| 73 |
description = (
|
| 74 |
"نموذج تحويل الكلام إلى نص يعتمد على Whisper Large v2، ومُدرب خصيصًا لدعم "
|
| 75 |
"التحويل الصوتي للغة العربية التي تحتوي على مزج لغوي (Code-Switching) مع الإنجليزية. "
|
|
@@ -86,4 +84,4 @@ gr.Interface(
|
|
| 86 |
title=title,
|
| 87 |
description=description,
|
| 88 |
live=False,
|
| 89 |
-
).launch()
|
|
|
|
| 10 |
device = 0 if torch.cuda.is_available() else "cpu"
|
| 11 |
print(f"Device set to use: {device}")
|
| 12 |
|
| 13 |
+
# 2. تحميل النموذج عبر الـ Pipeline (مع تحديد طول المقطع فقط)
|
| 14 |
try:
|
| 15 |
print("Loading ASR pipeline (Whisper) with chunking parameters...")
|
| 16 |
|
| 17 |
+
# تعريف وسائط التقسيم: نستخدم chunk_length_s فقط لتجنب مشكلة stride
|
| 18 |
pipeline_kwargs = {
|
| 19 |
"chunk_length_s": 30, # طول المقطع الأقصى (30 ثانية)
|
| 20 |
+
# ❌ تم حذف وسيط "stride" لتجنب الخطأ
|
| 21 |
}
|
| 22 |
|
| 23 |
asr_pipeline = pipeline(
|
| 24 |
"automatic-speech-recognition",
|
| 25 |
model=MODEL_ID,
|
| 26 |
device=device,
|
| 27 |
+
**pipeline_kwargs # تمرير الوسائط
|
| 28 |
)
|
| 29 |
+
print("Pipeline loaded successfully with simplified chunking enabled.")
|
| 30 |
|
| 31 |
except Exception as e:
|
| 32 |
print(f"Error loading pipeline: {e}.")
|
|
|
|
| 33 |
asr_pipeline = None
|
| 34 |
|
| 35 |
|
|
|
|
| 45 |
print(f"Processing audio file: {audio_path}")
|
| 46 |
|
| 47 |
try:
|
| 48 |
+
# 💡 نمرر return_timestamps=True وهو أمر ضروري لتفعيل تقسيم الملفات
|
|
|
|
| 49 |
result = asr_pipeline(
|
| 50 |
audio_path,
|
| 51 |
+
return_timestamps=True
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# استخراج النص من النتيجة (الـ Pipeline يقوم بالتقسيم والتجميع)
|
| 55 |
if 'chunks' in result and isinstance(result['chunks'], list):
|
| 56 |
segments_text = [seg['text'] for seg in result['chunks']]
|
| 57 |
final_text = " ".join(segments_text).strip()
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
# 4. إعداد واجهة Gradio
|
| 70 |
+
title = " Arabic-Whisper-CodeSwitching-Edition"
|
| 71 |
description = (
|
| 72 |
"نموذج تحويل الكلام إلى نص يعتمد على Whisper Large v2، ومُدرب خصيصًا لدعم "
|
| 73 |
"التحويل الصوتي للغة العربية التي تحتوي على مزج لغوي (Code-Switching) مع الإنجليزية. "
|
|
|
|
| 84 |
title=title,
|
| 85 |
description=description,
|
| 86 |
live=False,
|
| 87 |
+
).launch()
|