Spaces:
Running
Running
| 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() | |