Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
-
import subprocess
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
|
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
# Convert to BytesIO to handle as a file-like object
|
29 |
-
audio_bytes = BytesIO(audio)
|
30 |
-
st.audio(audio_bytes, format="audio/wav")
|
31 |
else:
|
32 |
-
st.
|
|
|
33 |
|
34 |
-
|
35 |
-
|
|
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
else:
|
47 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import MarianMTModel, MarianTokenizer, pipeline, SpeechT5Processor, SpeechT5ForTextToSpeech
|
3 |
+
from datasets import load_dataset
|
4 |
+
import torch
|
5 |
+
import soundfile as sf
|
6 |
import os
|
|
|
7 |
|
8 |
+
# Function to check and load translation model
|
9 |
+
def load_translation_model(model_name):
|
10 |
+
try:
|
11 |
+
model = MarianMTModel.from_pretrained(model_name)
|
12 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
13 |
+
return model, tokenizer
|
14 |
+
except EnvironmentError as e:
|
15 |
+
st.error(f"Error loading model '{model_name}': {e}")
|
16 |
+
return None, None
|
17 |
|
18 |
+
# Translation models
|
19 |
+
translator_urdu_model_name = "Helsinki-NLP/opus-mt-en-ur"
|
20 |
+
translator_hindi_model_name = "Helsinki-NLP/opus-mt-en-hi"
|
21 |
+
translator_bengali_model_name = "Helsinki-NLP/opus-mt-en-bn"
|
22 |
|
23 |
+
translator_urdu, tokenizer_urdu = load_translation_model(translator_urdu_model_name)
|
24 |
+
translator_hindi, tokenizer_hindi = load_translation_model(translator_hindi_model_name)
|
25 |
+
translator_bengali, tokenizer_bengali = load_translation_model(translator_bengali_model_name)
|
26 |
|
27 |
+
# TTS model and processor
|
28 |
+
tts_model_name = "microsoft/speecht5_tts"
|
29 |
+
tts_model = SpeechT5ForTextToSpeech.from_pretrained(tts_model_name)
|
30 |
+
processor = SpeechT5Processor.from_pretrained(tts_model_name)
|
31 |
+
speaker_embeddings = torch.tensor(load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")["xvector"][0]).unsqueeze(0)
|
32 |
|
33 |
+
# Function to translate text
|
34 |
+
def translate_text(text, target_lang):
|
35 |
+
if target_lang == "Urdu":
|
36 |
+
model, tokenizer = translator_urdu, tokenizer_urdu
|
37 |
+
elif target_lang == "Hindi":
|
38 |
+
model, tokenizer = translator_hindi, tokenizer_hindi
|
39 |
+
elif target_lang == "Bengali":
|
40 |
+
model, tokenizer = translator_bengali, tokenizer_bengali
|
|
|
|
|
|
|
41 |
else:
|
42 |
+
st.error(f"Translation to {target_lang} is not supported.")
|
43 |
+
return ""
|
44 |
|
45 |
+
if model is None or tokenizer is None:
|
46 |
+
st.error(f"Model or tokenizer not available for {target_lang}.")
|
47 |
+
return ""
|
48 |
|
49 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
50 |
+
translated = model.generate(**inputs)
|
51 |
+
translated_text = tokenizer.batch_decode(translated, skip_special_tokens=True)[0]
|
52 |
+
return translated_text
|
53 |
+
|
54 |
+
# Function to synthesize speech
|
55 |
+
def synthesize_speech(text, target_lang):
|
56 |
+
inputs = processor(text=text, return_tensors="pt")
|
57 |
+
speech = tts_model.generate_speech(inputs["input_ids"], speaker_embeddings)
|
58 |
+
|
59 |
+
output_path = "output.wav"
|
60 |
+
sf.write(output_path, speech.numpy(), samplerate=16000)
|
61 |
+
|
62 |
+
if os.path.exists(output_path):
|
63 |
+
return output_path
|
64 |
else:
|
65 |
+
st.error("Failed to generate audio.")
|
66 |
+
return None
|
67 |
+
|
68 |
+
# Streamlit UI
|
69 |
+
st.title("Language Translator with Speech Synthesis")
|
70 |
+
|
71 |
+
text_input = st.text_input("Enter text in English:")
|
72 |
+
target_lang = st.selectbox("Select Target Language:", ["Urdu", "Hindi", "Bengali"])
|
73 |
+
|
74 |
+
if st.button("Translate"):
|
75 |
+
translated_text = translate_text(text_input, target_lang)
|
76 |
+
st.text_area("Translated text:", value=translated_text, height=100)
|
77 |
+
|
78 |
+
audio_file = synthesize_speech(translated_text, target_lang)
|
79 |
+
if audio_file:
|
80 |
+
st.audio(audio_file)
|
81 |
|
82 |
+
# Clear input for new text
|
83 |
+
st.session_state.text_input = ""
|