engrphoenix commited on
Commit
22f6356
·
verified ·
1 Parent(s): f3d698c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -25
app.py CHANGED
@@ -1,20 +1,9 @@
1
  import streamlit as st
2
- from transformers import pipeline
3
- from googletrans import Translator
4
 
5
- # Function to handle translation using Hugging Face or Google Translate
6
- def translate_text(text, target_language="es"):
7
- # Hugging Face pipeline for translation (can be used for various language pairs)
8
- try:
9
- translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-es")
10
- result = translator(text, max_length=400)
11
- return result[0]['translation_text']
12
- except Exception as e:
13
- st.error("Error translating text using Hugging Face. Trying Google Translate instead.")
14
- # Fallback to Google Translate if Hugging Face fails
15
- translator = Translator()
16
- translated_text = translator.translate(text, dest=target_language).text
17
- return translated_text
18
 
19
  # Streamlit UI setup
20
  st.set_page_config(page_title="AI-Powered Language Learning Assistant", page_icon="🧠", layout="wide")
@@ -32,40 +21,42 @@ Welcome to your AI-powered language assistant! Here you can:
32
  text_input = st.text_input("Enter the text you want to translate or practice", "")
33
 
34
  # Select target language for translation
35
- language = st.selectbox("Select the language to translate to", ["es", "fr", "de", "it", "pt", "ru"])
36
 
37
  if text_input:
38
- # Translate text
39
  st.subheader(f"Original Text: {text_input}")
40
- translated_text = translate_text(text_input, target_language=language)
41
 
42
  # Display translation
43
  st.markdown(f"### Translated Text to {language.upper()}:")
44
- st.write(translated_text)
45
 
46
  # Show pronunciation tip
47
  st.subheader("Pronunciation Tip:")
48
- st.write("Use an app like Google Translate or Forvo to practice pronunciation. If you want help with specific words, type them below.")
49
-
50
  # Grammar Check (simple demo)
51
  st.subheader("Grammar Feedback:")
52
- st.write("For more advanced grammar feedback, please use language tools like Grammarly or LanguageTool.")
53
 
54
  # Vocabulary practice section
55
  st.markdown("---")
56
  st.header("Vocabulary Practice")
57
  word_input = st.text_input("Enter a word to get its definition and synonyms", "")
58
  if word_input:
59
- # Using Hugging Face API to fetch definitions and synonyms (this part can be expanded with a dedicated model)
 
 
60
  try:
61
  word_model = pipeline("fill-mask", model="bert-base-uncased") # Using BERT to predict related words
62
  result = word_model(f"The synonym of {word_input} is [MASK].")
63
  st.write(f"Synonyms or related words for **{word_input}**: {result}")
64
  except Exception as e:
65
  st.error("Error fetching vocabulary practice data.")
66
-
67
  # Footer for engagement
68
  st.markdown("""
69
  ---
70
- **Need more practice?** Visit [Google Translate](https://translate.google.com) for real-time translations and pronunciation!
71
  """)
 
1
  import streamlit as st
2
+ import deepl # DeepL API client library
 
3
 
4
+ # Initialize the DeepL Translator with your API key
5
+ DEEPL_API_KEY = "your_deepL_api_key" # Replace with your DeepL API key
6
+ translator = deepl.Translator(DEEPL_API_KEY)
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Streamlit UI setup
9
  st.set_page_config(page_title="AI-Powered Language Learning Assistant", page_icon="🧠", layout="wide")
 
21
  text_input = st.text_input("Enter the text you want to translate or practice", "")
22
 
23
  # Select target language for translation
24
+ language = st.selectbox("Select the language to translate to", ["ES", "FR", "DE", "IT", "PT", "RU"])
25
 
26
  if text_input:
27
+ # Translate text using DeepL API
28
  st.subheader(f"Original Text: {text_input}")
29
+ translated_text = translator.translate_text(text_input, target_lang=language)
30
 
31
  # Display translation
32
  st.markdown(f"### Translated Text to {language.upper()}:")
33
+ st.write(translated_text.text)
34
 
35
  # Show pronunciation tip
36
  st.subheader("Pronunciation Tip:")
37
+ st.write("Use an app like Forvo or Google Translate to practice pronunciation. You can also use DeepL for listening practice on the translated text.")
38
+
39
  # Grammar Check (simple demo)
40
  st.subheader("Grammar Feedback:")
41
+ st.write("For advanced grammar feedback, use grammar-checking tools like LanguageTool.")
42
 
43
  # Vocabulary practice section
44
  st.markdown("---")
45
  st.header("Vocabulary Practice")
46
  word_input = st.text_input("Enter a word to get its definition and synonyms", "")
47
  if word_input:
48
+ # For vocabulary practice, let's use the Hugging Face BERT model for related words
49
+ from transformers import pipeline
50
+
51
  try:
52
  word_model = pipeline("fill-mask", model="bert-base-uncased") # Using BERT to predict related words
53
  result = word_model(f"The synonym of {word_input} is [MASK].")
54
  st.write(f"Synonyms or related words for **{word_input}**: {result}")
55
  except Exception as e:
56
  st.error("Error fetching vocabulary practice data.")
57
+
58
  # Footer for engagement
59
  st.markdown("""
60
  ---
61
+ **Need more practice?** Visit [DeepL](https://www.deepl.com) for real-time translations and pronunciation!
62
  """)