Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import librosa
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# --- Load the trained Random Forest model ---
|
| 7 |
+
MODEL_PATH = "model.joblib"
|
| 8 |
+
model = joblib.load(MODEL_PATH)
|
| 9 |
+
|
| 10 |
+
# --- Extract MFCC features from audio ---
|
| 11 |
+
def extract_mfcc(file_path):
|
| 12 |
+
y, sr = librosa.load(file_path, sr=None)
|
| 13 |
+
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
|
| 14 |
+
return np.mean(mfcc, axis=1)
|
| 15 |
+
|
| 16 |
+
# --- Prediction function ---
|
| 17 |
+
def predict_audio(audio_file):
|
| 18 |
+
try:
|
| 19 |
+
features = extract_mfcc(audio_file).reshape(1, -1)
|
| 20 |
+
prediction = model.predict(features)[0]
|
| 21 |
+
return "True Story" if prediction == 1 else "Deceptive Story"
|
| 22 |
+
except Exception as e:
|
| 23 |
+
return f"Error: {e}"
|
| 24 |
+
|
| 25 |
+
# --- Gradio Interface ---
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=predict_audio,
|
| 28 |
+
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 29 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 30 |
+
title="Truth Detection Model",
|
| 31 |
+
description="Upload an audio clip to detect if the story is true or deceptive."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
iface.launch()
|