Add Moroccan Darija extraction app5
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, pipeline
|
4 |
-
import
|
5 |
import librosa
|
6 |
|
7 |
# Load models
|
@@ -10,42 +10,63 @@ 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 transcribe audio using Wav2Vec2
|
16 |
def transcribe_audio(audio_path):
|
17 |
-
# Load and
|
18 |
-
audio_input,
|
19 |
-
|
20 |
-
audio_input = librosa.resample(audio_input, orig_sr=original_sample_rate, target_sr=16000)
|
21 |
|
22 |
-
#
|
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
|
31 |
-
def
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
return "Customer Service"
|
34 |
-
elif
|
35 |
-
return "Retention"
|
36 |
else:
|
37 |
return "Other"
|
38 |
|
39 |
-
# Function to transcribe, summarize, and
|
40 |
-
def
|
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
|
48 |
-
topic =
|
|
|
49 |
return transcription, summary, topic
|
50 |
|
51 |
# Gradio Interface
|
@@ -57,11 +78,11 @@ outputs = [
|
|
57 |
]
|
58 |
|
59 |
app = gr.Interface(
|
60 |
-
fn=
|
61 |
inputs=inputs,
|
62 |
outputs=outputs,
|
63 |
-
title="Moroccan Darija Audio
|
64 |
-
description="Upload an audio file in Moroccan Darija to get its transcription, a summarized version of the content, and
|
65 |
)
|
66 |
|
67 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, pipeline
|
4 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
5 |
import librosa
|
6 |
|
7 |
# Load models
|
|
|
10 |
transcription_model = Wav2Vec2ForCTC.from_pretrained("boumehdi/wav2vec2-large-xlsr-moroccan-darija")
|
11 |
|
12 |
# Summarization model
|
13 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
14 |
+
|
15 |
+
# Topic Classification Model (BERT for example)
|
16 |
+
topic_model = BertForSequenceClassification.from_pretrained("your-finetuned-topic-model")
|
17 |
+
topic_tokenizer = BertTokenizer.from_pretrained("your-finetuned-topic-model")
|
18 |
+
|
19 |
+
# Function to resample audio to 16kHz if necessary
|
20 |
+
def resample_audio(audio_path, target_sr=16000):
|
21 |
+
audio_input, original_sr = librosa.load(audio_path, sr=None) # Load audio with original sampling rate
|
22 |
+
if original_sr != target_sr:
|
23 |
+
audio_input = librosa.resample(audio_input, orig_sr=original_sr, target_sr=target_sr) # Resample to 16kHz
|
24 |
+
return audio_input, target_sr
|
25 |
|
26 |
# Function to transcribe audio using Wav2Vec2
|
27 |
def transcribe_audio(audio_path):
|
28 |
+
# Load and preprocess audio
|
29 |
+
audio_input, sample_rate = resample_audio(audio_path)
|
30 |
+
inputs = processor(audio_input, sampling_rate=sample_rate, return_tensors="pt", padding=True)
|
|
|
31 |
|
32 |
+
# Get predictions
|
|
|
33 |
with torch.no_grad():
|
34 |
logits = transcription_model(**inputs).logits
|
35 |
+
|
36 |
+
# Decode predictions
|
37 |
predicted_ids = torch.argmax(logits, dim=-1)
|
38 |
transcription = processor.batch_decode(predicted_ids)[0]
|
39 |
return transcription
|
40 |
|
41 |
+
# Function to classify the transcription into topics
|
42 |
+
def classify_topic(transcription):
|
43 |
+
# Tokenize the transcription and pass it through the BERT classifier
|
44 |
+
inputs = topic_tokenizer(transcription, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
45 |
+
with torch.no_grad():
|
46 |
+
outputs = topic_model(**inputs)
|
47 |
+
|
48 |
+
# Get the predicted label (0 for Customer Service, 1 for Retention Service, etc.)
|
49 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
50 |
+
|
51 |
+
# Map prediction to a topic
|
52 |
+
if predicted_class == 0:
|
53 |
return "Customer Service"
|
54 |
+
elif predicted_class == 1:
|
55 |
+
return "Retention Service"
|
56 |
else:
|
57 |
return "Other"
|
58 |
|
59 |
+
# Function to transcribe, summarize, and classify topic
|
60 |
+
def transcribe_and_summarize(audio_file):
|
61 |
# Transcription
|
62 |
transcription = transcribe_audio(audio_file)
|
63 |
|
64 |
# Summarization
|
65 |
summary = summarizer(transcription, max_length=50, min_length=10, do_sample=False)[0]["summary_text"]
|
66 |
|
67 |
+
# Topic classification
|
68 |
+
topic = classify_topic(transcription)
|
69 |
+
|
70 |
return transcription, summary, topic
|
71 |
|
72 |
# Gradio Interface
|
|
|
78 |
]
|
79 |
|
80 |
app = gr.Interface(
|
81 |
+
fn=transcribe_and_summarize,
|
82 |
inputs=inputs,
|
83 |
outputs=outputs,
|
84 |
+
title="Moroccan Darija Audio Transcription, Summarization, and Topic Classification",
|
85 |
+
description="Upload an audio file in Moroccan Darija to get its transcription, a summarized version of the content, and the detected topic."
|
86 |
)
|
87 |
|
88 |
# Launch the app
|