Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
api.py
CHANGED
|
@@ -43,6 +43,12 @@ async def startup_event():
|
|
| 43 |
if os.path.exists(model_path):
|
| 44 |
pipeline.load_model(model_path)
|
| 45 |
logger.info("Pre-trained model loaded successfully.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
else:
|
| 47 |
logger.warning(f"No pre-trained model found at {model_path}. You must generate it first!")
|
| 48 |
|
|
@@ -50,6 +56,61 @@ async def startup_event():
|
|
| 50 |
except Exception as e:
|
| 51 |
logger.error(f"Failed to initialize pipeline: {e}")
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
@app.post("/analyze")
|
| 54 |
async def analyze_video_endpoint(
|
| 55 |
file: UploadFile = File(...),
|
|
|
|
| 43 |
if os.path.exists(model_path):
|
| 44 |
pipeline.load_model(model_path)
|
| 45 |
logger.info("Pre-trained model loaded successfully.")
|
| 46 |
+
|
| 47 |
+
# Also load calibrator profiles
|
| 48 |
+
calib_path = os.path.join("models", "subject_calibrator.json")
|
| 49 |
+
if os.path.exists(calib_path):
|
| 50 |
+
pipeline.calibrator.load_profiles(calib_path)
|
| 51 |
+
logger.info(f"Loaded calibration profiles from {calib_path}")
|
| 52 |
else:
|
| 53 |
logger.warning(f"No pre-trained model found at {model_path}. You must generate it first!")
|
| 54 |
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
logger.error(f"Failed to initialize pipeline: {e}")
|
| 58 |
|
| 59 |
+
@app.post("/calibrate")
|
| 60 |
+
async def calibrate_subject_endpoint(
|
| 61 |
+
file: UploadFile = File(...),
|
| 62 |
+
subject_id: str = Form(...)
|
| 63 |
+
):
|
| 64 |
+
if not pipeline:
|
| 65 |
+
raise HTTPException(status_code=500, detail="ML Pipeline not initialized yet")
|
| 66 |
+
|
| 67 |
+
# Create temporary directory
|
| 68 |
+
temp_dir = tempfile.mkdtemp(prefix="deception_calib_")
|
| 69 |
+
video_path = os.path.join(temp_dir, file.filename)
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
# Save video
|
| 73 |
+
with open(video_path, "wb") as f:
|
| 74 |
+
content = await file.read()
|
| 75 |
+
f.write(content)
|
| 76 |
+
|
| 77 |
+
logger.info(f"CALIBRATING subject: {subject_id}")
|
| 78 |
+
|
| 79 |
+
# Extract features from the TRUTHFUL video
|
| 80 |
+
facial_df = pipeline.facial_extractor.process_video(video_path)
|
| 81 |
+
|
| 82 |
+
# Extract audio features
|
| 83 |
+
audio_temp = os.path.join(temp_dir, "audio.wav")
|
| 84 |
+
from src.utils.io import extract_audio_from_video
|
| 85 |
+
audio_path = extract_audio_from_video(video_path, audio_temp)
|
| 86 |
+
audio_df = pipeline.audio_extractor.process_audio_segments(str(audio_path), segment_duration=2.0)
|
| 87 |
+
|
| 88 |
+
# Fuse
|
| 89 |
+
aligned_df = pipeline.fusion.align_features(facial_df, audio_df, video_fps=pipeline.config.features.fps)
|
| 90 |
+
fused_df = pipeline.fusion.extract_fusion_features(aligned_df)
|
| 91 |
+
|
| 92 |
+
# Add to subject calibrator
|
| 93 |
+
pipeline.calibrator.add_subject_profile(subject_id, fused_df)
|
| 94 |
+
|
| 95 |
+
# Save profiles to disk so they persist after restart
|
| 96 |
+
calib_path = os.path.join("models", "subject_calibrator.json")
|
| 97 |
+
pipeline.calibrator.save_profiles(calib_path)
|
| 98 |
+
|
| 99 |
+
return {
|
| 100 |
+
"status": "success",
|
| 101 |
+
"message": f"Calibration complete for {subject_id}",
|
| 102 |
+
"frames_analyzed": len(fused_df)
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
except Exception as e:
|
| 106 |
+
logger.error(f"Calibration failed: {str(e)}")
|
| 107 |
+
raise HTTPException(status_code=500, detail=f"Calibration failed: {str(e)}")
|
| 108 |
+
finally:
|
| 109 |
+
try:
|
| 110 |
+
if os.path.exists(video_path): os.remove(video_path)
|
| 111 |
+
os.rmdir(temp_dir)
|
| 112 |
+
except: pass
|
| 113 |
+
|
| 114 |
@app.post("/analyze")
|
| 115 |
async def analyze_video_endpoint(
|
| 116 |
file: UploadFile = File(...),
|