Create inference.py
Browse files- inference.py +41 -0
inference.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
# Function to extract MFCC features
|
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)
|
20 |
+
prediction = model.predict(features)[0]
|
21 |
+
return "True Story" if prediction == 1 else "Deceptive Story"
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error during prediction: {e}"
|
24 |
+
|
25 |
+
# Gradio Blocks layout
|
26 |
+
with gr.Blocks() as demo:
|
27 |
+
gr.Markdown("<h1 style='text-align: center;'>Truth Detection from Audio Stories</h1>")
|
28 |
+
gr.Markdown(
|
29 |
+
"<p style='text-align: center;'>"
|
30 |
+
"This tool analyzes an audio story and predicts whether it is true or deceptive "
|
31 |
+
"based on MFCC features and a trained Random Forest classifier."
|
32 |
+
"</p>"
|
33 |
+
)
|
34 |
+
audio_input = gr.Audio(type="filepath", label="Upload Audio File")
|
35 |
+
output = gr.Textbox(label="Prediction")
|
36 |
+
submit_btn = gr.Button("Predict")
|
37 |
+
submit_btn.click(fn=predict_audio, inputs=audio_input, outputs=output)
|
38 |
+
gr.Markdown("<p style='text-align: center; font-size: 12px; color: gray;'>Developed by Sangam Sanjay Bhamare, 2025.</p>")
|
39 |
+
|
40 |
+
if __name__ == "__main__":
|
41 |
+
demo.launch()
|