Spaces:
Running
Running
File size: 2,038 Bytes
a946755 910f47b a946755 910f47b a946755 ae46b97 a946755 910f47b ae46b97 910f47b ae46b97 910f47b ae46b97 a946755 b7f9da0 a946755 ae46b97 b7f9da0 a946755 ae46b97 a946755 910f47b a946755 |
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import os
import librosa
import numpy as np
import joblib
import gradio as gr
from huggingface_hub import hf_hub_download
# --- Load model from Hugging Face Hub ---
MODEL_REPO = "sangambhamare/TruthDetection"
MODEL_FILENAME = "model.joblib"
model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
model = joblib.load(model_path)
# --- Load interactive report HTML (must be in same directory) ---
report_html = ""
if os.path.exists("interactive_report.html"):
with open("interactive_report.html", "r", encoding="utf-8") as f:
report_html = f.read()
# --- MFCC feature extraction ---
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: {e}"
# --- Gradio Interface ---
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 WAV Audio File")
output = gr.Textbox(label="Prediction")
submit_btn = gr.Button("Predict")
submit_btn.click(fn=predict_audio, inputs=audio_input, outputs=output)
if report_html:
gr.Markdown("<hr>")
gr.Markdown("<h3 style='text-align: center;'>Interactive Report</h3>")
gr.HTML(value=report_html)
gr.Markdown("<p style='text-align: center; font-size: 12px; color: gray;'>Developed by Sangam Sanjay Bhamare, 2025.</p>")
if __name__ == "__main__":
demo.launch()
|