Create process_audio.py
Browse files- process_audio.py +47 -0
process_audio.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import whisper
|
| 3 |
+
from transformers import MarianMTModel, MarianTokenizer
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
import certifi
|
| 8 |
+
|
| 9 |
+
os.environ["SSL_CERT_FILE"] = certifi.where()
|
| 10 |
+
|
| 11 |
+
def process_audio(input_path, output_path, target_language):
|
| 12 |
+
# Model 1: Speech-to-Text using Whisper
|
| 13 |
+
model = whisper.load_model("tiny")
|
| 14 |
+
result = model.transcribe(input_path)
|
| 15 |
+
text = result["text"]
|
| 16 |
+
|
| 17 |
+
# Model 2: Translation (Dynamic model selection based on target_language)
|
| 18 |
+
if target_language == 'hi': # Hindi
|
| 19 |
+
model_name = "Helsinki-NLP/opus-mt-en-hi"
|
| 20 |
+
elif target_language == 'es': # Spanish
|
| 21 |
+
model_name = "Helsinki-NLP/opus-mt-en-es"
|
| 22 |
+
elif target_language == 'fr': # French
|
| 23 |
+
model_name = "Helsinki-NLP/opus-mt-en-fr"
|
| 24 |
+
elif target_language == 'bn': # Bengali
|
| 25 |
+
model_name = "shhossain/opus-mt-en-to-bn"
|
| 26 |
+
else:
|
| 27 |
+
raise ValueError(f"Unsupported target language: {target_language}")
|
| 28 |
+
|
| 29 |
+
# Load translation model
|
| 30 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
| 31 |
+
model = MarianMTModel.from_pretrained(model_name)
|
| 32 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
| 33 |
+
outputs = model.generate(**inputs)
|
| 34 |
+
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
+
|
| 36 |
+
# Model 3: Text-to-Speech using gTTS
|
| 37 |
+
tts = gTTS(translation, lang=target_language)
|
| 38 |
+
|
| 39 |
+
# Save the translated text as an audio file
|
| 40 |
+
tts.save(output_path)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
input_file = sys.argv[1] # Path to input audio file
|
| 45 |
+
output_file = sys.argv[2] # Path to output audio file
|
| 46 |
+
target_language = sys.argv[3] # Target language passed from backend
|
| 47 |
+
process_audio(input_file, output_file, target_language)
|