Upload 4 files
Browse files- README (3).md +39 -0
- app (2).py +53 -0
- gradio_app (2).py +50 -0
- requirements (2).txt +5 -0
README (3).md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Voice Detection - Deepfake Voice Detector
|
| 3 |
+
emoji: 🎤
|
| 4 |
+
colorFrom: "#FF6F61"
|
| 5 |
+
colorTo: "#6A5ACD"
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: "3.0.0"
|
| 8 |
+
app_file: gradio_app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Voice Detection - Deepfake Voice Detector
|
| 13 |
+
|
| 14 |
+
## Mô tả
|
| 15 |
+
|
| 16 |
+
Ứng dụng này sử dụng mô hình học máy để phân loại giọng nói thực và giọng nói giả. Ứng dụng sử dụng Flask API để xử lý các yêu cầu từ người dùng và Gradio để cung cấp giao diện người dùng.
|
| 17 |
+
|
| 18 |
+
## Cách cài đặt và chạy ứng dụng
|
| 19 |
+
|
| 20 |
+
1. Cài đặt các thư viện cần thiết:
|
| 21 |
+
```bash
|
| 22 |
+
pip install -r requirements.txt
|
| 23 |
+
```
|
| 24 |
+
|
| 25 |
+
2. Chạy ứng dụng Flask:
|
| 26 |
+
```bash
|
| 27 |
+
python app.py
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
3. Truy cập giao diện Gradio:
|
| 31 |
+
```bash
|
| 32 |
+
python gradio_app.py
|
| 33 |
+
```
|
| 34 |
+
|
| 35 |
+
4. Tải lên các tệp âm thanh `.wav` để nhận diện giọng nói.
|
| 36 |
+
|
| 37 |
+
## Các mô hình sử dụng
|
| 38 |
+
|
| 39 |
+
Mô hình sử dụng RandomForestClassifier để phân loại giọng nói thành "Real" hoặc "Fake".
|
app (2).py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import os
|
| 3 |
+
import librosa
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
|
| 9 |
+
# Hàm trích xuất đặc trưng từ file âm thanh
|
| 10 |
+
def extract_features(file_path):
|
| 11 |
+
try:
|
| 12 |
+
audio, sr = librosa.load(file_path, sr=None)
|
| 13 |
+
mfcc = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=13)
|
| 14 |
+
mfcc_mean = np.mean(mfcc.T, axis=0)
|
| 15 |
+
return mfcc_mean
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print(f"Lỗi xử lý {file_path}: {e}")
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
# Hàm dự đoán giọng nói (thực hay giả)
|
| 21 |
+
def predict_voice(file_path):
|
| 22 |
+
if not os.path.exists(file_path):
|
| 23 |
+
return "Không tìm thấy file."
|
| 24 |
+
|
| 25 |
+
feat = extract_features(file_path)
|
| 26 |
+
if feat is None:
|
| 27 |
+
return "Không thể trích xuất đặc trưng từ file."
|
| 28 |
+
|
| 29 |
+
if 'clf' not in globals():
|
| 30 |
+
return "Mô hình chưa được huấn luyện."
|
| 31 |
+
|
| 32 |
+
# Dự đoán
|
| 33 |
+
pred = clf.predict([feat])[0]
|
| 34 |
+
return "Real ✅" if pred == 0 else "Fake ❌"
|
| 35 |
+
|
| 36 |
+
# API để nhận tệp âm thanh và trả về kết quả dự đoán
|
| 37 |
+
@app.route('/predict', methods=['POST'])
|
| 38 |
+
def predict():
|
| 39 |
+
file = request.files.get('file')
|
| 40 |
+
if not file:
|
| 41 |
+
return jsonify({"error": "No file provided"}), 400
|
| 42 |
+
|
| 43 |
+
# Lưu file vào thư mục tạm
|
| 44 |
+
file_path = os.path.join("/tmp", file.filename)
|
| 45 |
+
file.save(file_path)
|
| 46 |
+
|
| 47 |
+
# Gọi hàm dự đoán
|
| 48 |
+
result = predict_voice(file_path)
|
| 49 |
+
|
| 50 |
+
return jsonify({"prediction": result})
|
| 51 |
+
|
| 52 |
+
if __name__ == '__main__':
|
| 53 |
+
app.run(debug=True)
|
gradio_app (2).py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import librosa
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
|
| 7 |
+
# Hàm trích xuất đặc trưng
|
| 8 |
+
def extract_features(file_path):
|
| 9 |
+
try:
|
| 10 |
+
audio, sr = librosa.load(file_path, sr=None)
|
| 11 |
+
mfcc = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=13)
|
| 12 |
+
mfcc_mean = np.mean(mfcc.T, axis=0)
|
| 13 |
+
return mfcc_mean
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"Lỗi xử lý {file_path}: {e}")
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
# Hàm dự đoán giọng nói (thực hay giả)
|
| 19 |
+
def predict_voice(file_path):
|
| 20 |
+
if not os.path.exists(file_path):
|
| 21 |
+
return "Không tìm thấy file."
|
| 22 |
+
|
| 23 |
+
feat = extract_features(file_path)
|
| 24 |
+
if feat is None:
|
| 25 |
+
return "Không thể trích xuất đặc trưng từ file."
|
| 26 |
+
|
| 27 |
+
if 'clf' not in globals():
|
| 28 |
+
return "Mô hình chưa được huấn luyện."
|
| 29 |
+
|
| 30 |
+
# Dự đoán
|
| 31 |
+
pred = clf.predict([feat])[0]
|
| 32 |
+
return "Real ✅" if pred == 0 else "Fake ❌"
|
| 33 |
+
|
| 34 |
+
# Tạo giao diện Gradio
|
| 35 |
+
def predict_from_upload(file_path):
|
| 36 |
+
return predict_voice(file_path)
|
| 37 |
+
|
| 38 |
+
# Đảm bảo mô hình đã được huấn luyện
|
| 39 |
+
clf = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 40 |
+
# Train mô hình của bạn ở đây (hoặc nạp mô hình đã huấn luyện từ file nếu cần)
|
| 41 |
+
|
| 42 |
+
# Cấu hình Gradio
|
| 43 |
+
interface = gr.Interface(
|
| 44 |
+
fn=predict_from_upload,
|
| 45 |
+
inputs=gr.Audio(type="filepath"),
|
| 46 |
+
outputs="text",
|
| 47 |
+
title="Phát hiện giọng giả (Deepfake Voice Detector)"
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
interface.launch(debug=True)
|
requirements (2).txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
librosa
|
| 3 |
+
numpy
|
| 4 |
+
scikit-learn
|
| 5 |
+
gradio
|