Update app.py
Browse files
app.py
CHANGED
@@ -60,32 +60,37 @@ def process_audio(input_data, segment_duration=10):
|
|
60 |
audio = audio[0]
|
61 |
segments = segment_audio(audio, sr, segment_duration)
|
62 |
segment_predictions = []
|
63 |
-
|
64 |
-
eer_threshold = thresh - 5e-3
|
|
|
65 |
for idx, segment in enumerate(segments):
|
66 |
features = FEATURE_EXTRACTOR(segment, sr)
|
67 |
-
features_avg = torch.mean(features, dim=1).cpu().numpy()
|
68 |
-
features_avg = features_avg.reshape(1, -1)
|
69 |
decision_score = classifier.decision_function(features_avg)
|
70 |
decision_score_scaled = scaler.transform(decision_score.reshape(-1, 1)).flatten()
|
71 |
decision_value = decision_score_scaled[0]
|
72 |
pred = 1 if decision_value >= eer_threshold else 0
|
|
|
73 |
if pred == 1:
|
74 |
confidence_percentage = expit(decision_score).item()
|
75 |
else:
|
76 |
confidence_percentage = 1 - expit(decision_score).item()
|
|
|
77 |
segment_predictions.append(pred)
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
89 |
json_output = json.dumps(output_dict, indent=4)
|
90 |
print(json_output)
|
91 |
return json_output
|
|
|
60 |
audio = audio[0]
|
61 |
segments = segment_audio(audio, sr, segment_duration)
|
62 |
segment_predictions = []
|
63 |
+
confidence_scores = []
|
64 |
+
eer_threshold = thresh - 5e-3 # small margin of error
|
65 |
+
|
66 |
for idx, segment in enumerate(segments):
|
67 |
features = FEATURE_EXTRACTOR(segment, sr)
|
68 |
+
features_avg = torch.mean(features, dim=1).cpu().numpy().reshape(1, -1)
|
|
|
69 |
decision_score = classifier.decision_function(features_avg)
|
70 |
decision_score_scaled = scaler.transform(decision_score.reshape(-1, 1)).flatten()
|
71 |
decision_value = decision_score_scaled[0]
|
72 |
pred = 1 if decision_value >= eer_threshold else 0
|
73 |
+
|
74 |
if pred == 1:
|
75 |
confidence_percentage = expit(decision_score).item()
|
76 |
else:
|
77 |
confidence_percentage = 1 - expit(decision_score).item()
|
78 |
+
|
79 |
segment_predictions.append(pred)
|
80 |
+
confidence_scores.append(confidence_percentage)
|
81 |
+
|
82 |
+
output_dict = {
|
83 |
+
"label": "real" if sum(segment_predictions) > (len(segment_predictions) / 2) else "fake",
|
84 |
+
"segments": [
|
85 |
+
{
|
86 |
+
"segment": idx + 1,
|
87 |
+
"prediction": "real" if pred == 1 else "fake",
|
88 |
+
"confidence": round(conf * 100, 2)
|
89 |
+
}
|
90 |
+
for idx, (pred, conf) in enumerate(zip(segment_predictions, confidence_scores))
|
91 |
+
]
|
92 |
+
}
|
93 |
+
|
94 |
json_output = json.dumps(output_dict, indent=4)
|
95 |
print(json_output)
|
96 |
return json_output
|