Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,13 @@ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
|
| 17 |
processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
|
| 18 |
|
| 19 |
# Placeholder sign labels
|
| 20 |
-
sign_labels = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Function to classify sign and refine using Groq API
|
| 23 |
def classify_sign(image):
|
|
@@ -25,28 +31,58 @@ def classify_sign(image):
|
|
| 25 |
inputs = processor(images=image, return_tensors="pt")
|
| 26 |
prediction = inputs['pixel_values'].argmax().item()
|
| 27 |
gesture = sign_labels.get(prediction % len(sign_labels), "Unknown Sign")
|
|
|
|
| 28 |
if GROQ_API_KEY:
|
| 29 |
-
response = requests.post(
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if response.status_code == 200:
|
| 32 |
return response.json()['choices'][0]['message']['content']
|
|
|
|
| 33 |
return gesture
|
| 34 |
|
| 35 |
-
# Function to generate sign
|
| 36 |
def generate_sign_video(text):
|
| 37 |
if GROQ_API_KEY:
|
| 38 |
-
response = requests.post(
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if response.status_code == 200:
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
return None
|
| 43 |
|
| 44 |
# Streamlit UI
|
|
|
|
| 45 |
def main():
|
| 46 |
st.set_page_config(page_title="Sign Language Translator", layout="wide")
|
| 47 |
st.markdown("<h1 style='text-align: center; font-size: 40px; font-weight: bold; color: #4CAF50;'>π€ Sign Language Translator</h1>", unsafe_allow_html=True)
|
| 48 |
|
| 49 |
-
tab1, tab2, tab3, tab4 = st.tabs([
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
with tab1:
|
| 52 |
uploaded_image = st.file_uploader("Upload an image of a hand gesture", type=["png", "jpg", "jpeg"])
|
|
@@ -68,6 +104,7 @@ def main():
|
|
| 68 |
if st.button("Enable Cam", key="enable_cam"):
|
| 69 |
cap = cv2.VideoCapture(0)
|
| 70 |
stframe = st.image([])
|
|
|
|
| 71 |
while cap.isOpened():
|
| 72 |
ret, frame = cap.read()
|
| 73 |
if not ret:
|
|
@@ -80,13 +117,16 @@ def main():
|
|
| 80 |
cap.release()
|
| 81 |
|
| 82 |
with tab4:
|
| 83 |
-
text_input = st.text_area("Enter text (
|
| 84 |
if st.button("Generate Sign"):
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
else:
|
| 89 |
-
st.
|
| 90 |
|
| 91 |
with st.sidebar:
|
| 92 |
st.markdown("<h2 style='font-size:28px; font-weight: bold; color: #4CAF50;'>Menu</h2>", unsafe_allow_html=True)
|
|
@@ -103,4 +143,5 @@ def main():
|
|
| 103 |
if st.button("π¬ Feedback", use_container_width=True):
|
| 104 |
st.text_area("We value your feedback! Please share your thoughts below:")
|
| 105 |
|
| 106 |
-
if __name__ == "__main__":
|
|
|
|
|
|
| 17 |
processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")
|
| 18 |
|
| 19 |
# Placeholder sign labels
|
| 20 |
+
sign_labels = {
|
| 21 |
+
0: "Hello",
|
| 22 |
+
1: "Thank You",
|
| 23 |
+
2: "Yes",
|
| 24 |
+
3: "No",
|
| 25 |
+
4: "Please"
|
| 26 |
+
}
|
| 27 |
|
| 28 |
# Function to classify sign and refine using Groq API
|
| 29 |
def classify_sign(image):
|
|
|
|
| 31 |
inputs = processor(images=image, return_tensors="pt")
|
| 32 |
prediction = inputs['pixel_values'].argmax().item()
|
| 33 |
gesture = sign_labels.get(prediction % len(sign_labels), "Unknown Sign")
|
| 34 |
+
|
| 35 |
if GROQ_API_KEY:
|
| 36 |
+
response = requests.post(
|
| 37 |
+
GROQ_API_URL,
|
| 38 |
+
headers={
|
| 39 |
+
"Content-Type": "application/json",
|
| 40 |
+
"Authorization": f"Bearer {GROQ_API_KEY}"
|
| 41 |
+
},
|
| 42 |
+
json={
|
| 43 |
+
"model": "llama-3.3-70b-versatile",
|
| 44 |
+
"messages": [{"role": "user", "content": f"Refine this detected sign: {gesture}"}]
|
| 45 |
+
}
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
if response.status_code == 200:
|
| 49 |
return response.json()['choices'][0]['message']['content']
|
| 50 |
+
|
| 51 |
return gesture
|
| 52 |
|
| 53 |
+
# Function to generate sign video from text
|
| 54 |
def generate_sign_video(text):
|
| 55 |
if GROQ_API_KEY:
|
| 56 |
+
response = requests.post(
|
| 57 |
+
GROQ_API_URL,
|
| 58 |
+
headers={
|
| 59 |
+
"Content-Type": "application/json",
|
| 60 |
+
"Authorization": f"Bearer {GROQ_API_KEY}"
|
| 61 |
+
},
|
| 62 |
+
json={
|
| 63 |
+
"model": "llama-3.3-70b-versatile",
|
| 64 |
+
"messages": [{"role": "user", "content": f"Generate sign language video for: {text}"}]
|
| 65 |
+
}
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
if response.status_code == 200:
|
| 69 |
+
video_url = response.json().get('video_url', '#')
|
| 70 |
+
return video_url
|
| 71 |
+
|
| 72 |
return None
|
| 73 |
|
| 74 |
# Streamlit UI
|
| 75 |
+
|
| 76 |
def main():
|
| 77 |
st.set_page_config(page_title="Sign Language Translator", layout="wide")
|
| 78 |
st.markdown("<h1 style='text-align: center; font-size: 40px; font-weight: bold; color: #4CAF50;'>π€ Sign Language Translator</h1>", unsafe_allow_html=True)
|
| 79 |
|
| 80 |
+
tab1, tab2, tab3, tab4 = st.tabs([
|
| 81 |
+
"πΈ **Image Load**",
|
| 82 |
+
"π· **Take Picture**",
|
| 83 |
+
"π₯ **Live**",
|
| 84 |
+
"π **Text2Sign**"
|
| 85 |
+
])
|
| 86 |
|
| 87 |
with tab1:
|
| 88 |
uploaded_image = st.file_uploader("Upload an image of a hand gesture", type=["png", "jpg", "jpeg"])
|
|
|
|
| 104 |
if st.button("Enable Cam", key="enable_cam"):
|
| 105 |
cap = cv2.VideoCapture(0)
|
| 106 |
stframe = st.image([])
|
| 107 |
+
|
| 108 |
while cap.isOpened():
|
| 109 |
ret, frame = cap.read()
|
| 110 |
if not ret:
|
|
|
|
| 117 |
cap.release()
|
| 118 |
|
| 119 |
with tab4:
|
| 120 |
+
text_input = st.text_area("Enter text to generate sign language (Max 200 characters)", max_chars=200)
|
| 121 |
if st.button("Generate Sign"):
|
| 122 |
+
if text_input:
|
| 123 |
+
video_url = generate_sign_video(text_input)
|
| 124 |
+
if video_url:
|
| 125 |
+
st.video(video_url)
|
| 126 |
+
else:
|
| 127 |
+
st.error("Failed to generate sign language video.")
|
| 128 |
else:
|
| 129 |
+
st.warning("Please enter some text.")
|
| 130 |
|
| 131 |
with st.sidebar:
|
| 132 |
st.markdown("<h2 style='font-size:28px; font-weight: bold; color: #4CAF50;'>Menu</h2>", unsafe_allow_html=True)
|
|
|
|
| 143 |
if st.button("π¬ Feedback", use_container_width=True):
|
| 144 |
st.text_area("We value your feedback! Please share your thoughts below:")
|
| 145 |
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
main()
|