TruthDetection / inference.py
sangambhamare's picture
Create inference.py
768c4a8 verified
import joblib
import librosa
import numpy as np
import gradio as gr
# Load the trained Random Forest model
MODEL_PATH = "model.joblib"
model = joblib.load(MODEL_PATH)
# Function to extract MFCC features
def extract_mfcc(file_path):
y, sr = librosa.load(file_path, sr=None)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
return np.mean(mfcc, axis=1)
# Prediction function
def predict_audio(audio_file):
try:
features = extract_mfcc(audio_file).reshape(1, -1)
prediction = model.predict(features)[0]
return "True Story" if prediction == 1 else "Deceptive Story"
except Exception as e:
return f"Error during prediction: {e}"
# Gradio Blocks layout
with gr.Blocks() as demo:
gr.Markdown("<h1 style='text-align: center;'>Truth Detection from Audio Stories</h1>")
gr.Markdown(
"<p style='text-align: center;'>"
"This tool analyzes an audio story and predicts whether it is true or deceptive "
"based on MFCC features and a trained Random Forest classifier."
"</p>"
)
audio_input = gr.Audio(type="filepath", label="Upload Audio File")
output = gr.Textbox(label="Prediction")
submit_btn = gr.Button("Predict")
submit_btn.click(fn=predict_audio, inputs=audio_input, outputs=output)
gr.Markdown("<p style='text-align: center; font-size: 12px; color: gray;'>Developed by Sangam Sanjay Bhamare, 2025.</p>")
if __name__ == "__main__":
demo.launch()