GVAmaresh
commited on
Commit
·
582d273
1
Parent(s):
4a2f439
dev check working
Browse files
app.py
CHANGED
@@ -296,3 +296,95 @@ def reencode_audio(input_path, output_path):
|
|
296 |
]
|
297 |
subprocess.run(command, check=True)
|
298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
]
|
297 |
subprocess.run(command, check=True)
|
298 |
|
299 |
+
#--------------------------------------------------------------------------------------------------------------------
|
300 |
+
from collections import Counter
|
301 |
+
from datetime import datetime
|
302 |
+
|
303 |
+
@app.post("/upload")
|
304 |
+
async def upload_file(file: UploadFile = File(...)):
|
305 |
+
print(f"Received file: {file.filename}")
|
306 |
+
|
307 |
+
original_filename = file.filename.rsplit('.', 1)[0]
|
308 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
309 |
+
wav_filename = os.path.join(SAVE_DIR, f"{timestamp}.wav")
|
310 |
+
reencoded_filename = os.path.join(SAVE_DIR, f"{timestamp}_reencoded.wav")
|
311 |
+
|
312 |
+
# os.makedirs(SAVE_DIR, exist_ok=True)
|
313 |
+
with open(wav_filename, "wb") as buffer:
|
314 |
+
shutil.copyfileobj(file.file, buffer)
|
315 |
+
|
316 |
+
reencode_audio(wav_filename, reencoded_filename)
|
317 |
+
os.remove(wav_filename)
|
318 |
+
print(f"File successfully re-encoded as: {reencoded_filename}")
|
319 |
+
|
320 |
+
try:
|
321 |
+
audio, sr = librosa.load(reencoded_filename, sr=None)
|
322 |
+
print("Loaded successfully with librosa")
|
323 |
+
except Exception as e:
|
324 |
+
print(f"Error loading re-encoded file: {e}")
|
325 |
+
new_features = extract_features(reencoded_filename)
|
326 |
+
prediction, entropy = classify_audio(new_features)
|
327 |
+
with open(reencoded_filename, "rb") as audio_file:
|
328 |
+
audio_data = audio_file.read()
|
329 |
+
|
330 |
+
# audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
331 |
+
os.remove(reencoded_filename)
|
332 |
+
return JSONResponse(content={
|
333 |
+
"prediction": bool(prediction),
|
334 |
+
"entropy": float(entropy),
|
335 |
+
})
|
336 |
+
|
337 |
+
|
338 |
+
@app.post("/upload_audio")
|
339 |
+
async def upload_file(file: UploadFile = File(...)):
|
340 |
+
print(f"Received file: {file.filename}")
|
341 |
+
|
342 |
+
original_filename = file.filename.rsplit('.', 1)[0]
|
343 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
344 |
+
wav_filename = os.path.join(SAVE_DIR, f"{timestamp}.wav")
|
345 |
+
reencoded_filename = os.path.join(SAVE_DIR, f"{timestamp}_reencoded.wav")
|
346 |
+
|
347 |
+
# os.makedirs(SAVE_DIR, exist_ok=True)
|
348 |
+
with open(wav_filename, "wb") as buffer:
|
349 |
+
shutil.copyfileobj(file.file, buffer)
|
350 |
+
|
351 |
+
reencode_audio(wav_filename, reencoded_filename)
|
352 |
+
|
353 |
+
os.remove(wav_filename)
|
354 |
+
print(f"File successfully re-encoded as: {reencoded_filename}")
|
355 |
+
|
356 |
+
try:
|
357 |
+
audio, sr = librosa.load(reencoded_filename, sr=None)
|
358 |
+
print("Loaded successfully with librosa")
|
359 |
+
except Exception as e:
|
360 |
+
print(f"Error loading re-encoded file: {e}")
|
361 |
+
new_features = extract_features(reencoded_filename)
|
362 |
+
detector = UnifiedDeepfakeDetector()
|
363 |
+
print(reencoded_filename)
|
364 |
+
result = detector.analyze_audio_rf(reencoded_filename, model_choice="all")
|
365 |
+
prediction, entropy = classify_audio(new_features)
|
366 |
+
with open(reencoded_filename, "rb") as audio_file:
|
367 |
+
audio_data = audio_file.read()
|
368 |
+
result = list(result)
|
369 |
+
result.append("FAKE" if float(entropy) < 150 else "REAL")
|
370 |
+
print(result)
|
371 |
+
r_normalized = [x.upper() for x in result]
|
372 |
+
counter = Counter(r_normalized)
|
373 |
+
|
374 |
+
most_common_element, _ = counter.most_common(1)[0]
|
375 |
+
|
376 |
+
print(f"The most frequent element is: {most_common_element}")
|
377 |
+
|
378 |
+
|
379 |
+
audio_base64 = base64.b64encode(audio_data).decode('utf-8')
|
380 |
+
print(f"Audio Data Length: {len(audio_data)}")
|
381 |
+
|
382 |
+
os.remove(reencoded_filename)
|
383 |
+
return JSONResponse(content={
|
384 |
+
"filename": file.filename,
|
385 |
+
"prediction": most_common_element.upper(),
|
386 |
+
"entropy": float(entropy),
|
387 |
+
"audio": audio_base64,
|
388 |
+
"content_type": "audio/wav"
|
389 |
+
})
|
390 |
+
|