Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import librosa
|
3 |
+
import librosa.display
|
4 |
+
import numpy as np
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import torch
|
7 |
+
from diffusers import StableDiffusionPipeline
|
8 |
+
|
9 |
+
# Load AI Art Model (Stable Diffusion)
|
10 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
|
11 |
+
pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
|
13 |
+
def process_audio(audio_file):
|
14 |
+
# Load audio file
|
15 |
+
y, sr = librosa.load(audio_file, sr=22050)
|
16 |
+
|
17 |
+
# Convert to Mel spectrogram
|
18 |
+
mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr)
|
19 |
+
mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)
|
20 |
+
|
21 |
+
# Save spectrogram image
|
22 |
+
plt.figure(figsize=(6, 4))
|
23 |
+
librosa.display.specshow(mel_spectrogram, sr=sr, x_axis='time', y_axis='mel')
|
24 |
+
plt.colorbar(format='%+2.0f dB')
|
25 |
+
plt.title("Mel Spectrogram")
|
26 |
+
spectrogram_path = "spectrogram.png"
|
27 |
+
plt.savefig(spectrogram_path)
|
28 |
+
plt.close()
|
29 |
+
|
30 |
+
# Generate AI Art (Based on Audio)
|
31 |
+
prompt = "An abstract painting inspired by music and sound waves"
|
32 |
+
image = pipe(prompt).images[0]
|
33 |
+
image_path = "generated_art.png"
|
34 |
+
image.save(image_path)
|
35 |
+
|
36 |
+
return spectrogram_path, image_path
|
37 |
+
|
38 |
+
# Create Web Interface
|
39 |
+
interface = gr.Interface(
|
40 |
+
fn=process_audio,
|
41 |
+
inputs=gr.Audio(source="upload", type="filepath"),
|
42 |
+
outputs=[gr.Image(type="filepath", label="Spectrogram"),
|
43 |
+
gr.Image(type="filepath", label="AI-Generated Art")],
|
44 |
+
title="Audio2Art 🎨",
|
45 |
+
description="Upload an audio file, and this AI will generate an artwork inspired by the sound."
|
46 |
+
)
|
47 |
+
|
48 |
+
interface.launch()
|