Update app.py
Browse files
app.py
CHANGED
|
@@ -15,6 +15,14 @@ model.eval()
|
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
model = model.to(device)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@GPU
|
| 19 |
def analyze_video(video):
|
| 20 |
cap = cv2.VideoCapture(video)
|
|
@@ -23,7 +31,6 @@ def analyze_video(video):
|
|
| 23 |
if total_frames == 0:
|
| 24 |
return "Could not read video frames."
|
| 25 |
|
| 26 |
-
# Evenly sample 10 frames
|
| 27 |
num_frames_to_process = min(200, total_frames)
|
| 28 |
frame_indices = [int(i * total_frames / num_frames_to_process) for i in range(num_frames_to_process)]
|
| 29 |
|
|
@@ -59,24 +66,13 @@ def analyze_video(video):
|
|
| 59 |
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 60 |
predictions = torch.argmax(probs, dim=1).tolist()
|
| 61 |
|
| 62 |
-
# Identify index for AI/fake class
|
| 63 |
-
ai_class_index = None
|
| 64 |
-
for k, v in model.config.id2label.items():
|
| 65 |
-
if v.strip().lower() in ["ai", "fake", "ai_generated"]:
|
| 66 |
-
ai_class_index = k
|
| 67 |
-
break
|
| 68 |
-
|
| 69 |
-
if ai_class_index is None:
|
| 70 |
-
return "Error: Could not identify the AI-generated class label from model config."
|
| 71 |
-
|
| 72 |
ai_scores = probs[:, ai_class_index].tolist()
|
| 73 |
|
| 74 |
avg_score = sum(ai_scores) / len(ai_scores)
|
| 75 |
total_frames = len(ai_scores)
|
| 76 |
-
ai_frame_count = sum(1 for score in ai_scores if score >= 0.5)
|
| 77 |
ai_frame_ratio = ai_frame_count / total_frames
|
| 78 |
-
|
| 79 |
-
# Refined verdict based on AI frame ratio and confidence
|
| 80 |
if ai_frame_ratio < 0.25:
|
| 81 |
verdict = "β
Very likely real β no strong signs of AI generation."
|
| 82 |
elif 0.25 <= ai_frame_ratio < 0.5:
|
|
@@ -88,10 +84,10 @@ def analyze_video(video):
|
|
| 88 |
verdict = "β οΈ Likely AI-generated β multiple signs suggest manipulation."
|
| 89 |
else:
|
| 90 |
verdict = "π€ Unclear β the video shows mixed signals."
|
| 91 |
-
|
| 92 |
results = []
|
| 93 |
for frame_idx, pred, ai_score in zip(frame_numbers, predictions, ai_scores):
|
| 94 |
-
label =
|
| 95 |
confidence = ai_score * 100
|
| 96 |
results.append(f"Frame {frame_idx}: {label} (AI Confidence: {confidence:.1f}%)")
|
| 97 |
|
|
|
|
| 15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
model = model.to(device)
|
| 17 |
|
| 18 |
+
# Map index to label
|
| 19 |
+
id2label = model.config.id2label
|
| 20 |
+
label2id = {v: k for k, v in id2label.items()}
|
| 21 |
+
|
| 22 |
+
# Just pick highest index as "AI" for now (based on SMOGY default config)
|
| 23 |
+
ai_class_index = max(id2label.keys())
|
| 24 |
+
ai_label_name = id2label[ai_class_index]
|
| 25 |
+
|
| 26 |
@GPU
|
| 27 |
def analyze_video(video):
|
| 28 |
cap = cv2.VideoCapture(video)
|
|
|
|
| 31 |
if total_frames == 0:
|
| 32 |
return "Could not read video frames."
|
| 33 |
|
|
|
|
| 34 |
num_frames_to_process = min(200, total_frames)
|
| 35 |
frame_indices = [int(i * total_frames / num_frames_to_process) for i in range(num_frames_to_process)]
|
| 36 |
|
|
|
|
| 66 |
probs = torch.nn.functional.softmax(outputs.logits, dim=1)
|
| 67 |
predictions = torch.argmax(probs, dim=1).tolist()
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
ai_scores = probs[:, ai_class_index].tolist()
|
| 70 |
|
| 71 |
avg_score = sum(ai_scores) / len(ai_scores)
|
| 72 |
total_frames = len(ai_scores)
|
| 73 |
+
ai_frame_count = sum(1 for score in ai_scores if score >= 0.5)
|
| 74 |
ai_frame_ratio = ai_frame_count / total_frames
|
| 75 |
+
|
|
|
|
| 76 |
if ai_frame_ratio < 0.25:
|
| 77 |
verdict = "β
Very likely real β no strong signs of AI generation."
|
| 78 |
elif 0.25 <= ai_frame_ratio < 0.5:
|
|
|
|
| 84 |
verdict = "β οΈ Likely AI-generated β multiple signs suggest manipulation."
|
| 85 |
else:
|
| 86 |
verdict = "π€ Unclear β the video shows mixed signals."
|
| 87 |
+
|
| 88 |
results = []
|
| 89 |
for frame_idx, pred, ai_score in zip(frame_numbers, predictions, ai_scores):
|
| 90 |
+
label = id2label[pred]
|
| 91 |
confidence = ai_score * 100
|
| 92 |
results.append(f"Frame {frame_idx}: {label} (AI Confidence: {confidence:.1f}%)")
|
| 93 |
|