|
import streamlit as st |
|
from googletrans import Translator |
|
from transformers import pipeline |
|
import requests |
|
|
|
|
|
translator = Translator() |
|
|
|
|
|
st.set_page_config(page_title="AI-Powered Language Learning Assistant", page_icon="🧠", layout="wide") |
|
|
|
|
|
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 grammar feedback. |
|
""") |
|
|
|
|
|
text_input = st.text_input("Enter the text you want to translate or practice", "") |
|
|
|
|
|
language = st.selectbox("Select the language to translate to", ["es", "fr", "de", "it", "pt", "ru"]) |
|
|
|
if text_input: |
|
|
|
st.subheader(f"Original Text: {text_input}") |
|
translated_text = translator.translate(text_input, dest=language).text |
|
|
|
|
|
st.markdown(f"### Translated Text to {language.upper()}:") |
|
st.write(translated_text) |
|
|
|
|
|
st.subheader("Pronunciation Tip:") |
|
st.write("Use Google Translate or Forvo to practice pronunciation.") |
|
|
|
|
|
st.subheader("Grammar Feedback:") |
|
grammar_check_url = "https://api.languagetool.org/v2/check" |
|
params = { |
|
"text": text_input, |
|
"language": "en-US" |
|
} |
|
response = requests.post(grammar_check_url, data=params) |
|
if response.status_code == 200: |
|
result = response.json() |
|
if result['matches']: |
|
st.write("### Grammar Issues Found:") |
|
for match in result['matches']: |
|
st.write(f"- **{match['message']}** at position {match['offset']}-{match['offset']+match['length']}") |
|
else: |
|
st.write("No grammar issues found!") |
|
else: |
|
st.write("Grammar check failed. Try again later.") |
|
|
|
|
|
st.markdown("---") |
|
st.header("Vocabulary Practice") |
|
word_input = st.text_input("Enter a word to get its definition and synonyms", "") |
|
if word_input: |
|
|
|
try: |
|
word_model = pipeline("fill-mask", model="bert-base-uncased") |
|
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.") |
|
|
|
|
|
st.markdown(""" |
|
--- |
|
**Need more practice?** Visit [Google Translate](https://translate.google.com) for real-time translations and pronunciation! |
|
""") |
|
|