Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
# اختر الجهاز
|
| 6 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 7 |
|
| 8 |
-
# تحميل الـ pipeline للنموذج العربي ال
|
| 9 |
pipe = pipeline(
|
| 10 |
"automatic-speech-recognition",
|
| 11 |
-
model="
|
| 12 |
-
device=device
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
def transcribe(audio):
|
| 16 |
if audio is None:
|
| 17 |
return "لم يتم رفع أي ملف صوتي."
|
| 18 |
|
| 19 |
-
# استدعاء النموذج لتحويل الصوت
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
# بناء واجهة Gradio
|
| 24 |
gr.Interface(
|
| 25 |
fn=transcribe,
|
| 26 |
inputs=gr.Audio(sources=["upload"], type="filepath", label="ارفع ملفاً صوتياً"),
|
| 27 |
outputs=gr.Textbox(label="النص المُحوَّل"),
|
| 28 |
title="API لتحويل الكلام العربي إلى نص",
|
| 29 |
-
description="واجهة لنموذج Whisper المتخصص باللغة العربية (
|
| 30 |
allow_flagging="never"
|
| 31 |
).launch()
|
|
|
|
| 1 |
+
# import gradio as gr
|
| 2 |
+
# from transformers import pipeline
|
| 3 |
+
# import torch
|
| 4 |
+
|
| 5 |
+
# # اختر الجهاز: استخدم GPU إذا كان متاحاً في الـ Space، وإلا CPU
|
| 6 |
+
# device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 7 |
+
|
| 8 |
+
# # تحميل الـ pipeline للنموذج العربي البديل والممتاز
|
| 9 |
+
# pipe = pipeline(
|
| 10 |
+
# "automatic-speech-recognition",
|
| 11 |
+
# model="openai-community/whisper-large-v3-arabic", # <<<--- هذا هو التغيير
|
| 12 |
+
# device=device
|
| 13 |
+
# )
|
| 14 |
+
|
| 15 |
+
# def transcribe(audio):
|
| 16 |
+
# if audio is None:
|
| 17 |
+
# return "لم يتم رفع أي ملف صوتي."
|
| 18 |
+
|
| 19 |
+
# # استدعاء النموذج لتحويل الصوت (المسار المؤقت للملف)
|
| 20 |
+
# text = pipe(audio)["text"]
|
| 21 |
+
# return text
|
| 22 |
+
|
| 23 |
+
# # بناء واجهة Gradio التي ستوفر لنا الـ API
|
| 24 |
+
# gr.Interface(
|
| 25 |
+
# fn=transcribe,
|
| 26 |
+
# inputs=gr.Audio(sources=["upload"], type="filepath", label="ارفع ملفاً صوتياً"),
|
| 27 |
+
# outputs=gr.Textbox(label="النص المُحوَّل"),
|
| 28 |
+
# title="API لتحويل الكلام العربي إلى نص",
|
| 29 |
+
# description="واجهة لنموذج Whisper المتخصص باللغة العربية (openai-community/whisper-large-v3-arabic).",
|
| 30 |
+
# allow_flagging="never"
|
| 31 |
+
# ).launch()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
import gradio as gr
|
| 35 |
from transformers import pipeline
|
| 36 |
import torch
|
| 37 |
|
| 38 |
+
# اختر الجهاز
|
| 39 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 40 |
|
| 41 |
+
# تحميل الـ pipeline للنموذج العربي الجديد والمتاح
|
| 42 |
pipe = pipeline(
|
| 43 |
"automatic-speech-recognition",
|
| 44 |
+
model="bakri/whisper-large-v2-arabic", # <<<--- هذا هو النموذج الجديد والمتاح حالياً
|
| 45 |
+
device=device,
|
| 46 |
+
torch_dtype=torch.float16 if device != "cpu" else torch.float32 # تحسين الأداء على الـ GPU
|
| 47 |
)
|
| 48 |
|
| 49 |
def transcribe(audio):
|
| 50 |
if audio is None:
|
| 51 |
return "لم يتم رفع أي ملف صوتي."
|
| 52 |
|
| 53 |
+
# استدعاء النموذج لتحويل الصوت
|
| 54 |
+
# إضافة chunk_length_s و stride_length_s يمكن أن يساعد في معالجة الملفات الطويلة
|
| 55 |
+
result = pipe(audio, chunk_length_s=30, stride_length_s=5, return_timestamps=False)
|
| 56 |
+
return result["text"]
|
| 57 |
|
| 58 |
+
# بناء واجهة Gradio
|
| 59 |
gr.Interface(
|
| 60 |
fn=transcribe,
|
| 61 |
inputs=gr.Audio(sources=["upload"], type="filepath", label="ارفع ملفاً صوتياً"),
|
| 62 |
outputs=gr.Textbox(label="النص المُحوَّل"),
|
| 63 |
title="API لتحويل الكلام العربي إلى نص",
|
| 64 |
+
description="واجهة لنموذج Whisper المتخصص باللغة العربية (bakri/whisper-large-v2-arabic).",
|
| 65 |
allow_flagging="never"
|
| 66 |
).launch()
|