Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import requests
|
|
|
|
| 4 |
import cloudinary
|
| 5 |
import cloudinary.uploader
|
| 6 |
from requests.adapters import HTTPAdapter
|
|
@@ -67,22 +68,20 @@ def time_to_seconds(t_str: str) -> int:
|
|
| 67 |
return h * 3600 + m * 60 + s
|
| 68 |
|
| 69 |
def background_processing(session_data: dict):
|
| 70 |
-
"""
|
| 71 |
-
Handles heavy AI processing: video download, pipeline execution,
|
| 72 |
-
result upload, and backend notification (callback).
|
| 73 |
-
"""
|
| 74 |
session_id = session_data.get('sessionId')
|
| 75 |
video_url = session_data.get('originalVideoUrl')
|
| 76 |
callback_url = session_data.get('callbackBaseUrl')
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
print(f"[LOG] Processing started for session: {session_id}")
|
| 79 |
|
| 80 |
-
# 1. Download the original video from the provided URL
|
| 81 |
local_input_path = os.path.join(UPLOAD_DIR, f"{session_id}_input.mp4")
|
| 82 |
-
|
|
|
|
| 83 |
try:
|
| 84 |
-
print(f"[LOG] Downloading video: {video_url}")
|
| 85 |
-
# Increased timeout to 300s (5 minutes) for large files
|
| 86 |
response = http.get(video_url, stream=True, timeout=300)
|
| 87 |
response.raise_for_status()
|
| 88 |
with open(local_input_path, 'wb') as f:
|
|
@@ -90,10 +89,9 @@ def background_processing(session_data: dict):
|
|
| 90 |
f.write(chunk)
|
| 91 |
except Exception as e:
|
| 92 |
print(f"[DOWNLOAD ERROR]: {e}")
|
| 93 |
-
# Notify backend that it failed due to download
|
| 94 |
return
|
| 95 |
|
| 96 |
-
|
| 97 |
final_questions = []
|
| 98 |
skipped_failed_reports = []
|
| 99 |
|
|
@@ -107,47 +105,45 @@ def background_processing(session_data: dict):
|
|
| 107 |
"end_time": time_to_seconds(q['submittedAt'])
|
| 108 |
})
|
| 109 |
else:
|
| 110 |
-
# Handle questions that weren't answered during the session
|
| 111 |
skipped_failed_reports.append({
|
| 112 |
"questionId": q['aiQuestionId'],
|
| 113 |
"userAnswerText": "N/A",
|
| 114 |
-
"score": 0.0,
|
| 115 |
-
"
|
| 116 |
-
"
|
| 117 |
-
"stress": 0.0,
|
| 118 |
-
"clarity": 0.0,
|
| 119 |
-
"pauses": 0.0,
|
| 120 |
-
"toneOfVoice": "N/A",
|
| 121 |
"status": "skipped" if q.get('isSkipped') else "failed"
|
| 122 |
})
|
| 123 |
|
| 124 |
-
# 3. Execute AI Pipeline (Analysis & Visualization)
|
| 125 |
ai_results = []
|
|
|
|
|
|
|
| 126 |
if final_questions:
|
| 127 |
-
|
| 128 |
-
run_intervision_pipeline(local_input_path, final_questions,
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
if os.path.exists(report_path):
|
| 131 |
with open(report_path, "r") as f:
|
| 132 |
ai_results = json.load(f).get("listOfAnswerReport", [])
|
| 133 |
|
| 134 |
-
|
| 135 |
-
final_video_path = os.path.join(RESULT_DIR, "Intervision_Final_Result.mp4")
|
| 136 |
final_video_url = None
|
| 137 |
-
if os.path.exists(final_video_path):
|
| 138 |
try:
|
| 139 |
upload_res = cloudinary.uploader.upload(
|
| 140 |
final_video_path,
|
| 141 |
public_id=f"res_{session_id}",
|
| 142 |
folder="intervision_results",
|
| 143 |
-
resource_type="video"
|
| 144 |
-
chunk_size=6000000
|
| 145 |
)
|
| 146 |
final_video_url = upload_res.get("secure_url")
|
| 147 |
except Exception as e:
|
| 148 |
print(f"[UPLOAD ERROR]: {e}")
|
| 149 |
|
| 150 |
-
|
| 151 |
final_payload = {
|
| 152 |
"sessionId": session_id,
|
| 153 |
"finalVideoUrl": final_video_url,
|
|
@@ -155,16 +151,17 @@ def background_processing(session_data: dict):
|
|
| 155 |
}
|
| 156 |
|
| 157 |
try:
|
| 158 |
-
|
| 159 |
-
cb_response = requests.post(f"{callback_url}/api/ai-callback", json=final_payload, timeout=30)
|
| 160 |
-
print(f"[LOG] Callback sent to {callback_url}. Status: {cb_response.status_code}")
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
if os.path.exists(
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
except Exception as e:
|
| 167 |
-
print(f"[CALLBACK ERROR]: {e}")
|
| 168 |
|
| 169 |
@app.get("/")
|
| 170 |
async def root():
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import requests
|
| 4 |
+
import shutil
|
| 5 |
import cloudinary
|
| 6 |
import cloudinary.uploader
|
| 7 |
from requests.adapters import HTTPAdapter
|
|
|
|
| 68 |
return h * 3600 + m * 60 + s
|
| 69 |
|
| 70 |
def background_processing(session_data: dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
session_id = session_data.get('sessionId')
|
| 72 |
video_url = session_data.get('originalVideoUrl')
|
| 73 |
callback_url = session_data.get('callbackBaseUrl')
|
| 74 |
|
| 75 |
+
|
| 76 |
+
session_dir = os.path.join(RESULT_DIR, session_id)
|
| 77 |
+
os.makedirs(session_dir, exist_ok=True)
|
| 78 |
+
|
| 79 |
print(f"[LOG] Processing started for session: {session_id}")
|
| 80 |
|
|
|
|
| 81 |
local_input_path = os.path.join(UPLOAD_DIR, f"{session_id}_input.mp4")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
try:
|
|
|
|
|
|
|
| 85 |
response = http.get(video_url, stream=True, timeout=300)
|
| 86 |
response.raise_for_status()
|
| 87 |
with open(local_input_path, 'wb') as f:
|
|
|
|
| 89 |
f.write(chunk)
|
| 90 |
except Exception as e:
|
| 91 |
print(f"[DOWNLOAD ERROR]: {e}")
|
|
|
|
| 92 |
return
|
| 93 |
|
| 94 |
+
|
| 95 |
final_questions = []
|
| 96 |
skipped_failed_reports = []
|
| 97 |
|
|
|
|
| 105 |
"end_time": time_to_seconds(q['submittedAt'])
|
| 106 |
})
|
| 107 |
else:
|
|
|
|
| 108 |
skipped_failed_reports.append({
|
| 109 |
"questionId": q['aiQuestionId'],
|
| 110 |
"userAnswerText": "N/A",
|
| 111 |
+
"score": 0.0, "relevance": 0.0, "confidence": 0.0,
|
| 112 |
+
"stress": 0.0, "clarity": 0.0, "pauses": 0.0,
|
| 113 |
+
"toneOfVoice": 3, # Natural/NA
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
"status": "skipped" if q.get('isSkipped') else "failed"
|
| 115 |
})
|
| 116 |
|
|
|
|
| 117 |
ai_results = []
|
| 118 |
+
final_video_path = None
|
| 119 |
+
|
| 120 |
if final_questions:
|
| 121 |
+
|
| 122 |
+
run_intervision_pipeline(local_input_path, final_questions, session_dir)
|
| 123 |
+
|
| 124 |
+
report_path = os.path.join(session_dir, "report.json")
|
| 125 |
+
|
| 126 |
+
final_video_path = os.path.join(session_dir, "Intervision_Final_Report.mp4")
|
| 127 |
+
|
| 128 |
if os.path.exists(report_path):
|
| 129 |
with open(report_path, "r") as f:
|
| 130 |
ai_results = json.load(f).get("listOfAnswerReport", [])
|
| 131 |
|
| 132 |
+
|
|
|
|
| 133 |
final_video_url = None
|
| 134 |
+
if final_video_path and os.path.exists(final_video_path):
|
| 135 |
try:
|
| 136 |
upload_res = cloudinary.uploader.upload(
|
| 137 |
final_video_path,
|
| 138 |
public_id=f"res_{session_id}",
|
| 139 |
folder="intervision_results",
|
| 140 |
+
resource_type="video"
|
|
|
|
| 141 |
)
|
| 142 |
final_video_url = upload_res.get("secure_url")
|
| 143 |
except Exception as e:
|
| 144 |
print(f"[UPLOAD ERROR]: {e}")
|
| 145 |
|
| 146 |
+
|
| 147 |
final_payload = {
|
| 148 |
"sessionId": session_id,
|
| 149 |
"finalVideoUrl": final_video_url,
|
|
|
|
| 151 |
}
|
| 152 |
|
| 153 |
try:
|
| 154 |
+
requests.post(f"{callback_url}/api/ai-callback", json=final_payload, timeout=30)
|
|
|
|
|
|
|
| 155 |
|
| 156 |
+
|
| 157 |
+
print(f"[LOG] Cleaning up session {session_id}...")
|
| 158 |
+
if os.path.exists(local_input_path):
|
| 159 |
+
os.remove(local_input_path)
|
| 160 |
+
if os.path.exists(session_dir):
|
| 161 |
+
shutil.rmtree(session_dir)
|
| 162 |
|
| 163 |
except Exception as e:
|
| 164 |
+
print(f"[CALLBACK/CLEANUP ERROR]: {e}")
|
| 165 |
|
| 166 |
@app.get("/")
|
| 167 |
async def root():
|