Fayza38 commited on
Commit
b637bd0
·
verified ·
1 Parent(s): 1851d76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -47
app.py CHANGED
@@ -74,27 +74,21 @@ def background_processing(session_data: dict):
74
 
75
  session_dir = os.path.join(RESULT_DIR, session_id)
76
  os.makedirs(session_dir, exist_ok=True)
77
-
78
- print(f"[LOG] Processing started for session: {session_id}")
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
-
98
  for q in session_data.get('answers', []):
99
  if q.get('isAnswered'):
100
  final_questions.append({
@@ -106,51 +100,36 @@ def background_processing(session_data: dict):
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,
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,
@@ -159,18 +138,14 @@ def background_processing(session_data: dict):
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)
169
- if os.path.exists(session_dir):
170
- shutil.rmtree(session_dir)
171
-
172
  except Exception as e:
173
- print(f"[CALLBACK/CLEANUP ERROR]: {e}")
 
174
 
175
  @app.get("/")
176
  async def root():
 
74
 
75
  session_dir = os.path.join(RESULT_DIR, session_id)
76
  os.makedirs(session_dir, exist_ok=True)
 
 
 
77
  local_input_path = os.path.join(UPLOAD_DIR, f"{session_id}_input.mp4")
78
 
79
+ # 1. Download
80
  try:
81
  response = http.get(str(video_url), stream=True, timeout=300)
82
  response.raise_for_status()
83
  with open(local_input_path, 'wb') as f:
84
  for chunk in response.iter_content(chunk_size=1024*1024):
85
  f.write(chunk)
 
86
  except Exception as e:
87
  print(f"[DOWNLOAD ERROR]: {e}")
88
  return
89
 
90
+ # 2. Prepare Data
91
+ final_questions, skipped_failed_reports = [], []
 
 
92
  for q in session_data.get('answers', []):
93
  if q.get('isAnswered'):
94
  final_questions.append({
 
100
  })
101
  else:
102
  skipped_failed_reports.append({
103
+ "questionId": q['aiQuestionId'], "userAnswerText": "N/A",
 
104
  "score": 0.0, "relevance": 0.0, "confidence": 0.0,
105
+ "stress": 0.0, "clarity": 0.0, "pauses": 0.0, "toneOfVoice": 3,
 
106
  "status": "skipped" if q.get('isSkipped') else "failed"
107
  })
108
 
109
+ ai_results, final_video_url = [], None
 
110
 
111
+ # 3. Run Pipeline
112
  if final_questions:
113
+ print(f"[LOG] Running pipeline...")
114
+ # Synchronized return: receive both paths
115
+ final_video_path, report_path = run_intervision_pipeline(local_input_path, final_questions, session_dir)
116
+
 
 
 
 
 
 
117
  if os.path.exists(report_path):
118
  with open(report_path, "r") as f:
119
+ # This now finds the key 'listOfAnswerReport'
120
  ai_results = json.load(f).get("listOfAnswerReport", [])
121
 
 
122
  if os.path.exists(final_video_path):
 
123
  try:
124
  upload_res = cloudinary.uploader.upload(
125
+ final_video_path, public_id=f"res_{session_id}",
126
+ folder="intervision_results", resource_type="video"
 
 
127
  )
128
  final_video_url = upload_res.get("secure_url")
 
129
  except Exception as e:
130
  print(f"[UPLOAD ERROR]: {e}")
 
 
131
 
132
+ # 4. Callback
133
  final_payload = {
134
  "sessionId": session_id,
135
  "finalVideoUrl": final_video_url,
 
138
 
139
  try:
140
  callback_endpoint = f"{str(callback_url).rstrip('/')}/api/ai-callback"
141
+ requests.post(callback_endpoint, json=final_payload, timeout=30)
142
+
143
+ # Cleanup
144
+ if os.path.exists(local_input_path): os.remove(local_input_path)
145
+ if os.path.exists(session_dir): shutil.rmtree(session_dir)
 
 
 
 
 
146
  except Exception as e:
147
+ print(f"[CALLBACK ERROR]: {e}")
148
+
149
 
150
  @app.get("/")
151
  async def root():