new-space / app.py
aliabd's picture
aliabd HF staff
initial commit
e98cc83 verified
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
# Function to convert an image to an audio signal
def image_to_audio(image):
# Convert the image to grayscale
gray_image = np.dot(image[..., :3], [0.2989, 0.5870, 0.1140])
# Normalize the grayscale image to the range [0, 1]
normalized_image = gray_image / 255.0
# Convert the normalized image to a 1D array
audio_signal = normalized_image.flatten()
# Generate a sample rate
sample_rate = 44100
# Return the audio signal and sample rate
return (sample_rate, audio_signal)
# Create a Gradio interface
with gr.Blocks() as demo:
# Input component for the image
input_image = gr.Image(label="Input Image")
# Output component for the audio
output_audio = gr.Audio(label="Output Audio", streaming=True, autoplay=True)
# Set up the event listener to convert the image to audio
input_image.change(image_to_audio, inputs=input_image, outputs=output_audio)
# Launch the interface
demo.launch(show_error=True)