Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import librosa
|
| 3 |
+
import numpy as np
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load the pre-trained model
|
| 8 |
+
model_path = 'sound_to_text_model.h5'
|
| 9 |
+
model = tf.keras.models.load_model(model_path)
|
| 10 |
+
|
| 11 |
+
# Function to extract features from audio
|
| 12 |
+
def extract_features(file_path):
|
| 13 |
+
y_audio, sr = librosa.load(file_path, duration=2.0)
|
| 14 |
+
mfccs = librosa.feature.mfcc(y=y_audio, sr=sr, n_mfcc=13)
|
| 15 |
+
return np.mean(mfccs.T, axis=0) # Average to create a fixed size
|
| 16 |
+
|
| 17 |
+
# Function to predict text from audio
|
| 18 |
+
def predict_sound_text(audio):
|
| 19 |
+
features = extract_features(audio.name)
|
| 20 |
+
prediction = model.predict(np.array([features]))
|
| 21 |
+
label = encoder.inverse_transform([np.argmax(prediction)])
|
| 22 |
+
return label[0]
|
| 23 |
+
|
| 24 |
+
# Define Gradio interface
|
| 25 |
+
interface = gr.Interface(fn=predict_sound_text,
|
| 26 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
| 27 |
+
outputs="text",
|
| 28 |
+
title="Audio to Text Converter",
|
| 29 |
+
description="Upload an audio file (MP3 format) and get the textual representation.")
|
| 30 |
+
|
| 31 |
+
# Launch the interface
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
interface.launch()
|