Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1115,63 +1115,114 @@ def send_message(recipient_id, message_text):
|
|
| 1115 |
def process_messenger_video(video_url, sender_id):
|
| 1116 |
"""處理來自 Messenger 的影片(HuggingFace 整合版本)"""
|
| 1117 |
import tempfile
|
| 1118 |
-
|
|
|
|
| 1119 |
try:
|
| 1120 |
print(f"🎬 開始處理 Messenger 影片:{video_url}")
|
| 1121 |
-
|
| 1122 |
-
#
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
| 1127 |
-
|
| 1128 |
-
|
| 1129 |
-
|
| 1130 |
-
# 創建臨時檔案
|
| 1131 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4', prefix=f'messenger_{sender_id}_') as temp_file:
|
| 1132 |
-
file_path = temp_file.name
|
| 1133 |
-
|
| 1134 |
-
# 寫入檔案
|
| 1135 |
-
for chunk in response.iter_content(chunk_size=8192):
|
| 1136 |
-
if chunk:
|
| 1137 |
-
temp_file.write(chunk)
|
| 1138 |
-
|
| 1139 |
-
print(f"✅ 影片下載完成:{file_path}")
|
| 1140 |
-
|
| 1141 |
-
# 初始化影片辨識器
|
| 1142 |
-
model_path = MODEL_PATH
|
| 1143 |
-
video_recognizer = VideoSignLanguageRecognizer(model_path, threshold=0.5)
|
| 1144 |
-
|
| 1145 |
-
# 處理影片
|
| 1146 |
-
result = video_recognizer.process_video(file_path)
|
| 1147 |
-
|
| 1148 |
-
# 清理臨時檔案
|
| 1149 |
-
try:
|
| 1150 |
-
os.remove(file_path)
|
| 1151 |
-
except:
|
| 1152 |
-
pass
|
| 1153 |
-
|
| 1154 |
-
if result:
|
| 1155 |
-
generated_sentence = result.get('generated_sentence', '無法辨識手語內容')
|
| 1156 |
-
confidence = result.get('confidence', 0.0)
|
| 1157 |
-
word_sequence = result.get('word_sequence', [])
|
| 1158 |
-
|
| 1159 |
-
print(f"✅ 手語辨識完成 - 用戶:{sender_id}")
|
| 1160 |
-
print(f"📝 模型辨識:{word_sequence}")
|
| 1161 |
-
print(f"💬 GPT翻譯:{generated_sentence}")
|
| 1162 |
-
print(f"🎯 信心度:{confidence:.2f}")
|
| 1163 |
-
|
| 1164 |
-
# 發送GPT翻譯結果給用戶
|
| 1165 |
-
send_message(sender_id, generated_sentence)
|
| 1166 |
-
else:
|
| 1167 |
-
send_message(sender_id, "抱歉,無法辨識您的手語內容,請再試一次。")
|
| 1168 |
-
|
| 1169 |
-
# 釋放 Mediapipe 資源
|
| 1170 |
try:
|
| 1171 |
-
|
| 1172 |
-
|
| 1173 |
-
|
| 1174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1175 |
except Exception as e:
|
| 1176 |
print(f"處理 Messenger 影片時發生錯誤:{e}")
|
| 1177 |
send_message(sender_id, "處理影片時發生錯誤,請稍後再試。")
|
|
|
|
| 1115 |
def process_messenger_video(video_url, sender_id):
|
| 1116 |
"""處理來自 Messenger 的影片(HuggingFace 整合版本)"""
|
| 1117 |
import tempfile
|
| 1118 |
+
import time
|
| 1119 |
+
|
| 1120 |
try:
|
| 1121 |
print(f"🎬 開始處理 Messenger 影片:{video_url}")
|
| 1122 |
+
|
| 1123 |
+
# 驗證影片 URL 格式
|
| 1124 |
+
if not video_url or 'xx.fbcdn.net' in video_url:
|
| 1125 |
+
print(f"❌ 影片 URL 格式無效或包含佔位符:{video_url}")
|
| 1126 |
+
send_message(sender_id, "影片 URL 格式無效,請重新發送影片。")
|
| 1127 |
+
return
|
| 1128 |
+
|
| 1129 |
+
# 檢查 URL 是否可訪問(輕量級檢查)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1130 |
try:
|
| 1131 |
+
# 使用 HEAD 請求檢查 URL 是否可訪問
|
| 1132 |
+
head_response = requests.head(video_url, timeout=10, verify=False, allow_redirects=True)
|
| 1133 |
+
if head_response.status_code != 200:
|
| 1134 |
+
print(f"❌ 影片 URL 不可訪問,狀態碼:{head_response.status_code}")
|
| 1135 |
+
send_message(sender_id, "影片連結已過期或無法訪問,請重新發送影片。")
|
| 1136 |
+
return
|
| 1137 |
+
except requests.exceptions.RequestException as e:
|
| 1138 |
+
print(f"❌ 影片 URL 檢查失敗:{e}")
|
| 1139 |
+
send_message(sender_id, "影片連結檢查失敗,請重新發送影片。")
|
| 1140 |
+
return
|
| 1141 |
+
|
| 1142 |
+
# 重試下載邏輯
|
| 1143 |
+
max_retries = 3
|
| 1144 |
+
retry_delay = 2 # 初始延遲 2 秒
|
| 1145 |
+
|
| 1146 |
+
for attempt in range(max_retries):
|
| 1147 |
+
try:
|
| 1148 |
+
print(f"📥 嘗試下載影片(第 {attempt + 1} 次)")
|
| 1149 |
+
|
| 1150 |
+
# 下載影片
|
| 1151 |
+
response = requests.get(video_url, stream=True, timeout=60, verify=False)
|
| 1152 |
+
response.raise_for_status()
|
| 1153 |
+
|
| 1154 |
+
# 使用臨時檔案避免權限問題
|
| 1155 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 1156 |
+
filename = f"messenger_video_{sender_id}_{timestamp}.mp4"
|
| 1157 |
+
|
| 1158 |
+
# 創建臨時檔案
|
| 1159 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4', prefix=f'messenger_{sender_id}_') as temp_file:
|
| 1160 |
+
file_path = temp_file.name
|
| 1161 |
+
|
| 1162 |
+
# 寫入檔案
|
| 1163 |
+
downloaded_size = 0
|
| 1164 |
+
for chunk in response.iter_content(chunk_size=8192):
|
| 1165 |
+
if chunk:
|
| 1166 |
+
temp_file.write(chunk)
|
| 1167 |
+
downloaded_size += len(chunk)
|
| 1168 |
+
|
| 1169 |
+
# 檢查下載的檔案大小
|
| 1170 |
+
if downloaded_size < 1024: # 小於 1KB 可能是錯誤
|
| 1171 |
+
raise ValueError(f"下載的檔案太小:{downloaded_size} bytes")
|
| 1172 |
+
|
| 1173 |
+
print(f"✅ 影片下載完成:{file_path} ({downloaded_size} bytes)")
|
| 1174 |
+
|
| 1175 |
+
# 初始化影片辨識器
|
| 1176 |
+
model_path = MODEL_PATH
|
| 1177 |
+
video_recognizer = VideoSignLanguageRecognizer(model_path, threshold=0.5)
|
| 1178 |
+
|
| 1179 |
+
# 處理影片
|
| 1180 |
+
result = video_recognizer.process_video(file_path)
|
| 1181 |
+
|
| 1182 |
+
# 清理臨時檔案
|
| 1183 |
+
try:
|
| 1184 |
+
os.remove(file_path)
|
| 1185 |
+
except:
|
| 1186 |
+
pass
|
| 1187 |
+
|
| 1188 |
+
if result:
|
| 1189 |
+
generated_sentence = result.get('generated_sentence', '無法辨識手語內容')
|
| 1190 |
+
confidence = result.get('confidence', 0.0)
|
| 1191 |
+
word_sequence = result.get('word_sequence', [])
|
| 1192 |
+
|
| 1193 |
+
print(f"✅ 手語辨識完成 - 用戶:{sender_id}")
|
| 1194 |
+
print(f"📝 模型辨識:{word_sequence}")
|
| 1195 |
+
print(f"💬 GPT翻譯:{generated_sentence}")
|
| 1196 |
+
print(f"🎯 信心度:{confidence:.2f}")
|
| 1197 |
+
|
| 1198 |
+
# 發送GPT翻譯結果給用戶
|
| 1199 |
+
send_message(sender_id, generated_sentence)
|
| 1200 |
+
else:
|
| 1201 |
+
send_message(sender_id, "抱歉,無法辨識您的手語內容,請再試一次。")
|
| 1202 |
+
|
| 1203 |
+
# 釋放 Mediapipe 資源
|
| 1204 |
+
try:
|
| 1205 |
+
video_recognizer.feature_extractor.close()
|
| 1206 |
+
except Exception:
|
| 1207 |
+
pass
|
| 1208 |
+
|
| 1209 |
+
return # 成功處理,退出函數
|
| 1210 |
+
|
| 1211 |
+
except requests.exceptions.RequestException as e:
|
| 1212 |
+
print(f"❌ 下載失敗(第 {attempt + 1} 次):{e}")
|
| 1213 |
+
if attempt < max_retries - 1:
|
| 1214 |
+
print(f"⏳ {retry_delay} 秒後重試...")
|
| 1215 |
+
time.sleep(retry_delay)
|
| 1216 |
+
retry_delay *= 2 # 指數退避
|
| 1217 |
+
else:
|
| 1218 |
+
print("❌ 所有重試次數已用完")
|
| 1219 |
+
send_message(sender_id, "影片下載失敗,請檢查網路連線後重新發送影片。")
|
| 1220 |
+
|
| 1221 |
+
except Exception as e:
|
| 1222 |
+
print(f"❌ 處理影片時發生錯誤:{e}")
|
| 1223 |
+
send_message(sender_id, "處理影片時發生錯誤,請稍後再試。")
|
| 1224 |
+
return
|
| 1225 |
+
|
| 1226 |
except Exception as e:
|
| 1227 |
print(f"處理 Messenger 影片時發生錯誤:{e}")
|
| 1228 |
send_message(sender_id, "處理影片時發生錯誤,請稍後再試。")
|