Commit
·
d898dac
1
Parent(s):
49f7b31
Deploy
Browse files- app.py +32 -0
- inference.py +314 -0
- models.py +137 -0
- requirements.txt +13 -0
- saved_models/checkpoint.pth +3 -0
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from inference import model_fn, predict_fn
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
model_dict = model_fn(".")
|
| 6 |
+
|
| 7 |
+
def predict(video):
|
| 8 |
+
temp_path = "temp.mp4"
|
| 9 |
+
try:
|
| 10 |
+
# Gradio gives a file path for video input
|
| 11 |
+
if isinstance(video, str) and os.path.exists(video):
|
| 12 |
+
os.rename(video, temp_path)
|
| 13 |
+
else:
|
| 14 |
+
with open(temp_path, "wb") as f:
|
| 15 |
+
f.write(video.read())
|
| 16 |
+
input_data = {"video_path": temp_path}
|
| 17 |
+
result = predict_fn(input_data, model_dict)
|
| 18 |
+
return result
|
| 19 |
+
finally:
|
| 20 |
+
if os.path.exists(temp_path):
|
| 21 |
+
os.remove(temp_path)
|
| 22 |
+
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=predict,
|
| 25 |
+
inputs=gr.Video(type="filepath"),
|
| 26 |
+
outputs="json",
|
| 27 |
+
title="Video Sentiment Analysis",
|
| 28 |
+
description="Upload an .mp4 video to get sentiment and emotion predictions for each utterance."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
demo.launch()
|
inference.py
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from models import MultimodalSentimentModel
|
| 3 |
+
import os
|
| 4 |
+
import cv2
|
| 5 |
+
import numpy as np
|
| 6 |
+
import subprocess
|
| 7 |
+
import torchaudio
|
| 8 |
+
from transformers import AutoTokenizer
|
| 9 |
+
import whisper
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
EMOTION_MAP = {0: "anger", 1: "disgust", 2: "fear",
|
| 13 |
+
3: "joy", 4: "neutral", 5: "sadness", 6: "surprise"}
|
| 14 |
+
SENTIMENT_MAP = {0: "negative", 1: "neutral", 2: "positive"}
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def install_ffmpeg():
|
| 18 |
+
print("Starting Ffmpeg installation...")
|
| 19 |
+
|
| 20 |
+
subprocess.check_call([sys.executable, "-m", "pip",
|
| 21 |
+
"install", "--upgrade", "pip"])
|
| 22 |
+
|
| 23 |
+
subprocess.check_call([sys.executable, "-m", "pip",
|
| 24 |
+
"install", "--upgrade", "setuptools"])
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
subprocess.check_call([sys.executable, "-m", "pip",
|
| 28 |
+
"install", "ffmpeg-python"])
|
| 29 |
+
print("Installed ffmpeg-python successfully")
|
| 30 |
+
except subprocess.CalledProcessError as e:
|
| 31 |
+
print("Failed to install ffmpeg-python via pip")
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
subprocess.check_call([
|
| 35 |
+
"wget",
|
| 36 |
+
"https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz",
|
| 37 |
+
"-O", "/tmp/ffmpeg.tar.xz"
|
| 38 |
+
])
|
| 39 |
+
|
| 40 |
+
subprocess.check_call([
|
| 41 |
+
"tar", "-xf", "/tmp/ffmpeg.tar.xz", "-C", "/tmp/"
|
| 42 |
+
])
|
| 43 |
+
|
| 44 |
+
result = subprocess.run(
|
| 45 |
+
["find", "/tmp", "-name", "ffmpeg", "-type", "f"],
|
| 46 |
+
capture_output=True,
|
| 47 |
+
text=True
|
| 48 |
+
)
|
| 49 |
+
ffmpeg_path = result.stdout.strip()
|
| 50 |
+
|
| 51 |
+
subprocess.check_call(["cp", ffmpeg_path, "/usr/local/bin/ffmpeg"])
|
| 52 |
+
|
| 53 |
+
subprocess.check_call(["chmod", "+x", "/usr/local/bin/ffmpeg"])
|
| 54 |
+
|
| 55 |
+
print("Installed static FFmpeg binary successfully")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"Failed to install static FFmpeg: {e}")
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
result = subprocess.run(["ffmpeg", "-version"],
|
| 61 |
+
capture_output=True, text=True, check=True)
|
| 62 |
+
print("FFmpeg version:")
|
| 63 |
+
print(result.stdout)
|
| 64 |
+
return True
|
| 65 |
+
except (subprocess.CalledProcessError, FileNotFoundError):
|
| 66 |
+
print("FFmpeg installation verification failed")
|
| 67 |
+
return False
|
| 68 |
+
|
| 69 |
+
class VideoProcessor:
|
| 70 |
+
def process_video(self, video_path):
|
| 71 |
+
cap = cv2.VideoCapture(video_path)
|
| 72 |
+
frames = []
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
if not cap.isOpened():
|
| 76 |
+
raise ValueError(f"Video not found: {video_path}")
|
| 77 |
+
|
| 78 |
+
# Try and read first frame to validate video
|
| 79 |
+
ret, frame = cap.read()
|
| 80 |
+
if not ret or frame is None:
|
| 81 |
+
raise ValueError(f"Video not found: {video_path}")
|
| 82 |
+
|
| 83 |
+
# Reset index to not skip first frame
|
| 84 |
+
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
|
| 85 |
+
|
| 86 |
+
while len(frames) < 30 and cap.isOpened():
|
| 87 |
+
ret, frame = cap.read()
|
| 88 |
+
if not ret:
|
| 89 |
+
break
|
| 90 |
+
|
| 91 |
+
frame = cv2.resize(frame, (224, 224))
|
| 92 |
+
frame = frame / 255.0
|
| 93 |
+
frames.append(frame)
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
raise ValueError(f"Video error: {str(e)}")
|
| 97 |
+
finally:
|
| 98 |
+
cap.release()
|
| 99 |
+
|
| 100 |
+
if (len(frames) == 0):
|
| 101 |
+
raise ValueError("No frames could be extracted")
|
| 102 |
+
|
| 103 |
+
# Pad or truncate frames
|
| 104 |
+
if len(frames) < 30:
|
| 105 |
+
frames += [np.zeros_like(frames[0])] * (30 - len(frames))
|
| 106 |
+
else:
|
| 107 |
+
frames = frames[:30]
|
| 108 |
+
|
| 109 |
+
# Before permute: [frames, height, width, channels]
|
| 110 |
+
# After permute: [frames, channels, height, width]
|
| 111 |
+
return torch.FloatTensor(np.array(frames)).permute(0, 3, 1, 2)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
class AudioProcessor:
|
| 115 |
+
def extract_features(self, video_path, max_length=300):
|
| 116 |
+
audio_path = video_path.replace('.mp4', '.wav')
|
| 117 |
+
|
| 118 |
+
try:
|
| 119 |
+
subprocess.run([
|
| 120 |
+
'ffmpeg',
|
| 121 |
+
'-i', video_path,
|
| 122 |
+
'-vn',
|
| 123 |
+
'-acodec', 'pcm_s16le',
|
| 124 |
+
'-ar', '16000',
|
| 125 |
+
'-ac', '1',
|
| 126 |
+
audio_path
|
| 127 |
+
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 128 |
+
|
| 129 |
+
waveform, sample_rate = torchaudio.load(audio_path)
|
| 130 |
+
|
| 131 |
+
if sample_rate != 16000:
|
| 132 |
+
resampler = torchaudio.transforms.Resample(sample_rate, 16000)
|
| 133 |
+
waveform = resampler(waveform)
|
| 134 |
+
|
| 135 |
+
mel_spectrogram = torchaudio.transforms.MelSpectrogram(
|
| 136 |
+
sample_rate=16000,
|
| 137 |
+
n_mels=64,
|
| 138 |
+
n_fft=1024,
|
| 139 |
+
hop_length=512
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
mel_spec = mel_spectrogram(waveform)
|
| 143 |
+
|
| 144 |
+
# Normalize
|
| 145 |
+
mel_spec = (mel_spec - mel_spec.mean()) / mel_spec.std()
|
| 146 |
+
|
| 147 |
+
if mel_spec.size(2) < 300:
|
| 148 |
+
padding = 300 - mel_spec.size(2)
|
| 149 |
+
mel_spec = torch.nn.functional.pad(mel_spec, (0, padding))
|
| 150 |
+
else:
|
| 151 |
+
mel_spec = mel_spec[:, :, :300]
|
| 152 |
+
|
| 153 |
+
return mel_spec
|
| 154 |
+
|
| 155 |
+
except subprocess.CalledProcessError as e:
|
| 156 |
+
raise ValueError(f"Audio extraction error: {str(e)}")
|
| 157 |
+
except Exception as e:
|
| 158 |
+
raise ValueError(f"Audio error: {str(e)}")
|
| 159 |
+
finally:
|
| 160 |
+
if os.path.exists(audio_path):
|
| 161 |
+
os.remove(audio_path)
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
class VideoUtteranceProcessor:
|
| 165 |
+
def __init__(self):
|
| 166 |
+
self.video_processor = VideoProcessor()
|
| 167 |
+
self.audio_processor = AudioProcessor()
|
| 168 |
+
|
| 169 |
+
def extract_segment(self, video_path, start_time, end_time, temp_dir="/tmp"):
|
| 170 |
+
os.makedirs(temp_dir, exist_ok=True)
|
| 171 |
+
segment_path = os.path.join(
|
| 172 |
+
temp_dir, f"segment_{start_time}_{end_time}.mp4")
|
| 173 |
+
|
| 174 |
+
subprocess.run([
|
| 175 |
+
"ffmpeg", "-i", video_path,
|
| 176 |
+
"-ss", str(start_time),
|
| 177 |
+
"-to", str(end_time),
|
| 178 |
+
"-c:v", "libx264",
|
| 179 |
+
"-c:a", "aac",
|
| 180 |
+
"-y",
|
| 181 |
+
segment_path
|
| 182 |
+
], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 183 |
+
|
| 184 |
+
if not os.path.exists(segment_path) or os.path.getsize(segment_path) == 0:
|
| 185 |
+
raise ValueError("Segment extraction failed: " + segment_path)
|
| 186 |
+
|
| 187 |
+
return segment_path
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def model_fn(model_dir):
|
| 191 |
+
# Load the model for inference
|
| 192 |
+
if not install_ffmpeg():
|
| 193 |
+
raise RuntimeError(
|
| 194 |
+
"FFmpeg installation failed - required for inference")
|
| 195 |
+
|
| 196 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 197 |
+
model = MultimodalSentimentModel().to(device)
|
| 198 |
+
|
| 199 |
+
model_path = os.path.join(model_dir, 'model.pth')
|
| 200 |
+
if not os.path.exists(model_path):
|
| 201 |
+
model_path = os.path.join(model_dir, "saved_models", 'checkpoint.pth')
|
| 202 |
+
if not os.path.exists(model_path):
|
| 203 |
+
raise FileNotFoundError(
|
| 204 |
+
"Model file not found in path " + model_path)
|
| 205 |
+
|
| 206 |
+
print("Loading model from path: " + model_path)
|
| 207 |
+
model.load_state_dict(torch.load(
|
| 208 |
+
model_path, map_location=device, weights_only=True))
|
| 209 |
+
model.eval()
|
| 210 |
+
|
| 211 |
+
return {
|
| 212 |
+
'model': model,
|
| 213 |
+
'tokenizer': AutoTokenizer.from_pretrained('bert-base-uncased'),
|
| 214 |
+
'transcriber': whisper.load_model(
|
| 215 |
+
"base",
|
| 216 |
+
device="cpu" if device.type == "cpu" else device,
|
| 217 |
+
),
|
| 218 |
+
'device': device
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def predict_fn(input_data, model_dict):
|
| 223 |
+
model = model_dict['model']
|
| 224 |
+
tokenizer = model_dict['tokenizer']
|
| 225 |
+
device = model_dict['device']
|
| 226 |
+
video_path = input_data['video_path']
|
| 227 |
+
|
| 228 |
+
result = model_dict['transcriber'].transcribe(
|
| 229 |
+
video_path, word_timestamps=True)
|
| 230 |
+
|
| 231 |
+
utterance_processor = VideoUtteranceProcessor()
|
| 232 |
+
predictions = []
|
| 233 |
+
|
| 234 |
+
for segment in result["segments"]:
|
| 235 |
+
try:
|
| 236 |
+
segment_path = utterance_processor.extract_segment(
|
| 237 |
+
video_path,
|
| 238 |
+
segment["start"],
|
| 239 |
+
segment["end"]
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
video_frames = utterance_processor.video_processor.process_video(
|
| 243 |
+
segment_path)
|
| 244 |
+
audio_features = utterance_processor.audio_processor.extract_features(
|
| 245 |
+
segment_path)
|
| 246 |
+
text_inputs = tokenizer(
|
| 247 |
+
segment["text"],
|
| 248 |
+
padding="max_length",
|
| 249 |
+
truncation=True,
|
| 250 |
+
max_length=128,
|
| 251 |
+
return_tensors="pt"
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
# Move to device
|
| 255 |
+
text_inputs = {k: v.to(device) for k, v in text_inputs.items()}
|
| 256 |
+
video_frames = video_frames.unsqueeze(0).to(device)
|
| 257 |
+
audio_features = audio_features.unsqueeze(0).to(device)
|
| 258 |
+
|
| 259 |
+
# Get predictions
|
| 260 |
+
with torch.inference_mode():
|
| 261 |
+
outputs = model(text_inputs, video_frames, audio_features)
|
| 262 |
+
emotion_probs = torch.softmax(outputs["emotions"], dim=1)[0]
|
| 263 |
+
sentiment_probs = torch.softmax(
|
| 264 |
+
outputs["sentiments"], dim=1)[0]
|
| 265 |
+
|
| 266 |
+
emotion_values, emotion_indices = torch.topk(emotion_probs, 3)
|
| 267 |
+
sentiment_values, sentiment_indices = torch.topk(
|
| 268 |
+
sentiment_probs, 3)
|
| 269 |
+
|
| 270 |
+
predictions.append({
|
| 271 |
+
"start_time": segment["start"],
|
| 272 |
+
"end_time": segment["end"],
|
| 273 |
+
"text": segment["text"],
|
| 274 |
+
"emotions": [
|
| 275 |
+
{"label": EMOTION_MAP[idx.item()], "confidence": conf.item()} for idx, conf in zip(emotion_indices, emotion_values)
|
| 276 |
+
],
|
| 277 |
+
"sentiments": [
|
| 278 |
+
{"label": SENTIMENT_MAP[idx.item()], "confidence": conf.item()} for idx, conf in zip(sentiment_indices, sentiment_values)
|
| 279 |
+
]
|
| 280 |
+
})
|
| 281 |
+
|
| 282 |
+
except Exception as e:
|
| 283 |
+
print("Segment failed inference: " + str(e))
|
| 284 |
+
|
| 285 |
+
finally:
|
| 286 |
+
# Cleanup
|
| 287 |
+
if os.path.exists(segment_path):
|
| 288 |
+
os.remove(segment_path)
|
| 289 |
+
return {"utterances": predictions}
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
def process_local_video(video_path, model_dir="."):
|
| 293 |
+
model_dict = model_fn(model_dir)
|
| 294 |
+
|
| 295 |
+
input_data = {'video_path': video_path}
|
| 296 |
+
|
| 297 |
+
predictions = predict_fn(input_data, model_dict)
|
| 298 |
+
|
| 299 |
+
for utterance in predictions["utterances"]:
|
| 300 |
+
print("\nUtterance:")
|
| 301 |
+
print(f"""Start: {utterance['start_time']}s, End: {
|
| 302 |
+
utterance['end_time']}s""")
|
| 303 |
+
print(f"Text: {utterance['text']}")
|
| 304 |
+
print("\n Top Emotions:")
|
| 305 |
+
for emotion in utterance['emotions']:
|
| 306 |
+
print(f"{emotion['label']}: {emotion['confidence']:.2f}")
|
| 307 |
+
print("\n Top Sentiments:")
|
| 308 |
+
for sentiment in utterance['sentiments']:
|
| 309 |
+
print(f"{sentiment['label']}: {sentiment['confidence']:.2f}")
|
| 310 |
+
print("-"*50)
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
if __name__ == "__main__":
|
| 314 |
+
process_local_video("./dia2_utt3.mp4")
|
models.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import BertModel
|
| 4 |
+
from torchvision import models as vision_models
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class TextEncoder(nn.Module):
|
| 8 |
+
def __init__(self):
|
| 9 |
+
super().__init__()
|
| 10 |
+
self.bert = BertModel.from_pretrained('bert-base-uncased')
|
| 11 |
+
|
| 12 |
+
for param in self.bert.parameters():
|
| 13 |
+
param.requires_grad = False
|
| 14 |
+
|
| 15 |
+
self.projection = nn.Linear(768, 128)
|
| 16 |
+
|
| 17 |
+
def forward(self, input_ids, attention_mask):
|
| 18 |
+
# Extract BERT embeddings
|
| 19 |
+
outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
|
| 20 |
+
|
| 21 |
+
# Use [CLS] token representation
|
| 22 |
+
pooler_output = outputs.pooler_output
|
| 23 |
+
|
| 24 |
+
return self.projection(pooler_output)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class VideoEncoder(nn.Module):
|
| 28 |
+
def __init__(self):
|
| 29 |
+
super().__init__()
|
| 30 |
+
self.backbone = vision_models.video.r3d_18(pretrained=True)
|
| 31 |
+
|
| 32 |
+
for param in self.backbone.parameters():
|
| 33 |
+
param.requires_grad = False
|
| 34 |
+
|
| 35 |
+
num_fts = self.backbone.fc.in_features
|
| 36 |
+
self.backbone.fc = nn.Sequential(
|
| 37 |
+
nn.Linear(num_fts, 128),
|
| 38 |
+
nn.ReLU(),
|
| 39 |
+
nn.Dropout(0.2)
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
def forward(self, x):
|
| 43 |
+
# [batch_size, frames, channels, height, width]->[batch_size, channels, frames, height, width]
|
| 44 |
+
x = x.transpose(1, 2)
|
| 45 |
+
return self.backbone(x)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class AudioEncoder(nn.Module):
|
| 49 |
+
def __init__(self):
|
| 50 |
+
super().__init__()
|
| 51 |
+
self.conv_layers = nn.Sequential(
|
| 52 |
+
# Lower level features
|
| 53 |
+
nn.Conv1d(64, 64, kernel_size=3),
|
| 54 |
+
nn.BatchNorm1d(64),
|
| 55 |
+
nn.ReLU(),
|
| 56 |
+
nn.MaxPool1d(2),
|
| 57 |
+
# Higher level features
|
| 58 |
+
nn.Conv1d(64, 128, kernel_size=3),
|
| 59 |
+
nn.BatchNorm1d(128),
|
| 60 |
+
nn.ReLU(),
|
| 61 |
+
nn.AdaptiveAvgPool1d(1)
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
for param in self.conv_layers.parameters():
|
| 65 |
+
param.requires_grad = False
|
| 66 |
+
|
| 67 |
+
self.projection = nn.Sequential(
|
| 68 |
+
nn.Linear(128, 128),
|
| 69 |
+
nn.ReLU(),
|
| 70 |
+
nn.Dropout(0.2)
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
def forward(self, x):
|
| 74 |
+
x = x.squeeze(1)
|
| 75 |
+
|
| 76 |
+
features = self.conv_layers(x)
|
| 77 |
+
# Features output: [batch_size, 128, 1]
|
| 78 |
+
|
| 79 |
+
return self.projection(features.squeeze(-1))
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
class MultimodalSentimentModel(nn.Module):
|
| 83 |
+
def __init__(self):
|
| 84 |
+
super().__init__()
|
| 85 |
+
|
| 86 |
+
# Encoders
|
| 87 |
+
self.text_encoder = TextEncoder()
|
| 88 |
+
self.video_encoder = VideoEncoder()
|
| 89 |
+
self.audio_encoder = AudioEncoder()
|
| 90 |
+
|
| 91 |
+
# Fusion layer
|
| 92 |
+
self.fusion_layer = nn.Sequential(
|
| 93 |
+
nn.Linear(128 * 3, 256),
|
| 94 |
+
nn.BatchNorm1d(256),
|
| 95 |
+
nn.ReLU(),
|
| 96 |
+
nn.Dropout(0.3)
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Classification heads
|
| 100 |
+
self.emotion_classifier = nn.Sequential(
|
| 101 |
+
nn.Linear(256, 64),
|
| 102 |
+
nn.ReLU(),
|
| 103 |
+
nn.Dropout(0.2),
|
| 104 |
+
nn.Linear(64, 7) # Sadness, anger
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
self.sentiment_classifier = nn.Sequential(
|
| 108 |
+
nn.Linear(256, 64),
|
| 109 |
+
nn.ReLU(),
|
| 110 |
+
nn.Dropout(0.2),
|
| 111 |
+
nn.Linear(64, 3) # Negative, positive, neutral
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
def forward(self, text_inputs, video_frames, audio_features):
|
| 115 |
+
text_features = self.text_encoder(
|
| 116 |
+
text_inputs['input_ids'],
|
| 117 |
+
text_inputs['attention_mask'],
|
| 118 |
+
)
|
| 119 |
+
video_features = self.video_encoder(video_frames)
|
| 120 |
+
audio_features = self.audio_encoder(audio_features)
|
| 121 |
+
|
| 122 |
+
# Concatenate multimodal features
|
| 123 |
+
combined_features = torch.cat([
|
| 124 |
+
text_features,
|
| 125 |
+
video_features,
|
| 126 |
+
audio_features
|
| 127 |
+
], dim=1) # [batch_size, 128 * 3]
|
| 128 |
+
|
| 129 |
+
fused_features = self.fusion_layer(combined_features)
|
| 130 |
+
|
| 131 |
+
emotion_output = self.emotion_classifier(fused_features)
|
| 132 |
+
sentiment_output = self.sentiment_classifier(fused_features)
|
| 133 |
+
|
| 134 |
+
return {
|
| 135 |
+
'emotions': emotion_output,
|
| 136 |
+
'sentiments': sentiment_output
|
| 137 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchaudio
|
| 3 |
+
torchvision
|
| 4 |
+
transformers
|
| 5 |
+
whisper
|
| 6 |
+
opencv-python
|
| 7 |
+
numpy
|
| 8 |
+
soundfile
|
| 9 |
+
ffmpeg-python
|
| 10 |
+
fastapi
|
| 11 |
+
uvicorn
|
| 12 |
+
python-multipart
|
| 13 |
+
gradio
|
saved_models/checkpoint.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e3f4c798f9853443a7acb6280033dc573c23861116cc00f5d0beccdc0e5caa8a
|
| 3 |
+
size 572162402
|