import streamlit as st from transformers import pipeline from googletrans import Translator # Function to handle translation using Hugging Face or Google Translate def translate_text(text, target_language="es"): # Hugging Face pipeline for translation (can be used for various language pairs) try: translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es") result = translator(text, max_length=400) return result[0]['translation_text'] except Exception as e: st.error("Error translating text using Hugging Face. Trying Google Translate instead.") # Fallback to Google Translate if Hugging Face fails translator = Translator() translated_text = translator.translate(text, dest=target_language).text return translated_text # Streamlit UI setup st.set_page_config(page_title="AI-Powered Language Learning Assistant", page_icon="🧠", layout="wide") # Header and introduction st.title("🧠 AI-Powered Language Learning Assistant") st.markdown(""" Welcome to your AI-powered language assistant! Here you can: - Translate words or sentences to different languages - Learn and practice new vocabulary - Get pronunciation tips and grammar feedback. """) # Input field for text text_input = st.text_input("Enter the text you want to translate or practice", "") # Select target language for translation language = st.selectbox("Select the language to translate to", ["es", "fr", "de", "it", "pt", "ru"]) if text_input: # Translate text st.subheader(f"Original Text: {text_input}") translated_text = translate_text(text_input, target_language=language) # Display translation st.markdown(f"### Translated Text to {language.upper()}:") st.write(translated_text) # Show pronunciation tip st.subheader("Pronunciation Tip:") st.write("Use an app like Google Translate or Forvo to practice pronunciation. If you want help with specific words, type them below.") # Grammar Check (simple demo) st.subheader("Grammar Feedback:") st.write("For more advanced grammar feedback, please use language tools like Grammarly or LanguageTool.") # Vocabulary practice section st.markdown("---") st.header("Vocabulary Practice") word_input = st.text_input("Enter a word to get its definition and synonyms", "") if word_input: # Using Hugging Face API to fetch definitions and synonyms (this part can be expanded with a dedicated model) try: word_model = pipeline("fill-mask", model="bert-base-uncased") # Using BERT to predict related words result = word_model(f"The synonym of {word_input} is [MASK].") st.write(f"Synonyms or related words for **{word_input}**: {result}") except Exception as e: st.error("Error fetching vocabulary practice data.") # Footer for engagement st.markdown(""" --- **Need more practice?** Visit [Google Translate](https://translate.google.com) for real-time translations and pronunciation! """)