sangambhamare commited on
Commit
a946755
·
verified ·
1 Parent(s): cb7bb51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -1,19 +1,24 @@
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)
@@ -22,14 +27,20 @@ def predict_audio(audio_file):
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()
 
1
+ import os
2
  import librosa
3
  import numpy as np
4
+ import joblib
5
  import gradio as gr
6
+ from huggingface_hub import hf_hub_download
7
+
8
+ # Download the model from Hugging Face Hub
9
+ MODEL_REPO = "sangambhamare/TruthDetection"
10
+ MODEL_FILENAME = "model.joblib"
11
 
12
+ model_path = hf_hub_download(repo_id=MODEL_REPO, filename=MODEL_FILENAME)
13
+ model = joblib.load(model_path)
 
14
 
15
+ # MFCC extraction
16
  def extract_mfcc(file_path):
17
  y, sr = librosa.load(file_path, sr=None)
18
  mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
19
  return np.mean(mfcc, axis=1)
20
 
21
+ # Prediction
22
  def predict_audio(audio_file):
23
  try:
24
  features = extract_mfcc(audio_file).reshape(1, -1)
 
27
  except Exception as e:
28
  return f"Error: {e}"
29
 
30
+ # Gradio UI
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("<h1 style='text-align: center;'>Truth Detection from Audio Stories</h1>")
33
+ gr.Markdown(
34
+ "<p style='text-align: center;'>"
35
+ "This tool analyzes a 30-second audio story and predicts whether it is true or deceptive "
36
+ "based on MFCC features and a trained Random Forest classifier."
37
+ "</p>"
38
+ )
39
+ audio_input = gr.Audio(type="filepath", label="Upload WAV Audio File (30 seconds)")
40
+ output = gr.Textbox(label="Prediction")
41
+ submit_btn = gr.Button("Predict")
42
+ submit_btn.click(fn=predict_audio, inputs=audio_input, outputs=output)
43
+ gr.Markdown("<p style='text-align: center; font-size: 12px; color: gray;'>Developed by Sangam Sanjay Bhamare, 2025.</p>")
44
 
45
  if __name__ == "__main__":
46
+ demo.launch()