File size: 1,514 Bytes
fb572c6
 
e12d370
20ff719
5677a1a
fb572c6
20ff719
55a8db0
f6bbe34
 
36a8c0a
f6bbe34
3d6d38e
 
fb572c6
3d6d38e
df31464
 
 
 
e228596
20ff719
df31464
20ff719
df31464
20ff719
 
e12d370
 
20ff719
 
 
 
 
3d6d38e
 
df31464
3d6d38e
 
b07cfd0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
from transformers import pipeline
import gradio as gr
import numpy as np
import soundfile as sf  # Để đọc âm thanh

# Kiểm tra xem token của Hugging Face có sẵn trong môi trường không
hf_token = os.getenv('HF_TOKEN')

if not hf_token:
    print("Warning: No Hugging Face token set. Continuing without token...")

# Tải mô hình phân loại deepfake âm thanh
pipe = pipeline("audio-classification", model="mo-thecreator/Deepfake-audio-detection")

def predict_deepfake(audio):
    # Nếu tệp âm thanh được tải lên, sử dụng soundfile để đọc
    if isinstance(audio, str):  # Đây là tệp âm thanh
        audio_data, _ = sf.read(audio)  # Đọc tệp âm thanh với soundfile
    elif isinstance(audio, np.ndarray):  # Nếu âm thanh đã là numpy array
        audio_data = audio
    else:
        return "Error: Invalid input format. Please upload a valid audio file."

    # Kiểm tra kích thước dữ liệu âm thanh
    if audio_data.shape[0] == 0:
        return "Error: Audio data is empty."

    # Phân loại âm thanh (real/fake)
    try:
        result = pipe(audio_data)
        return result[0]['label']  # Trả về nhãn 'real' hoặc 'fake'
    except Exception as e:
        return f"Error during classification: {str(e)}"

# Tạo giao diện người dùng với Gradio
iface = gr.Interface(fn=predict_deepfake, inputs=gr.Audio(type="filepath"), outputs="text", live=True)

# Khởi chạy giao diện
iface.launch(share=True)