Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,43 +6,66 @@ from transformers import pipeline
|
|
6 |
# Create a Flask app
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
-
# Initialize
|
10 |
audio_model = None
|
11 |
|
12 |
def download_models():
|
13 |
global audio_model
|
14 |
-
print("Downloading
|
15 |
# Download and load the audio model
|
16 |
audio_model = pipeline("audio-classification", model="MelodyMachine/Deepfake-audio-detection-V2")
|
17 |
-
print("
|
18 |
|
19 |
-
# Download
|
20 |
download_models()
|
21 |
|
22 |
@app.route('/detect', methods=['POST'])
|
23 |
def detect_deepfake():
|
|
|
|
|
24 |
folder_path = request.form.get('folder_path')
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
for file_name in os.listdir(folder_path):
|
33 |
-
if file_name.endswith('.wav') or file_name.endswith('.mp3'):
|
34 |
-
file_path = os.path.join(folder_path, file_name)
|
35 |
-
result = audio_model(file_path)
|
36 |
-
results[file_name] = {item['label']: item['score'] for item in result}
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
f.write(json.dumps(results))
|
41 |
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
if __name__ == '__main__':
|
48 |
# Run the Flask app
|
|
|
6 |
# Create a Flask app
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
+
# Initialize models at the start of the API
|
10 |
audio_model = None
|
11 |
|
12 |
def download_models():
|
13 |
global audio_model
|
14 |
+
print("Downloading models...")
|
15 |
# Download and load the audio model
|
16 |
audio_model = pipeline("audio-classification", model="MelodyMachine/Deepfake-audio-detection-V2")
|
17 |
+
print("Model downloaded and ready to use.")
|
18 |
|
19 |
+
# Download model when the server starts
|
20 |
download_models()
|
21 |
|
22 |
@app.route('/detect', methods=['POST'])
|
23 |
def detect_deepfake():
|
24 |
+
data_type = request.form.get('type') # "audio"
|
25 |
+
audio_file = request.files.get('audio_file')
|
26 |
folder_path = request.form.get('folder_path')
|
27 |
|
28 |
+
# If a single audio file is provided
|
29 |
+
if audio_file:
|
30 |
+
try:
|
31 |
+
# Save the uploaded file temporarily
|
32 |
+
file_path = os.path.join("/tmp", audio_file.filename)
|
33 |
+
audio_file.save(file_path)
|
34 |
|
35 |
+
# Perform detection
|
36 |
+
result = audio_model(file_path)
|
37 |
+
result_dict = {item['label']: item['score'] for item in result}
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
+
# Remove the temporary file
|
40 |
+
os.remove(file_path)
|
|
|
41 |
|
42 |
+
return jsonify({"message": "Detection completed", "results": result_dict}), 200
|
43 |
|
44 |
+
except Exception as e:
|
45 |
+
return jsonify({"error": str(e)}), 500
|
46 |
+
|
47 |
+
# If a folder path is provided
|
48 |
+
elif folder_path and os.path.isdir(folder_path):
|
49 |
+
results = {}
|
50 |
+
try:
|
51 |
+
for file_name in os.listdir(folder_path):
|
52 |
+
if file_name.endswith('.wav') or file_name.endswith('.mp3'):
|
53 |
+
file_path = os.path.join(folder_path, file_name)
|
54 |
+
result = audio_model(file_path)
|
55 |
+
results[file_name] = {item['label']: item['score'] for item in result}
|
56 |
+
|
57 |
+
# Save results to a file
|
58 |
+
with open('detection_results.json', 'w') as f:
|
59 |
+
f.write(json.dumps(results))
|
60 |
+
|
61 |
+
return jsonify({"message": "Detection completed", "results": results}), 200
|
62 |
+
|
63 |
+
except Exception as e:
|
64 |
+
return jsonify({"error": str(e)}), 500
|
65 |
+
|
66 |
+
# Invalid request if neither audio file nor valid folder path is provided
|
67 |
+
else:
|
68 |
+
return jsonify({"error": "Invalid input. Provide an audio file or a valid folder path."}), 400
|
69 |
|
70 |
if __name__ == '__main__':
|
71 |
# Run the Flask app
|