Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
argostranslate.package.install_from_path(pkg.download())
|
14 |
-
break
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
for
|
22 |
-
if
|
23 |
-
|
24 |
break
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
if
|
28 |
-
|
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 |
-
|
39 |
-
|
40 |
-
|
41 |
-
st.markdown("Translate **Urdu** text to **English** and view it in **Sign Language** (ASL).")
|
42 |
|
43 |
-
|
44 |
|
45 |
def translate_urdu_to_english(urdu_text):
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
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 |
-
|
57 |
-
if os.path.exists(
|
58 |
-
st.image(
|
59 |
else:
|
60 |
for char in word:
|
61 |
if char.isalpha():
|
62 |
-
|
63 |
-
if os.path.exists(
|
64 |
-
st.image(
|
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)
|