ParthBarot commited on
Commit
7ec95bc
·
verified ·
1 Parent(s): 5a18439

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -2,6 +2,7 @@ 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__)
@@ -19,6 +20,11 @@ def download_models():
19
  # Download model when the server starts
20
  download_models()
21
 
 
 
 
 
 
22
  @app.route('/detect', methods=['POST'])
23
  def detect_deepfake():
24
  # Expect an audio file in the request
@@ -28,15 +34,20 @@ def detect_deepfake():
28
  if audio_file:
29
  try:
30
  # Save the uploaded file temporarily
31
- file_path = os.path.join("/tmp", audio_file.filename)
32
- audio_file.save(file_path)
 
 
 
 
33
 
34
  # Perform detection
35
- result = audio_model(file_path)
36
  result_dict = {item['label']: item['score'] for item in result}
37
 
38
- # Remove the temporary file
39
- os.remove(file_path)
 
40
 
41
  return jsonify({"message": "Detection completed", "results": result_dict}), 200
42
 
 
2
  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__)
 
20
  # Download model when the server starts
21
  download_models()
22
 
23
+ def convert_audio_to_wav(input_path, output_path):
24
+ # Convert any audio format to WAV using pydub
25
+ audio = AudioSegment.from_file(input_path)
26
+ audio.export(output_path, format="wav")
27
+
28
  @app.route('/detect', methods=['POST'])
29
  def detect_deepfake():
30
  # Expect an audio file in the request
 
34
  if audio_file:
35
  try:
36
  # Save the uploaded file temporarily
37
+ input_path = os.path.join("/tmp", audio_file.filename)
38
+ audio_file.save(input_path)
39
+
40
+ # Convert the file to WAV format
41
+ output_path = os.path.splitext(input_path)[0] + '.wav'
42
+ convert_audio_to_wav(input_path, output_path)
43
 
44
  # Perform detection
45
+ result = audio_model(output_path)
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