Views
No views yet
pip install transformers torch torchaudio1from transformers import WhisperProcessor, WhisperForConditionalGeneration
2import torch
3import torchaudio
4
5# Load model and processor
6processor = WhisperProcessor.from_pretrained("CUAIStudents/DeepAr")
7model = WhisperForConditionalGeneration.from_pretrained("CUAIStudents/DeepAr")
8
9# Load and preprocess audio
10audio_path = "path_to_your_arabic_audio.wav"
11waveform, sample_rate = torchaudio.load(audio_path)
12
13# Resample to 16kHz if necessary
14if sample_rate != 16000:
15 resampler = torchaudio.transforms.Resample(sample_rate, 16000)
16 waveform = resampler(waveform)
17
18# Process audio
19input_features = processor(waveform.squeeze().numpy(), sampling_rate=16000, return_tensors="pt").input_features
20
21# Generate transcription
22with torch.no_grad():
23 predicted_ids = model.generate(input_features, language="ar")
24
25# Decode transcription (exactly as pronounced)
26transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
27print(f"Pronounced as: {transcription}")1def analyze_pronunciation(audio_path, target_text=None):
2 """
3 Analyze pronunciation and compare with target text if provided
4 """
5 waveform, sample_rate = torchaudio.load(audio_path)
6
7 if sample_rate != 16000:
8 resampler = torchaudio.transforms.Resample(sample_rate, 16000)
9 waveform = resampler(waveform)
10
11 input_features = processor(waveform.squeeze().numpy(), sampling_rate=16000, return_tensors="pt").input_features
12
13 with torch.no_grad():
14 predicted_ids = model.generate(input_features, language="ar")
15
16 actual_pronunciation = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
17
18 print(f"Actual pronunciation: {actual_pronunciation}")
19
20 if target_text:
21 print(f"Target text: {target_text}")
22 print("Analysis: Compare the differences for speech improvement")
23
24 return actual_pronunciation
25
26# Example usage
27pronunciation = analyze_pronunciation("student_reading.wav", "النص المطلوب قراءته")1def assess_multiple_recordings(audio_files, target_texts=None):
2 """
3 Process multiple recordings for comprehensive speech assessment
4 """
5 results = []
6
7 for i, audio_file in enumerate(audio_files):
8 waveform, sample_rate = torchaudio.load(audio_file)
9
10 if sample_rate != 16000:
11 resampler = torchaudio.transforms.Resample(sample_rate, 16000)
12 waveform = resampler(waveform)
13
14 input_features = processor(waveform.squeeze().numpy(), sampling_rate=16000, return_tensors="pt").input_features
15
16 with torch.no_grad():
17 predicted_ids = model.generate(input_features, language="ar")
18
19 pronunciation = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
20
21 result = {
22 'file': audio_file,
23 'pronunciation': pronunciation,
24 'target': target_texts[i] if target_texts else None
25 }
26 results.append(result)
27
28 print(f"File {i+1}: {pronunciation}")
29
30 return results
31
32# Example usage
33audio_files = ["recording1.wav", "recording2.wav", "recording3.wav"]
34target_texts = ["النص الأول", "النص الثاني", "النص الثالث"]
35assessment_results = assess_multiple_recordings(audio_files, target_texts)MIT License
Copyright (c) 2024 CUAIStudents
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.