Update app.py
Browse files
app.py
CHANGED
@@ -163,16 +163,50 @@ def save_animation(anim, format="mp4"):
|
|
163 |
print(f"{format.upper()} save failed:", e)
|
164 |
return None
|
165 |
|
166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
def predict(text, threshold):
|
168 |
if not text.strip():
|
169 |
return None, "β οΈ Please enter valid text."
|
170 |
|
171 |
try:
|
172 |
-
pose, conf, labels = concatenate_and_smooth_sequences(
|
173 |
-
|
|
|
|
|
|
|
174 |
return None, "β οΈ No pose predicted."
|
175 |
-
|
176 |
anim = animate_pose(pose, pred_conf=conf, frame_labels=labels, conf_threshold=threshold)
|
177 |
path = save_animation(anim, format="mp4")
|
178 |
if not path:
|
@@ -184,31 +218,31 @@ def predict(text, threshold):
|
|
184 |
msg = ""
|
185 |
|
186 |
cleaned_text = text.strip().lower()
|
187 |
-
|
|
|
188 |
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
197 |
else:
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
video_url = get_youtube_link(cleaned_text)
|
202 |
-
if video_url:
|
203 |
-
video_id = video_url.split("v=")[-1]
|
204 |
-
thumbnail_url = f"https://img.youtube.com/vi/{video_id}/0.jpg"
|
205 |
-
checks.append(f"\nπΊ []({video_url})")
|
206 |
-
else:
|
207 |
-
checks.append(f"\nβ οΈ No YouTube video found for β{cleaned_text}β β the predicted pose is based on the modelβs learned approximation.")
|
208 |
|
|
|
209 |
|
210 |
return path, msg + "\n" + "\n".join(checks)
|
211 |
-
|
212 |
except Exception as e:
|
213 |
print("Error during prediction:", e)
|
214 |
return None, f"β Runtime error: {str(e)}"
|
|
|
163 |
print(f"{format.upper()} save failed:", e)
|
164 |
return None
|
165 |
|
166 |
+
def find_all_matches(text, video_lookup):
|
167 |
+
words = text.strip().lower().split()
|
168 |
+
matched = []
|
169 |
+
i = 0
|
170 |
+
while i < len(words):
|
171 |
+
found = False
|
172 |
+
# Try trigram
|
173 |
+
if i + 2 < len(words):
|
174 |
+
phrase3 = " ".join(words[i:i+3])
|
175 |
+
if phrase3 in video_lookup:
|
176 |
+
matched.append((phrase3, video_lookup[phrase3]))
|
177 |
+
i += 3
|
178 |
+
found = True
|
179 |
+
continue
|
180 |
+
# Try bigram
|
181 |
+
if i + 1 < len(words):
|
182 |
+
phrase2 = " ".join(words[i:i+2])
|
183 |
+
if phrase2 in video_lookup:
|
184 |
+
matched.append((phrase2, video_lookup[phrase2]))
|
185 |
+
i += 2
|
186 |
+
found = True
|
187 |
+
continue
|
188 |
+
# Try unigram
|
189 |
+
word = words[i]
|
190 |
+
if word in video_lookup:
|
191 |
+
matched.append((word, video_lookup[word]))
|
192 |
+
else:
|
193 |
+
matched.append((word, None)) # not in lookup
|
194 |
+
i += 1
|
195 |
+
return matched
|
196 |
+
|
197 |
+
|
198 |
def predict(text, threshold):
|
199 |
if not text.strip():
|
200 |
return None, "β οΈ Please enter valid text."
|
201 |
|
202 |
try:
|
203 |
+
pose, conf, labels = concatenate_and_smooth_sequences(
|
204 |
+
text, tokenizer, model, device, GLOBAL_MEAN_T, GLOBAL_STD_T
|
205 |
+
)
|
206 |
+
|
207 |
+
if pose is None:
|
208 |
return None, "β οΈ No pose predicted."
|
209 |
+
|
210 |
anim = animate_pose(pose, pred_conf=conf, frame_labels=labels, conf_threshold=threshold)
|
211 |
path = save_animation(anim, format="mp4")
|
212 |
if not path:
|
|
|
218 |
msg = ""
|
219 |
|
220 |
cleaned_text = text.strip().lower()
|
221 |
+
matches = find_all_matches(cleaned_text, video_lookup)
|
222 |
+
checks = ["**Match Check (Phrase + Word Level):**"]
|
223 |
|
224 |
+
for phrase, url in matches:
|
225 |
+
# Dataset presence
|
226 |
+
if phrase in annotated_words:
|
227 |
+
line = f'- β{phrase}β β
in dataset'
|
228 |
+
else:
|
229 |
+
line = f'- β{phrase}β β οΈ not in dataset β model will approximate'
|
230 |
+
|
231 |
+
# YouTube thumbnail
|
232 |
+
if url:
|
233 |
+
if "v=" in url:
|
234 |
+
video_id = url.split("v=")[-1].split("&")[0]
|
235 |
+
thumbnail_url = f"https://img.youtube.com/vi/{video_id}/0.jpg"
|
236 |
+
line += f'\n πΊ []({url})'
|
237 |
else:
|
238 |
+
line += f'\n π [Reference Video]({url})'
|
239 |
+
else:
|
240 |
+
line += f'\n β οΈ No YouTube video found for β{phrase}β'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
|
242 |
+
checks.append(line)
|
243 |
|
244 |
return path, msg + "\n" + "\n".join(checks)
|
245 |
+
|
246 |
except Exception as e:
|
247 |
print("Error during prediction:", e)
|
248 |
return None, f"β Runtime error: {str(e)}"
|