masadonline commited on
Commit
d88e1e6
·
verified ·
1 Parent(s): f14206b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -36
app.py CHANGED
@@ -3,61 +3,57 @@ import requests
3
  import os
4
  from PIL import Image
5
 
6
- # Hugging Face model for Urdu to English translation (ensure it supports ur -> en)
7
- HF_API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-ur-en"
8
- HF_API_TOKEN = os.getenv("HF_API_TOKEN") # Set this in your environment
 
9
 
10
- headers = {
11
- "Authorization": f"Bearer {HF_API_TOKEN}"
12
- }
 
13
 
14
- # Sign images folder (e.g., a.jpg, b.jpg, hello.jpg, etc.)
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
- return response.json()[0]['translation_text']
 
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
- return ""
30
-
31
 
32
- def display_sign_language(text):
33
- words = text.lower().split()
 
34
  for word in words:
35
- img_path_word = os.path.join(SIGN_IMAGE_FOLDER, f"{word}.jpg")
36
- if os.path.exists(img_path_word):
37
- st.image(img_path_word, caption=word)
38
  else:
39
- # Fallback to letter-by-letter spelling
40
  for char in word:
41
  if char.isalpha():
42
- img_path_char = os.path.join(SIGN_IMAGE_FOLDER, f"{char}.jpg")
43
- if os.path.exists(img_path_char):
44
- st.image(img_path_char, caption=char)
45
 
46
- # Streamlit UI
47
- st.set_page_config(page_title="DeafTranslator", layout="centered")
48
- st.title("🤟 DeafTranslator")
49
- st.markdown("Translate **Urdu text** to **English** and see it in **sign language**.")
50
 
51
- urdu_text = st.text_area("Enter Urdu Text", height=150)
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
- english_text = translate_urdu_to_english(urdu_text)
59
- if english_text:
60
- st.subheader("Translated English Text")
61
- st.success(english_text)
62
- st.subheader("Sign Language Representation")
63
- display_sign_language(english_text)
 
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)