Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
from flask import Flask, jsonify, request
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Create a Flask app
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Initialize the audio model at the start of the API
|
10 |
+
audio_model = None
|
11 |
+
|
12 |
+
def download_models():
|
13 |
+
global audio_model
|
14 |
+
print("Downloading audio model...")
|
15 |
+
# Download and load the audio model
|
16 |
+
audio_model = pipeline("audio-classification", model="MelodyMachine/Deepfake-audio-detection-V2")
|
17 |
+
print("Audio model downloaded and ready to use.")
|
18 |
+
|
19 |
+
# Download the model when the server starts
|
20 |
+
download_models()
|
21 |
+
|
22 |
+
@app.route('/detect', methods=['POST'])
|
23 |
+
def detect_deepfake():
|
24 |
+
folder_path = request.form.get('folder_path')
|
25 |
+
|
26 |
+
if not folder_path or not os.path.isdir(folder_path):
|
27 |
+
return jsonify({"error": "Invalid folder path"}), 400
|
28 |
+
|
29 |
+
results = {}
|
30 |
+
try:
|
31 |
+
# Process audio files only
|
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 |
+
# Save results to a file
|
39 |
+
with open('detection_results.json', 'w') as f:
|
40 |
+
f.write(json.dumps(results))
|
41 |
+
|
42 |
+
return jsonify({"message": "Detection completed", "results": results}), 200
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
return jsonify({"error": str(e)}), 500
|
46 |
+
|
47 |
+
if __name__ == '__main__':
|
48 |
+
# Run the Flask app
|
49 |
+
app.run(host='0.0.0.0', port=5000)
|