CoolFace
Apppublic

sangambhamare/TruthDetection

sourceHugging Facemitupdated 1y agoView on Hugging Face
5likes
app.py59 linesDownload Raw Back to root
1import os2import librosa3import numpy as np4import joblib5import gradio as gr6from huggingface_hub import hf_hub_download7 8# --- Load model from Hugging Face Hub ---9MODEL_REPO = "sangambhamare/TruthDetection"10MODEL_FILENAME = "model.joblib"11model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)12model = joblib.load(model_path)13 14# --- Load interactive report HTML (must be in same directory) ---15report_html = ""16if os.path.exists("interactive_report.html"):17    with open("interactive_report.html", "r", encoding="utf-8") as f:18        report_html = f.read()19 20# --- MFCC feature extraction ---21def extract_mfcc(file_path):22    y, sr = librosa.load(file_path, sr=None)23    mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)24    return np.mean(mfcc, axis=1)25 26# --- Prediction function ---27def predict_audio(audio_file):28    try:29        features = extract_mfcc(audio_file).reshape(1, -1)30        prediction = model.predict(features)[0]31        return "True Story" if prediction == 1 else "Deceptive Story"32    except Exception as e:33        return f"Error: {e}"34 35# --- Gradio Interface ---36with gr.Blocks() as demo:37    gr.Markdown("<h1 style='text-align: center;'>Truth Detection from Audio Stories</h1>")38    gr.Markdown(39        "<p style='text-align: center;'>"40        "This tool analyzes an audio story and predicts whether it is true or deceptive "41        "based on MFCC features and a trained Random Forest classifier."42        "</p>"43    )44 45    audio_input = gr.Audio(type="filepath", label="Upload WAV Audio File")46    output = gr.Textbox(label="Prediction")47    submit_btn = gr.Button("Predict")48    submit_btn.click(fn=predict_audio, inputs=audio_input, outputs=output)49 50    if report_html:51        gr.Markdown("<hr>")52        gr.Markdown("<h3 style='text-align: center;'>Interactive Report</h3>")53        gr.HTML(value=report_html)54 55    gr.Markdown("<p style='text-align: center; font-size: 12px; color: gray;'>Developed by Sangam Sanjay Bhamare, 2025.</p>")56 57if __name__ == "__main__":58    demo.launch()59