Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import joblib
|
| 4 |
+
import librosa
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# Load model (adjust paths as needed)
|
| 8 |
+
model = torch.load("voice_recognition_fullmodel.pth")
|
| 9 |
+
label_encoder = joblib.load("label_encoder.joblib")
|
| 10 |
+
|
| 11 |
+
def predict(audio_file):
|
| 12 |
+
# Extract features
|
| 13 |
+
features = extract_features(audio_file)
|
| 14 |
+
if features is None:
|
| 15 |
+
return "Error processing audio"
|
| 16 |
+
|
| 17 |
+
# Prepare input
|
| 18 |
+
input_tensor = torch.tensor(features).unsqueeze(0).unsqueeze(0).float()
|
| 19 |
+
|
| 20 |
+
# Predict
|
| 21 |
+
with torch.no_grad():
|
| 22 |
+
outputs = model(input_tensor)
|
| 23 |
+
_, predicted = torch.max(outputs, 1)
|
| 24 |
+
|
| 25 |
+
user = label_encoder.inverse_transform([predicted.item()])[0]
|
| 26 |
+
return f"Recognized user: {user}"
|
| 27 |
+
|
| 28 |
+
# Create interface
|
| 29 |
+
iface = gr.Interface(
|
| 30 |
+
fn=predict,
|
| 31 |
+
inputs=gr.Audio(source="microphone", type="filepath"),
|
| 32 |
+
outputs="text",
|
| 33 |
+
title="Voice Recognition Security System",
|
| 34 |
+
description="Upload an audio file or record your voice to test user recognition."
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
iface.launch()
|