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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""Untitled1.ipynb
|
3 |
+
|
4 |
+
Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1yIRyQq7IUIqBQiXAPKoa0n2gT8Lr_qMw
|
8 |
+
"""
|
9 |
+
|
10 |
+
!pip install openai
|
11 |
+
!pip install imageio
|
12 |
+
!pip install replicate
|
13 |
+
!pip install gradio
|
14 |
+
|
15 |
+
"""## Importing Libraries"""
|
16 |
+
|
17 |
+
import openai
|
18 |
+
import re
|
19 |
+
import pandas as pd
|
20 |
+
import replicate
|
21 |
+
import pandas as pd
|
22 |
+
from PIL import Image
|
23 |
+
import requests
|
24 |
+
from io import BytesIO
|
25 |
+
import os
|
26 |
+
from IPython.display import Audio, display
|
27 |
+
import gradio as gr
|
28 |
+
import numpy
|
29 |
+
from PIL import ImageDraw
|
30 |
+
from PIL import ImageFont
|
31 |
+
import random
|
32 |
+
|
33 |
+
!git clone https://github.com/Neelanjan-chakraborty/MelodyGen.git
|
34 |
+
|
35 |
+
!ls MelodyGen
|
36 |
+
|
37 |
+
"""## Function To Generate Genre Based on Mood"""
|
38 |
+
|
39 |
+
def generate_genre_recommendations(mood,key):
|
40 |
+
openai.api_key=key
|
41 |
+
response = openai.Completion.create(
|
42 |
+
engine='text-davinci-003',
|
43 |
+
prompt=f"I'm feeling {mood} and I want only genre recommendations and not songs also please separate using comma.",
|
44 |
+
max_tokens=100,
|
45 |
+
n=1,
|
46 |
+
stop=None,
|
47 |
+
temperature=0.5
|
48 |
+
)
|
49 |
+
|
50 |
+
genres = set(response.choices[0].text.strip().split(',')) # Extract genres from response
|
51 |
+
|
52 |
+
#if len(genres) < 3:
|
53 |
+
# # Provide some default genre recommendations
|
54 |
+
# genres = {'Pop', 'Rock', 'Electronic'}
|
55 |
+
|
56 |
+
genre_recommendations = list(genres)[:3] # Limit to 3 unique genres
|
57 |
+
|
58 |
+
return ', '.join(genre_recommendations) # Return as comma-separated string
|
59 |
+
|
60 |
+
def recommend(user_mood,key):
|
61 |
+
genre_recommendations = generate_genre_recommendations(user_mood,key)
|
62 |
+
out = genre_recommendations.split(',')
|
63 |
+
return out
|
64 |
+
|
65 |
+
"""## Function to Generate Melody"""
|
66 |
+
|
67 |
+
def melodygen(api_token,prompt):
|
68 |
+
client = replicate.Client(api_token)
|
69 |
+
output = client.run(
|
70 |
+
"joehoover/musicgen:ba9bdc5a86f60525ba23590a03ae1e407b9a40f4a318a85af85748d641e6659f",
|
71 |
+
input={"model_version": "melody","prompt":prompt,"continuation_start": 0,"continuation_end": 30, "duration":30,
|
72 |
+
}
|
73 |
+
)
|
74 |
+
return output
|
75 |
+
|
76 |
+
#api_token='r8_Z30IhmLhqMoeJeNkl4S92JzSp8stlh82NZAnI'
|
77 |
+
#prompt=input("Enter your Genre : ")
|
78 |
+
#melodygen(api_token,prompt)
|
79 |
+
|
80 |
+
"""## Function To Generate a Song Name"""
|
81 |
+
|
82 |
+
def generate_song_name(mood,key):
|
83 |
+
openai.api_key=key
|
84 |
+
response = openai.Completion.create(
|
85 |
+
engine='text-davinci-003',
|
86 |
+
prompt=f"I'm feeling {mood} and I want only random ai generated Song Name and please separate these names using comma.",
|
87 |
+
max_tokens=100,
|
88 |
+
n=1,
|
89 |
+
stop=None,
|
90 |
+
temperature=0.5
|
91 |
+
)
|
92 |
+
|
93 |
+
names = set(response.choices[0].text.strip().split(',')) # Extract genres from response
|
94 |
+
get_names = list(names)[:3] # Limit to 3 unique genres
|
95 |
+
return ', '.join(get_names) # Return as comma-separated string(mood):
|
96 |
+
|
97 |
+
"""## Function To Generate Album Art and Album Art Animation"""
|
98 |
+
|
99 |
+
def albumart(user_mood,key,name):
|
100 |
+
openai.api_key=key
|
101 |
+
response = openai.Image.create(
|
102 |
+
prompt=f"Music album art of mood{user_mood}",
|
103 |
+
n=1,
|
104 |
+
size="512x512"
|
105 |
+
)
|
106 |
+
image_url = response['data'][0]['url']
|
107 |
+
# Fetch the image from the URL
|
108 |
+
response = requests.get(image_url)
|
109 |
+
response.raise_for_status()
|
110 |
+
# Load the image into a PIL Image object
|
111 |
+
image = Image.open(BytesIO(response.content))
|
112 |
+
# Load the font
|
113 |
+
font_path = "MelodyGen/Lobster-Regular.ttf"
|
114 |
+
font_size = 48
|
115 |
+
font = ImageFont.truetype(font_path, font_size)
|
116 |
+
position = (image.width // 2, image.height // 2)
|
117 |
+
# Create a new ImageDraw object
|
118 |
+
draw = ImageDraw.Draw(image)
|
119 |
+
# Get a random color
|
120 |
+
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
|
121 |
+
# Calculate the position to center the text
|
122 |
+
text_width, text_height = draw.textsize(name, font=font)
|
123 |
+
text_position = (position[0] - text_width // 2, position[1] - text_height // 2)
|
124 |
+
# Add the text to the image
|
125 |
+
draw.text(text_position, name, font=font, fill=color)
|
126 |
+
path='album_art.jpg'
|
127 |
+
image.save(path)
|
128 |
+
return path
|
129 |
+
|
130 |
+
def overlay_images(background_image_path, foreground_image_path):
|
131 |
+
# Open the background and foreground images
|
132 |
+
background_image = Image.open(background_image_path).resize((512, 512))
|
133 |
+
foreground_image = Image.open(foreground_image_path).resize((1024, 768)).convert("RGBA")
|
134 |
+
|
135 |
+
# Create a new image with transparency
|
136 |
+
final_image = Image.new("RGBA", (1024, 768))
|
137 |
+
|
138 |
+
# Calculate the position to paste the background image at the center
|
139 |
+
paste_x = int((1024 - background_image.width) / 2)
|
140 |
+
paste_y = int((768 - background_image.height) / 2)
|
141 |
+
|
142 |
+
# Overlay the background image at the center of the final image
|
143 |
+
final_image.paste(background_image, (paste_x, paste_y))
|
144 |
+
|
145 |
+
# Overlay the foreground image on the background image
|
146 |
+
final_image.paste(foreground_image, (0, 0), mask=foreground_image)
|
147 |
+
|
148 |
+
# Convert the image mode to RGB (removing alpha channel)
|
149 |
+
final_image = final_image.convert("RGB")
|
150 |
+
|
151 |
+
# Return the final image
|
152 |
+
return final_image
|
153 |
+
|
154 |
+
|
155 |
+
background_image_path = "/content/input.jpg" # Path to the background image
|
156 |
+
foreground_image_path = "/content/Transparent CD Case Mockup2.png" # Path to the foreground PNG image
|
157 |
+
|
158 |
+
result_image = overlay_images(background_image_path, foreground_image_path)
|
159 |
+
|
160 |
+
result_image.save("result.jpg") # Save the final image to a file
|
161 |
+
result_image.show() # Display the final image
|
162 |
+
|
163 |
+
!pip install imageio
|
164 |
+
|
165 |
+
from PIL import Image, ImageSequence
|
166 |
+
|
167 |
+
def overlay_images(background_image_path, foreground_image_path, num_frames=36, rotation_angle=10):
|
168 |
+
# Open the background and foreground images
|
169 |
+
background_image = Image.open(background_image_path).resize((512, 512))
|
170 |
+
foreground_image = Image.open(foreground_image_path).resize((1024, 768)).convert("RGBA")
|
171 |
+
|
172 |
+
# Create a new image with transparency
|
173 |
+
final_image = Image.new("RGBA", (1024, 768))
|
174 |
+
|
175 |
+
# Calculate the position to paste the background image at the center
|
176 |
+
paste_x = int((1024 - background_image.width) / 2)
|
177 |
+
paste_y = int((768 - background_image.height) / 2)
|
178 |
+
|
179 |
+
# Create a list to store the frames of the GIF animation
|
180 |
+
frames = []
|
181 |
+
|
182 |
+
# Calculate the center point of the background image
|
183 |
+
center_x = int(background_image.width / 2)
|
184 |
+
center_y = int(background_image.height / 2)
|
185 |
+
|
186 |
+
# Rotate the background image and create frames for the GIF animation
|
187 |
+
for angle in range(0, 360, rotation_angle):
|
188 |
+
# Create a copy of the background image
|
189 |
+
rotated_image = background_image.copy()
|
190 |
+
|
191 |
+
# Rotate the image by the specified angle around its center
|
192 |
+
rotated_image = rotated_image.rotate(angle, resample=Image.BICUBIC, center=(center_x, center_y))
|
193 |
+
|
194 |
+
# Overlay the rotated background image at the center of the final image
|
195 |
+
final_image.paste(rotated_image, (paste_x, paste_y))
|
196 |
+
|
197 |
+
# Overlay the foreground image on the background image
|
198 |
+
final_image.paste(foreground_image, (0, 0), mask=foreground_image)
|
199 |
+
|
200 |
+
# Append the current frame to the list of frames
|
201 |
+
frames.append(final_image.copy())
|
202 |
+
|
203 |
+
# Convert the image mode of frames to RGB (removing alpha channel)
|
204 |
+
frames_rgb = [frame.convert("RGB") for frame in frames]
|
205 |
+
|
206 |
+
# Save the frames as an animated GIF
|
207 |
+
output_path = "animation.gif"
|
208 |
+
frames_rgb[0].save(output_path, save_all=True, append_images=frames_rgb[1:], loop=0, duration=100,optimize=True, quality=100)
|
209 |
+
|
210 |
+
# Return the output path of the animated GIF
|
211 |
+
return output_path
|
212 |
+
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
#background_image_path = "/content/input.png" # Path to the background image
|
217 |
+
#foreground_image_path = "/content/Transparent CD Case Mockup2.png" # Path to the foreground PNG image
|
218 |
+
#rotation_angle = 10
|
219 |
+
|
220 |
+
#overlay_images(background_image_path, foreground_image_path)
|
221 |
+
|
222 |
+
from PIL import Image, ImageSequence
|
223 |
+
def overlay_images(background_image_path):
|
224 |
+
foreground_image_path='MelodyGen/Mockup4.png'
|
225 |
+
num_frames=36
|
226 |
+
rotation_angle=10
|
227 |
+
# Open the background and foreground images
|
228 |
+
background_image = Image.open(background_image_path).resize((600, 600))
|
229 |
+
foreground_image = Image.open(foreground_image_path).resize((1024, 768)).convert("RGBA")
|
230 |
+
|
231 |
+
# Create a new image with transparency
|
232 |
+
final_image = Image.new("RGBA", (1024, 768))
|
233 |
+
|
234 |
+
# Calculate the position to paste the background image at the center
|
235 |
+
paste_x = int(100.50)
|
236 |
+
paste_y = int(100.50)
|
237 |
+
|
238 |
+
# Create a list to store the frames of the GIF animation
|
239 |
+
frames = []
|
240 |
+
|
241 |
+
# Calculate the center point of the background image
|
242 |
+
center_x = int(background_image.width / 2)
|
243 |
+
center_y = int(background_image.height / 2)
|
244 |
+
|
245 |
+
# Rotate the background image and create frames for the GIF animation
|
246 |
+
for angle in range(0, 360, rotation_angle):
|
247 |
+
# Create a copy of the background image
|
248 |
+
rotated_image = background_image.copy()
|
249 |
+
|
250 |
+
# Rotate the image by the specified angle around its center
|
251 |
+
rotated_image = rotated_image.rotate(angle, resample=Image.BICUBIC, center=(center_x, center_y))
|
252 |
+
|
253 |
+
# Overlay the rotated background image at the center of the final image
|
254 |
+
final_image.paste(rotated_image, (paste_x, paste_y))
|
255 |
+
|
256 |
+
# Overlay the foreground image on the background image
|
257 |
+
final_image.paste(foreground_image, (0, 0), mask=foreground_image)
|
258 |
+
|
259 |
+
# Append the current frame to the list of frames
|
260 |
+
frames.append(final_image.copy())
|
261 |
+
|
262 |
+
# Convert the image mode of frames to RGB (removing alpha channel)
|
263 |
+
frames_rgb = [frame.convert("RGB") for frame in frames]
|
264 |
+
|
265 |
+
# Save the frames as an animated GIF
|
266 |
+
output_path = "animation2.gif"
|
267 |
+
frames_rgb[0].save(output_path, save_all=True, append_images=frames_rgb[1:], loop=0, duration=100,optimize=True, quality=100)
|
268 |
+
|
269 |
+
# Return the output path of the animated GIF
|
270 |
+
return output_path
|
271 |
+
|
272 |
+
|
273 |
+
|
274 |
+
|
275 |
+
#background_image_path = "/content/input.png" # Path to the background image
|
276 |
+
#foreground_image_path = "/content/Mockup4.png" # Path to the foreground PNG image
|
277 |
+
#rotation_angle = 10
|
278 |
+
|
279 |
+
#overlay_images(background_image_path, foreground_image_path)
|
280 |
+
|
281 |
+
def main_func(user_mood,key,api_token,genre):
|
282 |
+
if genre is None:
|
283 |
+
out = recommend(user_mood, key)[0]
|
284 |
+
else:
|
285 |
+
out = genre
|
286 |
+
name=generate_song_name(user_mood,key).split(",")
|
287 |
+
image=albumart(user_mood ,key,name[0])
|
288 |
+
album_art=overlay_images(image)
|
289 |
+
song=melodygen(api_token,out[0])
|
290 |
+
|
291 |
+
return album_art,name[0],genre,song
|
292 |
+
|
293 |
+
|
294 |
+
"""## Gradio UI"""
|
295 |
+
|
296 |
+
css1= """.gradio-container{
|
297 |
+
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
|
298 |
+
background-size: 400% 400%;
|
299 |
+
animation: gradient 15s ease infinite;
|
300 |
+
height: 100vh;
|
301 |
+
}
|
302 |
+
|
303 |
+
@keyframes gradient {
|
304 |
+
0% {
|
305 |
+
background-position: 0% 50%;
|
306 |
+
}
|
307 |
+
50% {
|
308 |
+
background-position: 100% 50%;
|
309 |
+
}
|
310 |
+
100% {
|
311 |
+
background-position: 0% 50%;
|
312 |
+
}
|
313 |
+
}
|
314 |
+
.mood {
|
315 |
+
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
|
316 |
+
background-size: 400% 400%;
|
317 |
+
animation: gradient 15s ease infinite;
|
318 |
+
height: 100vh;
|
319 |
+
}
|
320 |
+
|
321 |
+
@keyframes gradient {
|
322 |
+
0% {
|
323 |
+
background-position: 0% 50%;
|
324 |
+
}
|
325 |
+
50% {
|
326 |
+
background-position: 100% 50%;
|
327 |
+
}
|
328 |
+
100% {
|
329 |
+
background-position: 0% 50%;
|
330 |
+
}
|
331 |
+
}
|
332 |
+
.OpenAI {
|
333 |
+
background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab)!important;
|
334 |
+
animation: gradient 15s ease infinite!important;
|
335 |
+
}
|
336 |
+
|
337 |
+
@keyframes gradient {
|
338 |
+
0% {
|
339 |
+
background-position: 0% 50%;
|
340 |
+
}
|
341 |
+
50% {
|
342 |
+
background-position: 100% 50%;
|
343 |
+
}
|
344 |
+
100% {
|
345 |
+
background-position: 0% 50%;
|
346 |
+
}
|
347 |
+
}
|
348 |
+
}
|
349 |
+
"""
|
350 |
+
|
351 |
+
def check_box(check,ch):
|
352 |
+
if(check==True):
|
353 |
+
return gr.Dropdown.update(visible=True)
|
354 |
+
return gr.Dropdown.update(visible=False, value=None)
|
355 |
+
|
356 |
+
|
357 |
+
with gr.Blocks(css=css1,theme='freddyaboulton/dracula_revamped',title="🎵MoodMelody🤖 by Neelanjan",thumbnail="MelodyGen/Mood-Melody.jpg") as demo:
|
358 |
+
with gr.Row():
|
359 |
+
with gr.Column(scale=1):
|
360 |
+
Mood=textbox = gr.Textbox(label="Enter Your Mood",value="Happy",placeholder="How is Your Mood Now?", elem_id="warning", elem_classes="feedback")
|
361 |
+
Key=textbox = gr.Textbox(label="Enter Open AI Key",placeholder="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",elem_id="warning", elem_classes="feedback")
|
362 |
+
Key2=textbox = gr.Textbox(label="Enter Replicate Key",placeholder="r8-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",elem_id="warning", elem_classes="feedback")
|
363 |
+
check=gr.Checkbox(label="Custom Genre?", info="Select if you want custom Genres",value=True,elem_id="warning", elem_classes="feedback")
|
364 |
+
ch=gr.Dropdown(
|
365 |
+
[
|
366 |
+
"Acoustic","Alternative","Ambient","Blues","Classical","Country","Dance","Disco","Electronic","Folk","Funk","Gospel","Hip Hop","Indie","Jazz","Latin","Metal","Opera","Pop","Punk",
|
367 |
+
"R&B","Reggae","Rock","Soul","Techno","World",# Add more genres as needed
|
368 |
+
],max_choices=1,
|
369 |
+
multiselect=True,visible=True,
|
370 |
+
label="Music Genres",
|
371 |
+
info="Select only one genre of music"
|
372 |
+
)
|
373 |
+
|
374 |
+
check.change(check_box, check, ch)
|
375 |
+
btn = gr.Button(value="Generate Song")
|
376 |
+
with gr.Column(scale=4):
|
377 |
+
SongName=gr.Textbox(label="Song Name",placeholder="Song Name will appear Here")
|
378 |
+
Genres=gr.Textbox(label="Genre",placeholder="Genres")
|
379 |
+
AlbumArt=gr.Image(label="AI Generated Album Art")
|
380 |
+
MusicGen=gr.Audio(label="AI Generated Music",format="wav")
|
381 |
+
|
382 |
+
|
383 |
+
|
384 |
+
btn.click(main_func, inputs=[Mood,Key,Key2,ch], outputs=[AlbumArt,SongName,Genres,MusicGen])
|
385 |
+
|
386 |
+
if __name__ == "__main__":
|
387 |
+
demo.launch(share=True,show_error=True,debug=True)
|