Update app.py
Browse files
app.py
CHANGED
|
@@ -71,7 +71,6 @@ 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)
|
|
@@ -80,18 +79,19 @@ def background_processing(session_data: dict):
|
|
| 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:
|
| 88 |
for chunk in response.iter_content(chunk_size=1024*1024):
|
| 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 |
|
|
@@ -110,40 +110,47 @@ def background_processing(session_data: dict):
|
|
| 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,
|
| 114 |
"status": "skipped" if q.get('isSkipped') else "failed"
|
| 115 |
})
|
| 116 |
|
| 117 |
ai_results = []
|
| 118 |
-
|
| 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 |
-
|
| 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,9 +158,11 @@ def background_processing(session_data: dict):
|
|
| 151 |
}
|
| 152 |
|
| 153 |
try:
|
| 154 |
-
|
| 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)
|
|
|
|
| 71 |
session_id = session_data.get('sessionId')
|
| 72 |
video_url = session_data.get('originalVideoUrl')
|
| 73 |
callback_url = session_data.get('callbackBaseUrl')
|
|
|
|
| 74 |
|
| 75 |
session_dir = os.path.join(RESULT_DIR, session_id)
|
| 76 |
os.makedirs(session_dir, exist_ok=True)
|
|
|
|
| 79 |
|
| 80 |
local_input_path = os.path.join(UPLOAD_DIR, f"{session_id}_input.mp4")
|
| 81 |
|
| 82 |
+
# 1. Download the original video from Cloudinary/URL
|
| 83 |
try:
|
| 84 |
+
response = http.get(str(video_url), stream=True, timeout=300)
|
| 85 |
response.raise_for_status()
|
| 86 |
with open(local_input_path, 'wb') as f:
|
| 87 |
for chunk in response.iter_content(chunk_size=1024*1024):
|
| 88 |
f.write(chunk)
|
| 89 |
+
print(f"[LOG] Download complete: {local_input_path}")
|
| 90 |
except Exception as e:
|
| 91 |
print(f"[DOWNLOAD ERROR]: {e}")
|
| 92 |
return
|
| 93 |
|
| 94 |
+
# 2. Prepare questions for the pipeline
|
| 95 |
final_questions = []
|
| 96 |
skipped_failed_reports = []
|
| 97 |
|
|
|
|
| 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,
|
| 114 |
"status": "skipped" if q.get('isSkipped') else "failed"
|
| 115 |
})
|
| 116 |
|
| 117 |
ai_results = []
|
| 118 |
+
final_video_url = None
|
| 119 |
|
| 120 |
+
# 3. Run the AI Pipeline
|
| 121 |
if final_questions:
|
| 122 |
+
print(f"[LOG] Running pipeline for {len(final_questions)} questions...")
|
| 123 |
+
# Capture the return message to ensure the pipeline finished
|
| 124 |
+
pipeline_status = run_intervision_pipeline(local_input_path, final_questions, session_dir)
|
| 125 |
+
print(f"[LOG] Pipeline Status: {pipeline_status}")
|
| 126 |
|
|
|
|
|
|
|
| 127 |
report_path = os.path.join(session_dir, "report.json")
|
| 128 |
+
# Ensure this filename exactly matches the one inside run_intervision_pipeline
|
| 129 |
final_video_path = os.path.join(session_dir, "Intervision_Final_Report.mp4")
|
| 130 |
|
| 131 |
+
# Parse JSON results
|
| 132 |
if os.path.exists(report_path):
|
| 133 |
with open(report_path, "r") as f:
|
| 134 |
ai_results = json.load(f).get("listOfAnswerReport", [])
|
| 135 |
|
| 136 |
+
# 4. Upload the generated video to Cloudinary
|
| 137 |
+
if os.path.exists(final_video_path):
|
| 138 |
+
print(f"[LOG] Uploading final video to Cloudinary...")
|
| 139 |
+
try:
|
| 140 |
+
upload_res = cloudinary.uploader.upload(
|
| 141 |
+
final_video_path,
|
| 142 |
+
public_id=f"res_{session_id}",
|
| 143 |
+
folder="intervision_results",
|
| 144 |
+
resource_type="video"
|
| 145 |
+
)
|
| 146 |
+
final_video_url = upload_res.get("secure_url")
|
| 147 |
+
print(f"[LOG] Upload successful: {final_video_url}")
|
| 148 |
+
except Exception as e:
|
| 149 |
+
print(f"[UPLOAD ERROR]: {e}")
|
| 150 |
+
else:
|
| 151 |
+
print(f"[ERROR] Final video file not found at {final_video_path}")
|
| 152 |
|
| 153 |
+
# 5. Send results back via Callback
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
final_payload = {
|
| 155 |
"sessionId": session_id,
|
| 156 |
"finalVideoUrl": final_video_url,
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
try:
|
| 161 |
+
callback_endpoint = f"{str(callback_url).rstrip('/')}/api/ai-callback"
|
| 162 |
+
callback_resp = requests.post(callback_endpoint, json=final_payload, timeout=30)
|
| 163 |
+
print(f"[LOG] Callback sent. Status: {callback_resp.status_code}")
|
| 164 |
|
| 165 |
+
# 6. Cleanup local storage
|
| 166 |
print(f"[LOG] Cleaning up session {session_id}...")
|
| 167 |
if os.path.exists(local_input_path):
|
| 168 |
os.remove(local_input_path)
|