GannaEslam38 commited on
Commit
97d0f51
·
verified ·
1 Parent(s): a41eac1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
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
- # تحديد وسائط التقسيم هنا لتجنب تمريرها كـ model_kwargs غير مستخدمة
18
  pipeline_kwargs = {
19
  "chunk_length_s": 30, # طول المقطع الأقصى (30 ثانية)
20
- "stride": (5, 5), # تداخل 5 ثواني بين المقاطع
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
- # 💡 التعديل هنا: نزيل 'chunk_length_s' و 'stride' ونبقي فقط 'return_timestamps'
50
- # وسائط التقسيم تم تحديدها بالفعل في خطوة التحميل (Initialisation)
51
  result = asr_pipeline(
52
  audio_path,
53
- return_timestamps=True # ضروري لعملية التقسيم
54
  )
55
 
56
- # استخراج النص من النتيجة (النتيجة تحتوي على 'chunks' عند استخدام return_timestamps=True)
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 = "👳 Arabic-Whisper-CodeSwitching-Edition"
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()