Add Moroccan Darija extraction app2
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 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,46 +9,40 @@ processor = Wav2Vec2Processor.from_pretrained("boumehdi/wav2vec2-large-xlsr-moro
|
|
| 10 |
transcription_model = Wav2Vec2ForCTC.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
| 11 |
|
| 12 |
# Summarization model
|
| 13 |
-
summarizer = pipeline("summarization", model="
|
| 14 |
|
| 15 |
-
# Function to
|
| 16 |
-
def resample_audio(audio_path, target_sr=16000):
|
| 17 |
-
audio_input, original_sr = librosa.load(audio_path, sr=None) # Load audio with original sampling rate
|
| 18 |
-
if original_sr != target_sr:
|
| 19 |
-
audio_input = librosa.resample(audio_input, orig_sr=original_sr, target_sr=target_sr) # Resample to 16kHz
|
| 20 |
-
return audio_input, target_sr
|
| 21 |
-
|
| 22 |
-
# Function to transcribe audio using Wav2Vec2
|
| 23 |
def transcribe_audio(audio_path):
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
inputs = processor(audio_input, sampling_rate=sample_rate, return_tensors="pt", padding=True)
|
| 27 |
-
|
| 28 |
-
# Get predictions
|
| 29 |
with torch.no_grad():
|
| 30 |
logits = transcription_model(**inputs).logits
|
| 31 |
-
|
| 32 |
-
# Decode predictions
|
| 33 |
predicted_ids = torch.argmax(logits, dim=-1)
|
| 34 |
transcription = processor.batch_decode(predicted_ids)[0]
|
| 35 |
return transcription
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# Function to transcribe and summarize
|
| 38 |
-
def transcribe_and_summarize(audio_file):
|
| 39 |
-
# Transcription
|
| 40 |
transcription = transcribe_audio(audio_file)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
if len(transcription.split()) < 10: # Check if the transcription is too short for summarization
|
| 44 |
-
summary = "Transcription is too short for summarization."
|
| 45 |
-
else:
|
| 46 |
-
# Summarization
|
| 47 |
-
summary = summarizer(transcription, max_length=50, min_length=10, do_sample=False)[0]["summary_text"]
|
| 48 |
-
|
| 49 |
return transcription, summary
|
| 50 |
|
| 51 |
# Gradio Interface
|
| 52 |
-
inputs =
|
|
|
|
|
|
|
|
|
|
| 53 |
outputs = [
|
| 54 |
gr.Textbox(label="Transcription"),
|
| 55 |
gr.Textbox(label="Summary")
|
|
@@ -60,7 +53,10 @@ app = gr.Interface(
|
|
| 60 |
inputs=inputs,
|
| 61 |
outputs=outputs,
|
| 62 |
title="Moroccan Darija Audio Transcription and Summarization",
|
| 63 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
# Launch the app
|
|
|
|
| 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 |
transcription_model = Wav2Vec2ForCTC.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
| 10 |
|
| 11 |
# Summarization model
|
| 12 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 13 |
|
| 14 |
+
# Function to transcribe audio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def transcribe_audio(audio_path):
|
| 16 |
+
audio_input, sample_rate = sf.read(audio_path)
|
| 17 |
+
if sample_rate != 16000:
|
| 18 |
+
raise ValueError("Audio must be sampled at 16kHz.")
|
| 19 |
inputs = processor(audio_input, sampling_rate=sample_rate, return_tensors="pt", padding=True)
|
|
|
|
|
|
|
| 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 filter text by keywords
|
| 27 |
+
def filter_text_by_keywords(text, keywords):
|
| 28 |
+
keyword_list = keywords.split(",")
|
| 29 |
+
filtered_sentences = [
|
| 30 |
+
sentence for sentence in text.split(". ") if any(keyword.strip().lower() in sentence.lower() for keyword in keyword_list)
|
| 31 |
+
]
|
| 32 |
+
return ". ".join(filtered_sentences) if filtered_sentences else text
|
| 33 |
+
|
| 34 |
# Function to transcribe and summarize
|
| 35 |
+
def transcribe_and_summarize(audio_file, keywords):
|
|
|
|
| 36 |
transcription = transcribe_audio(audio_file)
|
| 37 |
+
filtered_text = filter_text_by_keywords(transcription, keywords)
|
| 38 |
+
summary = summarizer(filtered_text, max_length=100, min_length=30, do_sample=False)[0]["summary_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return transcription, summary
|
| 40 |
|
| 41 |
# Gradio Interface
|
| 42 |
+
inputs = [
|
| 43 |
+
gr.Audio(type="filepath", label="Upload your audio file"),
|
| 44 |
+
gr.Textbox(label="Enter Keywords (comma-separated)", placeholder="e.g., customer, service, retention")
|
| 45 |
+
]
|
| 46 |
outputs = [
|
| 47 |
gr.Textbox(label="Transcription"),
|
| 48 |
gr.Textbox(label="Summary")
|
|
|
|
| 53 |
inputs=inputs,
|
| 54 |
outputs=outputs,
|
| 55 |
title="Moroccan Darija Audio Transcription and Summarization",
|
| 56 |
+
description=(
|
| 57 |
+
"Upload an audio file in Moroccan Darija to get its transcription and a summarized version. "
|
| 58 |
+
"Specify relevant keywords (comma-separated) to filter the transcription before summarization."
|
| 59 |
+
)
|
| 60 |
)
|
| 61 |
|
| 62 |
# Launch the app
|