Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ import json
|
|
3 |
from flask import Flask, jsonify, request
|
4 |
from transformers import pipeline
|
5 |
from pydub import AudioSegment
|
|
|
6 |
|
7 |
# Create a Flask app
|
8 |
app = Flask(__name__)
|
@@ -13,17 +14,32 @@ audio_model = None
|
|
13 |
def download_models():
|
14 |
global audio_model
|
15 |
print("Downloading models...")
|
16 |
-
# Download and load the audio model
|
17 |
-
audio_model = pipeline("audio-classification", model="MelodyMachine/Deepfake-audio-detection-V2"
|
18 |
print("Model downloaded and ready to use.")
|
19 |
|
20 |
# Download model when the server starts
|
21 |
download_models()
|
22 |
|
23 |
-
def
|
24 |
-
#
|
25 |
-
audio = AudioSegment.from_file(
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
@app.route('/detect', methods=['POST'])
|
29 |
def detect_deepfake():
|
@@ -33,22 +49,18 @@ def detect_deepfake():
|
|
33 |
# If a single audio file is provided
|
34 |
if audio_file:
|
35 |
try:
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
|
44 |
# Perform detection
|
45 |
-
result = audio_model(
|
46 |
result_dict = {item['label']: item['score'] for item in result}
|
47 |
|
48 |
-
# Remove the temporary files
|
49 |
-
os.remove(input_path)
|
50 |
-
os.remove(output_path)
|
51 |
-
|
52 |
return jsonify({"message": "Detection completed", "results": result_dict}), 200
|
53 |
|
54 |
except Exception as e:
|
|
|
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
|
9 |
app = Flask(__name__)
|
|
|
14 |
def download_models():
|
15 |
global audio_model
|
16 |
print("Downloading models...")
|
17 |
+
# Download and load the audio model
|
18 |
+
audio_model = pipeline("audio-classification", model="MelodyMachine/Deepfake-audio-detection-V2")
|
19 |
print("Model downloaded and ready to use.")
|
20 |
|
21 |
# Download model when the server starts
|
22 |
download_models()
|
23 |
|
24 |
+
def preprocess_audio(file):
|
25 |
+
# Load audio file
|
26 |
+
audio = AudioSegment.from_file(file)
|
27 |
+
|
28 |
+
# Convert audio to mono and normalize volume
|
29 |
+
audio = audio.set_channels(1).set_frame_rate(16000)
|
30 |
+
|
31 |
+
# Ensure audio is of a standard length (e.g., 10 seconds)
|
32 |
+
duration_ms = len(audio)
|
33 |
+
target_duration_ms = 10000 # Target duration in milliseconds (10 seconds)
|
34 |
+
if duration_ms < target_duration_ms:
|
35 |
+
# Pad with silence if shorter than target duration
|
36 |
+
padding = AudioSegment.silent(duration=target_duration_ms - duration_ms)
|
37 |
+
audio = audio + padding
|
38 |
+
elif duration_ms > target_duration_ms:
|
39 |
+
# Truncate if longer than target duration
|
40 |
+
audio = audio[:target_duration_ms]
|
41 |
+
|
42 |
+
return audio
|
43 |
|
44 |
@app.route('/detect', methods=['POST'])
|
45 |
def detect_deepfake():
|
|
|
49 |
# If a single audio file is provided
|
50 |
if audio_file:
|
51 |
try:
|
52 |
+
# Preprocess the audio file
|
53 |
+
audio = preprocess_audio(audio_file)
|
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(temp_wav)
|
62 |
result_dict = {item['label']: item['score'] for item in result}
|
63 |
|
|
|
|
|
|
|
|
|
64 |
return jsonify({"message": "Detection completed", "results": result_dict}), 200
|
65 |
|
66 |
except Exception as e:
|