fargerm commited on
Commit
899d4f3
1 Parent(s): 43b331f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -37
app.py CHANGED
@@ -1,48 +1,83 @@
 
 
 
 
 
1
  import os
2
- import subprocess
3
 
4
- # Install sentencepiece
5
- subprocess.check_call([os.sys.executable, "-m", "pip", "install", "sentencepiece"])
 
 
 
 
 
 
 
6
 
7
- import streamlit as st
8
- from transformers import pipeline
9
- import torch
10
- from io import BytesIO
11
 
12
- # Load the text-to-speech model
13
- tts_model = pipeline("text-to-speech", model="microsoft/speecht5_tts")
 
14
 
15
- # Load the translation models
16
- translator_hindi = pipeline("translation_en_to_hi", model="Helsinki-NLP/opus-mt-en-hi")
17
- translator_urdu = pipeline("translation_en_to_ur", model="Helsinki-NLP/opus-mt-en-ur")
18
- translator_bengali = pipeline("translation_en_to_bn", model="Helsinki-NLP/opus-mt-en-bn")
 
19
 
20
- # Streamlit UI
21
- st.title("Language Translator")
22
-
23
- # Step 1: Take input from the user in English
24
- input_text = st.text_area("Enter text in English:")
25
- if st.button("Generate Audio"):
26
- if input_text:
27
- audio = tts_model(input_text)["audio"]
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.warning("Please enter some text.")
 
33
 
34
- # Step 2: Translate the input into the selected language
35
- selected_language = st.selectbox("Select target language", ["Hindi", "Urdu", "Bengali"])
 
36
 
37
- if st.button("Translate"):
38
- if input_text:
39
- if selected_language == "Hindi":
40
- translation = translator_hindi(input_text)[0]["translation_text"]
41
- elif selected_language == "Urdu":
42
- translation = translator_urdu(input_text)[0]["translation_text"]
43
- elif selected_language == "Bengali":
44
- translation = translator_bengali(input_text)[0]["translation_text"]
45
- st.write(f"Translated text in {selected_language}: {translation}")
 
 
 
 
 
 
46
  else:
47
- st.warning("Please enter some text.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = ""