Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,61 +3,57 @@ import requests
|
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
13 |
|
14 |
-
# Sign images folder (
|
15 |
-
SIGN_IMAGE_FOLDER = "sign_images"
|
16 |
|
17 |
def translate_urdu_to_english(urdu_text):
|
18 |
payload = {"inputs": urdu_text}
|
19 |
-
response = requests.post(HF_API_URL, headers=headers, json=payload)
|
20 |
-
|
21 |
try:
|
|
|
22 |
response.raise_for_status()
|
23 |
-
|
|
|
24 |
except requests.exceptions.HTTPError as err:
|
25 |
st.error(f"Translation failed: {response.status_code} - {response.text}")
|
26 |
-
return ""
|
27 |
except Exception as e:
|
28 |
st.error(f"Unexpected error: {str(e)}")
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
def display_sign_language(
|
33 |
-
words =
|
|
|
34 |
for word in words:
|
35 |
-
|
36 |
-
if os.path.exists(
|
37 |
-
st.image(
|
38 |
else:
|
39 |
-
# Fallback to letter-by-letter spelling
|
40 |
for char in word:
|
41 |
if char.isalpha():
|
42 |
-
|
43 |
-
if os.path.exists(
|
44 |
-
st.image(
|
45 |
|
46 |
-
# Streamlit UI
|
47 |
-
st.
|
48 |
-
st.title("🤟 DeafTranslator")
|
49 |
-
st.markdown("Translate **Urdu text** to **English** and see it in **sign language**.")
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
if st.button("Translate to Sign Language"):
|
54 |
if not urdu_text.strip():
|
55 |
st.warning("Please enter some Urdu text.")
|
56 |
else:
|
57 |
with st.spinner("Translating..."):
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
st.
|
62 |
-
st.
|
63 |
-
display_sign_language(
|
|
|
3 |
import os
|
4 |
from PIL import Image
|
5 |
|
6 |
+
# Streamlit config
|
7 |
+
st.set_page_config(page_title="DeafTranslator", layout="centered")
|
8 |
+
st.title("🤟 DeafTranslator")
|
9 |
+
st.markdown("Translate **Urdu** text to **English** and view it in **Sign Language** (ASL).")
|
10 |
|
11 |
+
# Hugging Face translation model (no token required for public models, but safer with token)
|
12 |
+
TRANSLATE_API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-ur-en"
|
13 |
+
HF_API_TOKEN = st.secrets.get("HF_API_TOKEN", None)
|
14 |
+
HEADERS = {"Authorization": f"Bearer {HF_API_TOKEN}"} if HF_API_TOKEN else {}
|
15 |
|
16 |
+
# Sign language images folder (alphabet or word-based)
|
17 |
+
SIGN_IMAGE_FOLDER = "sign_images" # Place a.jpg, b.jpg, etc. here
|
18 |
|
19 |
def translate_urdu_to_english(urdu_text):
|
20 |
payload = {"inputs": urdu_text}
|
|
|
|
|
21 |
try:
|
22 |
+
response = requests.post(TRANSLATE_API_URL, headers=HEADERS, json=payload)
|
23 |
response.raise_for_status()
|
24 |
+
result = response.json()
|
25 |
+
return result[0]['translation_text']
|
26 |
except requests.exceptions.HTTPError as err:
|
27 |
st.error(f"Translation failed: {response.status_code} - {response.text}")
|
|
|
28 |
except Exception as e:
|
29 |
st.error(f"Unexpected error: {str(e)}")
|
30 |
+
return ""
|
|
|
31 |
|
32 |
+
def display_sign_language(english_text):
|
33 |
+
words = english_text.lower().split()
|
34 |
+
st.markdown("### 👐 Sign Language Representation")
|
35 |
for word in words:
|
36 |
+
word_image_path = os.path.join(SIGN_IMAGE_FOLDER, f"{word}.jpg")
|
37 |
+
if os.path.exists(word_image_path):
|
38 |
+
st.image(word_image_path, caption=word, width=120)
|
39 |
else:
|
|
|
40 |
for char in word:
|
41 |
if char.isalpha():
|
42 |
+
char_image_path = os.path.join(SIGN_IMAGE_FOLDER, f"{char}.jpg")
|
43 |
+
if os.path.exists(char_image_path):
|
44 |
+
st.image(char_image_path, caption=char.upper(), width=80)
|
45 |
|
46 |
+
# --- Streamlit App UI ---
|
47 |
+
urdu_text = st.text_area("📝 Enter Urdu Text", height=150)
|
|
|
|
|
48 |
|
49 |
+
if st.button("🔄 Translate & Show Signs"):
|
|
|
|
|
50 |
if not urdu_text.strip():
|
51 |
st.warning("Please enter some Urdu text.")
|
52 |
else:
|
53 |
with st.spinner("Translating..."):
|
54 |
+
english_translation = translate_urdu_to_english(urdu_text)
|
55 |
+
|
56 |
+
if english_translation:
|
57 |
+
st.markdown("### ✅ Translated English")
|
58 |
+
st.success(english_translation)
|
59 |
+
display_sign_language(english_translation)
|