hantupocong commited on
Commit
126e658
Β·
verified Β·
1 Parent(s): f61da19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -24
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
- # === Inference with fallback ===
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(text, tokenizer, model, device, GLOBAL_MEAN_T, GLOBAL_STD_T)
173
- if pose is None:
 
 
 
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
- checks = []
 
188
 
189
- if cleaned_text in annotated_words:
190
- checks.append("**Phrase Check:**")
191
- checks.append(f"- β€œ{cleaned_text}” βœ… in dataset")
192
- else:
193
- checks.append("**Word-by-Word Check:**")
194
- for w in cleaned_text.split():
195
- if w in annotated_words:
196
- checks.append(f"- β€œ{w}” βœ… in dataset")
 
 
 
 
 
197
  else:
198
- checks.append(f"- β€œ{w}” ⚠️ not in dataset β€” will be generalized")
199
-
200
- # === YouTube link lookup ===
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πŸ“Ί [![Watch Reference Video]({thumbnail_url})]({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 πŸ“Ί [![Watch Video]({thumbnail_url})]({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)}"