szili2011's picture
Update app.py
90e00da verified
raw
history blame
2.71 kB
import gradio as gr
import tensorflow as tf
import numpy as np
import nltk
from nltk.corpus import cmudict
from scipy.io.wavfile import write
# --- FIX 1: Define sample_rate as a global constant ---
SAMPLE_RATE = 22050
# Download required NLTK data
nltk.download('averaged_perceptron_tagger')
nltk.download('cmudict')
# Load your model from the root directory
model = tf.keras.models.load_model('audio_model.h5')
# Preprocess input text
def preprocess_text(text):
d = cmudict.dict()
words = text.lower().split()
phonemes = []
for word in words:
if word in d:
phonemes.append(d[word][0])
else:
phonemes.append(['UNKNOWN'])
flattened_phonemes = [p for sublist in phonemes for p in sublist]
# Create dummy 13-feature vectors for each phoneme (implement your own feature extraction)
num_features = 13
sequence_length = len(flattened_phonemes)
input_data = np.random.rand(sequence_length, num_features)
# Add batch dimension
input_data = np.expand_dims(input_data, axis=0) # Shape (1, sequence_length, 13)
return input_data
# Convert model output to an audio file
def convert_to_audio(model_output, filename="output.wav"):
# Now uses the global SAMPLE_RATE constant
normalized_output = np.interp(model_output, (model_output.min(), model_output.max()), (-1, 1))
write(filename, SAMPLE_RATE, normalized_output.astype(np.float32))
return filename
# Define function to generate sound effect
def generate_sfx(text, duration): # duration no longer needs a default here
input_data = preprocess_text(text)
prediction = model.predict(input_data)
# Generate longer output by repeating or padding
# This line now works because SAMPLE_RATE is defined globally
audio_data = np.tile(prediction.flatten(), (duration * SAMPLE_RATE // len(prediction.flatten()) + 1))[:duration * SAMPLE_RATE]
audio_file = convert_to_audio(audio_data, filename="output.wav")
return audio_file
# Define the Gradio interface
interface = gr.Interface(
fn=generate_sfx,
inputs=[
gr.Textbox(label="Enter a Word", placeholder="Write a Word To Convert it into SFX Sound"),
# --- FIX 2: Corrected the default slider value to be within the min/max range ---
gr.Slider(minimum=2, maximum=20, value=5, label="Duration (seconds)")
],
outputs=gr.Audio(label="Generated SFX", type="filepath"),
title="SFX Generator from Text",
description="Enter a word or sentence, and the model will generate an SFX sound.",
)
# Run the interface
if __name__ == "__main__":
tf.config.set_visible_devices([], 'GPU') # Disable GPU
interface.launch()