JabriA commited on
Commit
3f47ed7
·
1 Parent(s): 7fd6b33

Add Moroccan Darija extraction app5

Browse files
Files changed (1) hide show
  1. app.py +41 -20
app.py CHANGED
@@ -1,7 +1,7 @@
1
  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
@@ -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="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
@@ -57,11 +78,11 @@ outputs = [
57
  ]
58
 
59
  app = gr.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
 
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