Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,58 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from googletrans import Translator # Free Google Translate API
|
| 3 |
-
from transformers import pipeline
|
| 4 |
import requests
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
translator = Translator()
|
| 8 |
-
|
| 9 |
-
# Streamlit UI setup
|
| 10 |
st.set_page_config(page_title="AI-Powered Language Learning Assistant", page_icon="🧠", layout="wide")
|
| 11 |
|
| 12 |
# Header and introduction
|
| 13 |
st.title("🧠 AI-Powered Language Learning Assistant")
|
| 14 |
st.markdown("""
|
| 15 |
Welcome to your AI-powered language assistant! Here you can:
|
| 16 |
-
- Translate words or sentences to different languages
|
| 17 |
- Learn and practice new vocabulary
|
| 18 |
- Get grammar feedback.
|
| 19 |
""")
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# Translate text using googletrans
|
| 29 |
-
st.subheader(f"Original Text: {text_input}")
|
| 30 |
-
translated_text = translator.translate(text_input, dest=language).text
|
| 31 |
-
|
| 32 |
-
# Display translation
|
| 33 |
-
st.markdown(f"### Translated Text to {language.upper()}:")
|
| 34 |
-
st.write(translated_text)
|
| 35 |
-
|
| 36 |
-
# Show pronunciation tip
|
| 37 |
-
st.subheader("Pronunciation Tip:")
|
| 38 |
-
st.write("Use Google Translate or Forvo to practice pronunciation.")
|
| 39 |
-
|
| 40 |
-
# Grammar Check (using LanguageTool)
|
| 41 |
-
st.subheader("Grammar Feedback:")
|
| 42 |
-
grammar_check_url = "https://api.languagetool.org/v2/check"
|
| 43 |
-
params = {
|
| 44 |
-
"text": text_input,
|
| 45 |
-
"language": "en-US"
|
| 46 |
}
|
| 47 |
-
response = requests.post(
|
| 48 |
if response.status_code == 200:
|
| 49 |
-
|
| 50 |
-
if result['matches']:
|
| 51 |
-
st.write("### Grammar Issues Found:")
|
| 52 |
-
for match in result['matches']:
|
| 53 |
-
st.write(f"- **{match['message']}** at position {match['offset']}-{match['offset']+match['length']}")
|
| 54 |
-
else:
|
| 55 |
-
st.write("No grammar issues found!")
|
| 56 |
else:
|
| 57 |
-
|
| 58 |
|
| 59 |
-
# Vocabulary
|
| 60 |
st.markdown("---")
|
| 61 |
st.header("Vocabulary Practice")
|
| 62 |
word_input = st.text_input("Enter a word to get its definition and synonyms", "")
|
| 63 |
if word_input:
|
| 64 |
-
# Using Hugging Face's BERT model for related words (synonyms)
|
| 65 |
try:
|
| 66 |
word_model = pipeline("fill-mask", model="bert-base-uncased") # Using BERT to predict related words
|
| 67 |
result = word_model(f"The synonym of {word_input} is [MASK].")
|
| 68 |
st.write(f"Synonyms or related words for **{word_input}**: {result}")
|
| 69 |
except Exception as e:
|
| 70 |
-
st.error("Error fetching vocabulary practice data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Footer for engagement
|
| 73 |
st.markdown("""
|
| 74 |
---
|
| 75 |
-
**Need more practice?** Visit [
|
| 76 |
""")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
import requests
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Set up the page
|
|
|
|
|
|
|
|
|
|
| 6 |
st.set_page_config(page_title="AI-Powered Language Learning Assistant", page_icon="🧠", layout="wide")
|
| 7 |
|
| 8 |
# Header and introduction
|
| 9 |
st.title("🧠 AI-Powered Language Learning Assistant")
|
| 10 |
st.markdown("""
|
| 11 |
Welcome to your AI-powered language assistant! Here you can:
|
| 12 |
+
- Translate words or sentences to different languages (using LibreTranslate API)
|
| 13 |
- Learn and practice new vocabulary
|
| 14 |
- Get grammar feedback.
|
| 15 |
""")
|
| 16 |
|
| 17 |
+
# Translation Function (Using LibreTranslate API)
|
| 18 |
+
def translate_text(text, target_language):
|
| 19 |
+
url = "https://libretranslate.de/translate" # Free LibreTranslate API
|
| 20 |
+
payload = {
|
| 21 |
+
'q': text,
|
| 22 |
+
'source': 'en',
|
| 23 |
+
'target': target_language
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
+
response = requests.post(url, data=payload)
|
| 26 |
if response.status_code == 200:
|
| 27 |
+
return response.json()['translatedText']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
else:
|
| 29 |
+
return "Translation failed."
|
| 30 |
|
| 31 |
+
# Vocabulary Practice Section using Hugging Face's BERT Model
|
| 32 |
st.markdown("---")
|
| 33 |
st.header("Vocabulary Practice")
|
| 34 |
word_input = st.text_input("Enter a word to get its definition and synonyms", "")
|
| 35 |
if word_input:
|
|
|
|
| 36 |
try:
|
| 37 |
word_model = pipeline("fill-mask", model="bert-base-uncased") # Using BERT to predict related words
|
| 38 |
result = word_model(f"The synonym of {word_input} is [MASK].")
|
| 39 |
st.write(f"Synonyms or related words for **{word_input}**: {result}")
|
| 40 |
except Exception as e:
|
| 41 |
+
st.error(f"Error fetching vocabulary practice data: {e}")
|
| 42 |
+
|
| 43 |
+
# Translation Section
|
| 44 |
+
st.markdown("---")
|
| 45 |
+
st.header("Translation")
|
| 46 |
+
text_input = st.text_input("Enter the text you want to translate", "")
|
| 47 |
+
language = st.selectbox("Select the language to translate to", ["es", "fr", "de", "it", "pt", "ru"])
|
| 48 |
+
|
| 49 |
+
if text_input:
|
| 50 |
+
translated_text = translate_text(text_input, language)
|
| 51 |
+
st.subheader(f"Translated Text to {language.upper()}:")
|
| 52 |
+
st.write(translated_text)
|
| 53 |
|
| 54 |
# Footer for engagement
|
| 55 |
st.markdown("""
|
| 56 |
---
|
| 57 |
+
**Need more practice?** Visit [LibreTranslate API](https://libretranslate.de/) for real-time translations and Hugging Face for more language models!
|
| 58 |
""")
|