masadonline commited on
Commit
21c1312
·
verified ·
1 Parent(s): 9fb1b7e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from transformers import MarianMTModel, MarianTokenizer
4
+
5
+ # Load translation model (Urdu → English)
6
+ @st.cache_resource
7
+ def load_model():
8
+ model_name = "Helsinki-NLP/opus-mt-ur-en"
9
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
10
+ model = MarianMTModel.from_pretrained(model_name)
11
+ return tokenizer, model
12
+
13
+ tokenizer, model = load_model()
14
+
15
+ # Giphy API Key (replace with your free key from https://developers.giphy.com/)
16
+ GIPHY_API_KEY = "YOUR_GIPHY_API_KEY"
17
+
18
+ # Urdu to English translator
19
+ def urdu_to_english(urdu_text):
20
+ inputs = tokenizer(urdu_text, return_tensors="pt", padding=True)
21
+ translated = model.generate(**inputs)
22
+ english_text = tokenizer.decode(translated[0], skip_special_tokens=True)
23
+ return english_text
24
+
25
+ # Fetch GIF for a word from Giphy
26
+ def get_gif_url(word):
27
+ search_url = f"https://api.giphy.com/v1/gifs/search"
28
+ params = {
29
+ "api_key": GIPHY_API_KEY,
30
+ "q": f"asl {word}",
31
+ "limit": 1
32
+ }
33
+ response = requests.get(search_url, params=params)
34
+ if response.status_code == 200:
35
+ data = response.json()
36
+ if data["data"]:
37
+ return data["data"][0]["images"]["downsized"]["url"]
38
+ return None
39
+
40
+ # Streamlit UI
41
+ st.set_page_config(page_title="DeafTranslator", layout="wide")
42
+ st.title("🧏 DeafTranslator – Urdu Text to Sign GIF Generator")
43
+ st.markdown("Translate Urdu text into **Sign Language GIFs** using free AI models.")
44
+
45
+ urdu_input = st.text_area("✍️ Enter Urdu text", height=120)
46
+
47
+ if st.button("🔁 Translate & Show GIFs") and urdu_input:
48
+ with st.spinner("Translating and fetching signs..."):
49
+ english_text = urdu_to_english(urdu_input)
50
+ st.success(f"**Translated English Text:** {english_text}")
51
+ words = english_text.lower().split()
52
+
53
+ st.subheader("📹 Sign Language GIFs")
54
+ for word in words:
55
+ gif_url = get_gif_url(word)
56
+ if gif_url:
57
+ st.image(gif_url, caption=word, width=200)
58
+ else:
59
+ st.warning(f"⚠️ No sign found for: `{word}`")