Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
3 |
-
import torch
|
4 |
from io import BytesIO
|
5 |
|
6 |
-
# Define the translation model
|
7 |
-
model_name = "Helsinki-NLP/opus-mt-en-es" # Default to Spanish for the example
|
8 |
-
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
9 |
-
model = MarianMTModel.from_pretrained(model_name)
|
10 |
-
|
11 |
-
# Define the TTS model
|
12 |
-
tts_model_name = "microsoft/speecht5_tts"
|
13 |
-
tts_pipeline = pipeline("text-to-speech", model=tts_model_name)
|
14 |
-
|
15 |
# Define the language options
|
16 |
languages = {
|
17 |
'es': 'Spanish',
|
@@ -39,14 +29,16 @@ def translate_text(text, target_lang):
|
|
39 |
|
40 |
# Translate text
|
41 |
encoded_text = tokenizer.encode(text, return_tensors="pt")
|
42 |
-
|
|
|
43 |
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
44 |
return translated_text
|
45 |
|
46 |
def synthesize_speech(text, lang):
|
47 |
# Synthesize speech
|
|
|
48 |
try:
|
49 |
-
audio = tts_pipeline(text
|
50 |
audio_bytes = BytesIO(audio["audio"])
|
51 |
return audio_bytes, None
|
52 |
except Exception as e:
|
@@ -65,3 +57,4 @@ if st.button("Translate and Synthesize Speech"):
|
|
65 |
st.audio(audio_bytes, format="audio/wav")
|
66 |
|
67 |
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
|
|
3 |
from io import BytesIO
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# Define the language options
|
6 |
languages = {
|
7 |
'es': 'Spanish',
|
|
|
29 |
|
30 |
# Translate text
|
31 |
encoded_text = tokenizer.encode(text, return_tensors="pt")
|
32 |
+
forced_bos_token_id = model.config.forced_bos_token_id
|
33 |
+
translated = model.generate(encoded_text, forced_bos_token_id=forced_bos_token_id)
|
34 |
translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
|
35 |
return translated_text
|
36 |
|
37 |
def synthesize_speech(text, lang):
|
38 |
# Synthesize speech
|
39 |
+
tts_pipeline = pipeline("text-to-speech", model="microsoft/speecht5_tts")
|
40 |
try:
|
41 |
+
audio = tts_pipeline(text)
|
42 |
audio_bytes = BytesIO(audio["audio"])
|
43 |
return audio_bytes, None
|
44 |
except Exception as e:
|
|
|
57 |
st.audio(audio_bytes, format="audio/wav")
|
58 |
|
59 |
|
60 |
+
|