Add Moroccan Darija extraction app4
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, pipeline
|
| 4 |
import soundfile as sf
|
|
|
|
| 5 |
|
| 6 |
# Load models
|
| 7 |
# Transcription model for Moroccan Darija
|
|
@@ -9,40 +10,42 @@ processor = Wav2Vec2Processor.from_pretrained("boumehdi/wav2vec2-large-xlsr-moro
|
|
| 9 |
transcription_model = Wav2Vec2ForCTC.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
| 10 |
|
| 11 |
# Summarization model
|
| 12 |
-
summarizer = pipeline("summarization", model="
|
| 13 |
|
| 14 |
-
# Function to transcribe audio
|
| 15 |
def transcribe_audio(audio_path):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
with torch.no_grad():
|
| 21 |
logits = transcription_model(**inputs).logits
|
| 22 |
predicted_ids = torch.argmax(logits, dim=-1)
|
| 23 |
transcription = processor.batch_decode(predicted_ids)[0]
|
| 24 |
return transcription
|
| 25 |
|
| 26 |
-
# Function to analyze topics
|
| 27 |
def analyze_topics(summary):
|
| 28 |
if "customer service" in summary.lower():
|
| 29 |
return "Customer Service"
|
| 30 |
elif "retention" in summary.lower():
|
| 31 |
return "Retention"
|
| 32 |
else:
|
| 33 |
-
return "
|
| 34 |
|
| 35 |
-
# Function to transcribe, summarize, and analyze
|
| 36 |
def transcribe_summarize_analyze(audio_file):
|
| 37 |
# Transcription
|
| 38 |
transcription = transcribe_audio(audio_file)
|
| 39 |
|
| 40 |
# Summarization
|
| 41 |
-
summary = summarizer(transcription, max_length=
|
| 42 |
|
| 43 |
# Topic Analysis
|
| 44 |
topic = analyze_topics(summary)
|
| 45 |
-
|
| 46 |
return transcription, summary, topic
|
| 47 |
|
| 48 |
# Gradio Interface
|
|
@@ -57,11 +60,8 @@ app = gr.Interface(
|
|
| 57 |
fn=transcribe_summarize_analyze,
|
| 58 |
inputs=inputs,
|
| 59 |
outputs=outputs,
|
| 60 |
-
title="Moroccan Darija Audio
|
| 61 |
-
description=(
|
| 62 |
-
"Upload an audio file in Moroccan Darija to get its transcription, a summarized version, "
|
| 63 |
-
"and the detected topic (Customer Service or Retention)."
|
| 64 |
-
)
|
| 65 |
)
|
| 66 |
|
| 67 |
# Launch the app
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, pipeline
|
| 4 |
import soundfile as sf
|
| 5 |
+
import librosa
|
| 6 |
|
| 7 |
# Load models
|
| 8 |
# Transcription model for Moroccan Darija
|
|
|
|
| 10 |
transcription_model = Wav2Vec2ForCTC.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
| 11 |
|
| 12 |
# Summarization model
|
| 13 |
+
summarizer = pipeline("summarization", model="t5-small")
|
| 14 |
|
| 15 |
+
# Function to transcribe audio using Wav2Vec2
|
| 16 |
def transcribe_audio(audio_path):
|
| 17 |
+
# Load and resample audio to 16kHz
|
| 18 |
+
audio_input, original_sample_rate = sf.read(audio_path)
|
| 19 |
+
if original_sample_rate != 16000:
|
| 20 |
+
audio_input = librosa.resample(audio_input, orig_sr=original_sample_rate, target_sr=16000)
|
| 21 |
+
|
| 22 |
+
# Process audio for transcription
|
| 23 |
+
inputs = processor(audio_input, sampling_rate=16000, return_tensors="pt", padding=True)
|
| 24 |
with torch.no_grad():
|
| 25 |
logits = transcription_model(**inputs).logits
|
| 26 |
predicted_ids = torch.argmax(logits, dim=-1)
|
| 27 |
transcription = processor.batch_decode(predicted_ids)[0]
|
| 28 |
return transcription
|
| 29 |
|
| 30 |
+
# Function to analyze topics
|
| 31 |
def analyze_topics(summary):
|
| 32 |
if "customer service" in summary.lower():
|
| 33 |
return "Customer Service"
|
| 34 |
elif "retention" in summary.lower():
|
| 35 |
return "Retention"
|
| 36 |
else:
|
| 37 |
+
return "Other"
|
| 38 |
|
| 39 |
+
# Function to transcribe, summarize, and analyze
|
| 40 |
def transcribe_summarize_analyze(audio_file):
|
| 41 |
# Transcription
|
| 42 |
transcription = transcribe_audio(audio_file)
|
| 43 |
|
| 44 |
# Summarization
|
| 45 |
+
summary = summarizer(transcription, max_length=50, min_length=10, do_sample=False)[0]["summary_text"]
|
| 46 |
|
| 47 |
# Topic Analysis
|
| 48 |
topic = analyze_topics(summary)
|
|
|
|
| 49 |
return transcription, summary, topic
|
| 50 |
|
| 51 |
# Gradio Interface
|
|
|
|
| 60 |
fn=transcribe_summarize_analyze,
|
| 61 |
inputs=inputs,
|
| 62 |
outputs=outputs,
|
| 63 |
+
title="Moroccan Darija Audio Processing",
|
| 64 |
+
description="Upload an audio file in Moroccan Darija to get its transcription, a summarized version of the content, and an identified topic (e.g., Customer Service or Retention)."
|
|
|
|
|
|
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
# Launch the app
|