Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 3 |
from googletrans import Translator
|
| 4 |
-
from gtts import gTTS
|
| 5 |
-
import
|
| 6 |
-
import time
|
| 7 |
import os
|
| 8 |
|
| 9 |
# Set up Streamlit page
|
|
@@ -16,76 +15,63 @@ st.markdown("Enter text in English, and we'll create a **fusion** of multiple la
|
|
| 16 |
translator = Translator()
|
| 17 |
|
| 18 |
# Load T5 model
|
| 19 |
-
model_name = "t5-small"
|
| 20 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 21 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 22 |
|
| 23 |
-
# Language codes
|
| 24 |
languages = {
|
| 25 |
'es': 'Spanish', 'fr': 'French', 'de': 'German', 'it': 'Italian', 'zh-cn': 'Chinese',
|
| 26 |
'ja': 'Japanese', 'hi': 'Hindi', 'ar': 'Arabic', 'mr': 'Marathi', 'ta': 'Tamil',
|
| 27 |
'el': 'Greek', 'la': 'Latin', 'pl': 'Polish', 'ru': 'Russian', 'tr': 'Turkish'
|
| 28 |
}
|
| 29 |
|
| 30 |
-
# Function to
|
| 31 |
def mix_languages_with_model(text):
|
| 32 |
input_text = f"Translate to mixed languages: {text}"
|
| 33 |
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 34 |
-
|
| 35 |
-
# Generate fusion text
|
| 36 |
outputs = model.generate(inputs, max_length=100, num_beams=5, early_stopping=True)
|
| 37 |
mixed_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
-
|
| 39 |
return mixed_text
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
def translate_text(text):
|
| 43 |
translations = {}
|
| 44 |
for code, lang in languages.items():
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
translations[lang] =
|
| 48 |
except Exception as e:
|
| 49 |
translations[lang] = f"Translation failed: {str(e)}"
|
| 50 |
return translations
|
| 51 |
|
| 52 |
-
# Function to convert text to speech
|
| 53 |
def text_to_speech(text):
|
| 54 |
try:
|
| 55 |
-
time.sleep(2) # Prevent API rate limits
|
| 56 |
tts = gTTS(text=text, lang="en")
|
| 57 |
tts.save("output.mp3")
|
| 58 |
return "output.mp3"
|
| 59 |
except Exception as e:
|
| 60 |
-
st.
|
| 61 |
-
|
| 62 |
-
# Try local TTS
|
| 63 |
-
try:
|
| 64 |
-
engine = pyttsx3.init()
|
| 65 |
-
engine.save_to_file(text, "output.mp3")
|
| 66 |
-
engine.runAndWait()
|
| 67 |
-
return "output.mp3"
|
| 68 |
-
except Exception as e:
|
| 69 |
-
st.error(f"Local TTS also failed: {e}")
|
| 70 |
-
return None
|
| 71 |
|
| 72 |
-
#
|
| 73 |
input_text = st.text_area("π Enter your text in English:")
|
| 74 |
|
| 75 |
if st.button("Generate Fusion Language"):
|
| 76 |
if input_text:
|
| 77 |
-
# Generate
|
| 78 |
fused_text = mix_languages_with_model(input_text)
|
| 79 |
st.subheader("π Generated Fusion Language:")
|
| 80 |
st.write(fused_text)
|
| 81 |
|
| 82 |
-
# Convert
|
| 83 |
audio_file = text_to_speech(fused_text)
|
| 84 |
if audio_file:
|
| 85 |
-
st.audio(audio_file, format='audio/mp3')
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
translations = translate_text(fused_text)
|
| 89 |
st.subheader("π£οΈ Translations in Different Languages:")
|
| 90 |
for lang, translation in translations.items():
|
| 91 |
st.write(f"**{lang}:** {translation}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 3 |
from googletrans import Translator
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import asyncio # Add asyncio
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
# Set up Streamlit page
|
|
|
|
| 15 |
translator = Translator()
|
| 16 |
|
| 17 |
# Load T5 model
|
| 18 |
+
model_name = "t5-small"
|
| 19 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 20 |
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 21 |
|
| 22 |
+
# Language codes
|
| 23 |
languages = {
|
| 24 |
'es': 'Spanish', 'fr': 'French', 'de': 'German', 'it': 'Italian', 'zh-cn': 'Chinese',
|
| 25 |
'ja': 'Japanese', 'hi': 'Hindi', 'ar': 'Arabic', 'mr': 'Marathi', 'ta': 'Tamil',
|
| 26 |
'el': 'Greek', 'la': 'Latin', 'pl': 'Polish', 'ru': 'Russian', 'tr': 'Turkish'
|
| 27 |
}
|
| 28 |
|
| 29 |
+
# Function to mix languages using T5 model
|
| 30 |
def mix_languages_with_model(text):
|
| 31 |
input_text = f"Translate to mixed languages: {text}"
|
| 32 |
inputs = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
|
|
|
|
|
|
| 33 |
outputs = model.generate(inputs, max_length=100, num_beams=5, early_stopping=True)
|
| 34 |
mixed_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 35 |
return mixed_text
|
| 36 |
|
| 37 |
+
# β
ASYNC FIX: Translate text properly
|
| 38 |
+
async def translate_text(text):
|
| 39 |
translations = {}
|
| 40 |
for code, lang in languages.items():
|
| 41 |
try:
|
| 42 |
+
translation = await translator.translate(text, dest=code)
|
| 43 |
+
translations[lang] = translation.text # β
FIXED: `await` required
|
| 44 |
except Exception as e:
|
| 45 |
translations[lang] = f"Translation failed: {str(e)}"
|
| 46 |
return translations
|
| 47 |
|
| 48 |
+
# Function to convert text to speech
|
| 49 |
def text_to_speech(text):
|
| 50 |
try:
|
|
|
|
| 51 |
tts = gTTS(text=text, lang="en")
|
| 52 |
tts.save("output.mp3")
|
| 53 |
return "output.mp3"
|
| 54 |
except Exception as e:
|
| 55 |
+
st.error(f"Google TTS failed: {e}")
|
| 56 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# User input
|
| 59 |
input_text = st.text_area("π Enter your text in English:")
|
| 60 |
|
| 61 |
if st.button("Generate Fusion Language"):
|
| 62 |
if input_text:
|
| 63 |
+
# Generate mixed language text
|
| 64 |
fused_text = mix_languages_with_model(input_text)
|
| 65 |
st.subheader("π Generated Fusion Language:")
|
| 66 |
st.write(fused_text)
|
| 67 |
|
| 68 |
+
# Convert to speech
|
| 69 |
audio_file = text_to_speech(fused_text)
|
| 70 |
if audio_file:
|
| 71 |
+
st.audio(audio_file, format='audio/mp3')
|
| 72 |
|
| 73 |
+
# β
Fix translation call with `asyncio.run()`
|
| 74 |
+
translations = asyncio.run(translate_text(fused_text))
|
| 75 |
st.subheader("π£οΈ Translations in Different Languages:")
|
| 76 |
for lang, translation in translations.items():
|
| 77 |
st.write(f"**{lang}:** {translation}")
|