masadonline commited on
Commit
76e6474
·
verified ·
1 Parent(s): 17fe25f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -44
app.py CHANGED
@@ -1,68 +1,63 @@
1
  import streamlit as st
2
- from PIL import Image
3
  import os
 
4
  import argostranslate.package
5
  import argostranslate.translate
6
 
7
- # Load Argos Translate Urdu to English model (first time only)
8
- def load_argos_translate_model():
9
- argostranslate.package.update_package_index() # 🔧 Add this line
10
- available_packages = argostranslate.package.get_available_packages()
11
- for pkg in available_packages:
12
- if pkg.from_code == "ur" and pkg.to_code == "en":
13
- argostranslate.package.install_from_path(pkg.download())
14
- break
15
 
16
- # Check if translation model is installed
17
- installed_languages = argostranslate.translate.get_installed_languages()
18
- urdu_to_english = None
19
- for lang in installed_languages:
20
- if lang.code == "ur":
21
- for to_lang in lang.translations:
22
- if to_lang.to_lang.code == "en":
23
- urdu_to_english = to_lang
24
  break
 
 
25
 
26
- # If not installed, download and install
27
- if urdu_to_english is None:
28
- with st.spinner("Downloading translation model..."):
29
- load_argos_translate_model()
30
- installed_languages = argostranslate.translate.get_installed_languages()
31
- for lang in installed_languages:
32
- if lang.code == "ur":
33
- for to_lang in lang.translations:
34
- if to_lang.to_lang.code == "en":
35
- urdu_to_english = to_lang
36
- break
37
 
38
- # Streamlit UI
39
- st.set_page_config(page_title="DeafTranslator", layout="centered")
40
- st.title("🤟 DeafTranslator")
41
- st.markdown("Translate **Urdu** text to **English** and view it in **Sign Language** (ASL).")
42
 
43
- SIGN_IMAGE_FOLDER = "sign_images" # Ensure images like a.jpg, b.jpg etc. are in this folder
44
 
45
  def translate_urdu_to_english(urdu_text):
46
- try:
47
- return urdu_to_english.translate(urdu_text)
48
- except Exception as e:
49
- st.error(f"Translation failed: {str(e)}")
50
- return ""
 
 
 
51
 
52
  def display_sign_language(english_text):
53
  words = english_text.lower().split()
54
  st.markdown("### 👐 Sign Language Representation")
55
  for word in words:
56
- word_image_path = os.path.join(SIGN_IMAGE_FOLDER, f"{word}.jpg")
57
- if os.path.exists(word_image_path):
58
- st.image(word_image_path, caption=word, width=120)
59
  else:
60
  for char in word:
61
  if char.isalpha():
62
- char_image_path = os.path.join(SIGN_IMAGE_FOLDER, f"{char}.jpg")
63
- if os.path.exists(char_image_path):
64
- st.image(char_image_path, caption=char.upper(), width=80)
65
 
 
66
  urdu_text = st.text_area("📝 Enter Urdu Text", height=150)
67
 
68
  if st.button("🔄 Translate & Show Signs"):
@@ -71,6 +66,7 @@ if st.button("🔄 Translate & Show Signs"):
71
  else:
72
  with st.spinner("Translating..."):
73
  english_translation = translate_urdu_to_english(urdu_text)
 
74
  if english_translation:
75
  st.markdown("### ✅ Translated English")
76
  st.success(english_translation)
 
1
  import streamlit as st
 
2
  import os
3
+ from PIL import Image
4
  import argostranslate.package
5
  import argostranslate.translate
6
 
7
+ # Streamlit UI setup
8
+ st.set_page_config(page_title="DeafTranslator", layout="centered")
9
+ st.title("🤟 DeafTranslator")
10
+ st.markdown("Translate **Urdu** text to **English** and view it in **Sign Language** (ASL).")
11
+
12
+ SIGN_IMAGE_FOLDER = "sign_images" # Place images like a.jpg, b.jpg, hello.jpg, etc. here
 
 
13
 
14
+ @st.cache_resource
15
+ def load_argos_urdu_to_english():
16
+ try:
17
+ argostranslate.package.update_package_index()
18
+ available_packages = argostranslate.package.get_available_packages()
19
+ for pkg in available_packages:
20
+ if pkg.from_code == "ur" and pkg.to_code == "en":
21
+ argostranslate.package.install_from_path(pkg.download())
22
  break
23
+ except Exception:
24
+ pass # ignore if already installed or network issue
25
 
26
+ installed_languages = argostranslate.translate.get_installed_languages()
27
+ from_lang = next((lang for lang in installed_languages if lang.code == "ur"), None)
28
+ to_lang = next((lang for lang in installed_languages if lang.code == "en"), None)
 
 
 
 
 
 
 
 
29
 
30
+ if from_lang and to_lang:
31
+ return argostranslate.translate.get_translation(from_lang, to_lang)
32
+ return None
 
33
 
34
+ translator = load_argos_urdu_to_english()
35
 
36
  def translate_urdu_to_english(urdu_text):
37
+ if translator:
38
+ try:
39
+ return translator.translate(urdu_text)
40
+ except Exception as e:
41
+ st.error(f"Translation failed: {e}")
42
+ else:
43
+ st.error("Translation model not loaded.")
44
+ return ""
45
 
46
  def display_sign_language(english_text):
47
  words = english_text.lower().split()
48
  st.markdown("### 👐 Sign Language Representation")
49
  for word in words:
50
+ word_img_path = os.path.join(SIGN_IMAGE_FOLDER, f"{word}.jpg")
51
+ if os.path.exists(word_img_path):
52
+ st.image(word_img_path, caption=word, width=120)
53
  else:
54
  for char in word:
55
  if char.isalpha():
56
+ char_img_path = os.path.join(SIGN_IMAGE_FOLDER, f"{char}.jpg")
57
+ if os.path.exists(char_img_path):
58
+ st.image(char_img_path, caption=char.upper(), width=80)
59
 
60
+ # --- Streamlit UI ---
61
  urdu_text = st.text_area("📝 Enter Urdu Text", height=150)
62
 
63
  if st.button("🔄 Translate & Show Signs"):
 
66
  else:
67
  with st.spinner("Translating..."):
68
  english_translation = translate_urdu_to_english(urdu_text)
69
+
70
  if english_translation:
71
  st.markdown("### ✅ Translated English")
72
  st.success(english_translation)