Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
|
|
|
| 3 |
from flask import Flask, jsonify, request
|
| 4 |
from transformers import pipeline
|
| 5 |
from pydub import AudioSegment
|
|
|
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
# Create a Flask app
|
|
@@ -39,7 +41,14 @@ def preprocess_audio(file):
|
|
| 39 |
# Truncate if longer than target duration
|
| 40 |
audio = audio[:target_duration_ms]
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
@app.route('/detect', methods=['POST'])
|
| 45 |
def detect_deepfake():
|
|
@@ -50,15 +59,10 @@ def detect_deepfake():
|
|
| 50 |
if audio_file:
|
| 51 |
try:
|
| 52 |
# Preprocess the audio file
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# Save the processed file temporarily
|
| 56 |
-
temp_wav = BytesIO()
|
| 57 |
-
audio.export(temp_wav, format="wav")
|
| 58 |
-
temp_wav.seek(0)
|
| 59 |
|
| 60 |
# Perform detection
|
| 61 |
-
result = audio_model(
|
| 62 |
result_dict = {item['label']: item['score'] for item in result}
|
| 63 |
|
| 64 |
return jsonify({"message": "Detection completed", "results": result_dict}), 200
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
+
import numpy as np
|
| 4 |
from flask import Flask, jsonify, request
|
| 5 |
from transformers import pipeline
|
| 6 |
from pydub import AudioSegment
|
| 7 |
+
from scipy.io import wavfile
|
| 8 |
from io import BytesIO
|
| 9 |
|
| 10 |
# Create a Flask app
|
|
|
|
| 41 |
# Truncate if longer than target duration
|
| 42 |
audio = audio[:target_duration_ms]
|
| 43 |
|
| 44 |
+
# Convert audio to numpy array
|
| 45 |
+
audio_np = np.array(audio.get_array_of_samples())
|
| 46 |
+
|
| 47 |
+
# Normalize to [-1, 1] range if needed
|
| 48 |
+
audio_np = audio_np.astype(np.float32)
|
| 49 |
+
audio_np /= np.max(np.abs(audio_np))
|
| 50 |
+
|
| 51 |
+
return audio_np
|
| 52 |
|
| 53 |
@app.route('/detect', methods=['POST'])
|
| 54 |
def detect_deepfake():
|
|
|
|
| 59 |
if audio_file:
|
| 60 |
try:
|
| 61 |
# Preprocess the audio file
|
| 62 |
+
audio_np = preprocess_audio(audio_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# Perform detection
|
| 65 |
+
result = audio_model(audio_np)
|
| 66 |
result_dict = {item['label']: item['score'] for item in result}
|
| 67 |
|
| 68 |
return jsonify({"message": "Detection completed", "results": result_dict}), 200
|