HarshRathi09
commited on
Commit
β’
ae49b72
1
Parent(s):
b5b0696
Upload 4 files
Browse files- Image_Generator.py +109 -0
- Music_Generator.py +108 -0
- home.py +68 -0
- musicgen.py +33 -0
Image_Generator.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from diffusers import DiffusionPipeline
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
# Custom CSS to improve the appearance
|
6 |
+
st.markdown("""
|
7 |
+
<style>
|
8 |
+
.stApp {
|
9 |
+
background-image:linear-gradient(to bottom, #000000 ,#000814 ,#001d3d, #003566);
|
10 |
+
}
|
11 |
+
.main-title {
|
12 |
+
font-size: 3rem !important;
|
13 |
+
color: #f1faee;
|
14 |
+
text-align: center;
|
15 |
+
padding: 1rem 0;
|
16 |
+
}
|
17 |
+
.subtitle {
|
18 |
+
font-size: 1.2rem;
|
19 |
+
color: #f1faee;
|
20 |
+
text-align: center;
|
21 |
+
margin-bottom: 2rem;
|
22 |
+
}
|
23 |
+
.stTextInput > div > div > input {
|
24 |
+
font-size: 1.2rem;
|
25 |
+
}
|
26 |
+
.generate-button {
|
27 |
+
font-size: 1.2rem;
|
28 |
+
border-radius: 10px;
|
29 |
+
padding: 0.5rem 1rem;
|
30 |
+
}
|
31 |
+
.info-section {
|
32 |
+
background-color: #ffffff;
|
33 |
+
padding: 1rem;
|
34 |
+
border-radius: 10px;
|
35 |
+
margin-top: 2rem;
|
36 |
+
}
|
37 |
+
</style>
|
38 |
+
""", unsafe_allow_html=True)
|
39 |
+
|
40 |
+
# Initialize the Stable Diffusion pipeline
|
41 |
+
@st.cache_resource
|
42 |
+
def load_image_model():
|
43 |
+
# Ensure that you are using the correct device (GPU if available)
|
44 |
+
pipe = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
|
45 |
+
if torch.cuda.is_available():
|
46 |
+
pipe.to("cuda")
|
47 |
+
return pipe
|
48 |
+
|
49 |
+
pipe = load_image_model()
|
50 |
+
|
51 |
+
# Streamlit interface
|
52 |
+
st.markdown("<h1 class='main-title'>πΌοΈ AI Image Generator</h1>", unsafe_allow_html=True)
|
53 |
+
st.markdown("<p class='subtitle'>Describe the image you want to create, and the AI will generate it for you.</p>", unsafe_allow_html=True)
|
54 |
+
|
55 |
+
# Main interaction area
|
56 |
+
image_description = st.text_input("ποΈ Describe the image you want to create",
|
57 |
+
placeholder="E.g., A sunset over a mountain range")
|
58 |
+
|
59 |
+
if st.button("π¨ Generate Image"):
|
60 |
+
if image_description.strip():
|
61 |
+
with st.spinner("π¨ Generating your image..."):
|
62 |
+
st.warning("Image generation may take some time. Please be patient.") # Display warning message
|
63 |
+
try:
|
64 |
+
# Generate the image
|
65 |
+
image = pipe(image_description).images[0]
|
66 |
+
|
67 |
+
# Display the generated image
|
68 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
69 |
+
|
70 |
+
# Provide download button for the generated image
|
71 |
+
img_path = "generated_image.png"
|
72 |
+
image.save(img_path)
|
73 |
+
|
74 |
+
with open(img_path, "rb") as file:
|
75 |
+
st.download_button(
|
76 |
+
label="π₯ Download Image",
|
77 |
+
data=file,
|
78 |
+
file_name="generated_image.png",
|
79 |
+
mime="image/png"
|
80 |
+
)
|
81 |
+
except Exception as e:
|
82 |
+
st.error(f"π Oops! An error occurred: {str(e)}")
|
83 |
+
else:
|
84 |
+
st.warning("π€ Please enter a description for your image.")
|
85 |
+
|
86 |
+
# Information sections
|
87 |
+
st.markdown("---")
|
88 |
+
|
89 |
+
col1, col2 = st.columns(2)
|
90 |
+
|
91 |
+
with col1:
|
92 |
+
st.markdown("### π How it works")
|
93 |
+
st.markdown("""
|
94 |
+
1. π Describe the image you want
|
95 |
+
2. π±οΈ Click 'Generate Image'
|
96 |
+
3. π¨ View your AI-generated artwork
|
97 |
+
4. π₯ Download and share!
|
98 |
+
""")
|
99 |
+
|
100 |
+
with col2:
|
101 |
+
st.markdown("### π¨ Tips for great results")
|
102 |
+
st.markdown("""
|
103 |
+
- Be specific about objects and settings
|
104 |
+
- Mention style, genre, or mood
|
105 |
+
- Describe the colors and lighting
|
106 |
+
""")
|
107 |
+
|
108 |
+
# Footer
|
109 |
+
st.markdown("---")
|
Music_Generator.py
ADDED
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from musicgen import init_musicgen, generate_music
|
3 |
+
import numpy as np
|
4 |
+
from scipy.io.wavfile import write
|
5 |
+
|
6 |
+
# Custom CSS to improve the appearance
|
7 |
+
st.markdown("""
|
8 |
+
<style>
|
9 |
+
.stApp {
|
10 |
+
background-image:linear-gradient(to bottom, #000000 ,#000814 ,#10002b, #240046);
|
11 |
+
}
|
12 |
+
.main-title {
|
13 |
+
font-size: 3rem !important;
|
14 |
+
color: #f1faee;
|
15 |
+
text-align: center;
|
16 |
+
padding: 1rem 0;
|
17 |
+
}
|
18 |
+
.subtitle {
|
19 |
+
font-size: 1.2rem;
|
20 |
+
color: #f1faee;
|
21 |
+
text-align: center;
|
22 |
+
margin-bottom: 2rem;
|
23 |
+
}
|
24 |
+
.stTextInput > div > div > input {
|
25 |
+
font-size: 1.2rem;
|
26 |
+
}
|
27 |
+
.generate-button {
|
28 |
+
font-size: 1.2rem;
|
29 |
+
border-radius: 10px;
|
30 |
+
padding: 0.5rem 1rem;
|
31 |
+
}
|
32 |
+
.info-section {
|
33 |
+
background-color: #ffffff;
|
34 |
+
padding: 1rem;
|
35 |
+
border-radius: 10px;
|
36 |
+
margin-top: 2rem;
|
37 |
+
}
|
38 |
+
</style>
|
39 |
+
""", unsafe_allow_html=True)
|
40 |
+
|
41 |
+
# Initialize the MusicGen model and tokenizer
|
42 |
+
@st.cache_resource
|
43 |
+
def load_model():
|
44 |
+
return init_musicgen('facebook/musicgen-small')
|
45 |
+
|
46 |
+
tokenizer, model = load_model()
|
47 |
+
|
48 |
+
# Streamlit interface
|
49 |
+
st.markdown("<h1 class='main-title'>π΅ AI Music Generator</h1>", unsafe_allow_html=True)
|
50 |
+
st.markdown("<p class='subtitle'>Describe the type of music you want, and the AI will generate it for you.</p>", unsafe_allow_html=True)
|
51 |
+
|
52 |
+
# Main interaction area
|
53 |
+
text_input = st.text_input("ποΈ Describe the music you want to create",
|
54 |
+
placeholder="E.g., A happy pop song with guitar and drums")
|
55 |
+
|
56 |
+
if st.button("πΌ Generate Music"):
|
57 |
+
if text_input.strip():
|
58 |
+
with st.spinner("π§ Generating your music..."):
|
59 |
+
try:
|
60 |
+
audio_array = generate_music(text_input, tokenizer, model)
|
61 |
+
|
62 |
+
# Convert to int16 and save as WAV file
|
63 |
+
audio_int16 = (audio_array * 32767).astype(np.int16)
|
64 |
+
write("generated_music.wav", 44100, audio_int16)
|
65 |
+
|
66 |
+
st.success("π Your music has been generated successfully!")
|
67 |
+
|
68 |
+
# Play the generated audio
|
69 |
+
st.audio("generated_music.wav")
|
70 |
+
|
71 |
+
# Provide download button
|
72 |
+
with open("generated_music.wav", "rb") as file:
|
73 |
+
st.download_button(
|
74 |
+
label="π₯ Download Music",
|
75 |
+
data=file,
|
76 |
+
file_name="ai_generated_music.wav",
|
77 |
+
mime="audio/wav"
|
78 |
+
)
|
79 |
+
except Exception as e:
|
80 |
+
st.error(f"π Oops! An error occurred: {str(e)}")
|
81 |
+
else:
|
82 |
+
st.warning("π€ Please enter a description for your music.")
|
83 |
+
|
84 |
+
# Information sections
|
85 |
+
st.markdown("---")
|
86 |
+
|
87 |
+
col1, col2 = st.columns(2)
|
88 |
+
|
89 |
+
with col1:
|
90 |
+
st.markdown("### π How it works")
|
91 |
+
st.markdown("""
|
92 |
+
1. π Describe the music you want
|
93 |
+
2. π±οΈ Click 'Generate Music'
|
94 |
+
3. π§ Listen to your AI-created tune
|
95 |
+
4. π₯ Download and share!
|
96 |
+
""")
|
97 |
+
|
98 |
+
with col2:
|
99 |
+
st.markdown("### π¨ Tips for great results")
|
100 |
+
st.markdown("""
|
101 |
+
- Be specific about instruments
|
102 |
+
- Mention genre or mood
|
103 |
+
- Describe tempo or rhythm
|
104 |
+
- Reference famous artists or songs
|
105 |
+
""")
|
106 |
+
|
107 |
+
# Footer
|
108 |
+
st.markdown("---")
|
home.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
# Set page configuration (only once in main.py)
|
4 |
+
st.set_page_config(page_title="AI Generator App", page_icon="π΅", layout="wide")
|
5 |
+
|
6 |
+
# Custom CSS to improve the appearance
|
7 |
+
st.markdown("""
|
8 |
+
<style>
|
9 |
+
.stApp {
|
10 |
+
background-image:linear-gradient(to bottom, #000000 , #0a0908, #250902, #641220);
|
11 |
+
font-family: 'Helvetica', sans-serif;
|
12 |
+
}
|
13 |
+
.main-title {
|
14 |
+
font-size: 3.5rem !important;
|
15 |
+
color: #f1faee;
|
16 |
+
text-align: center;
|
17 |
+
margin-bottom: 0.5rem;
|
18 |
+
font-weight: bold;
|
19 |
+
}
|
20 |
+
.subtitle {
|
21 |
+
font-size: 1.5rem;
|
22 |
+
color: #f1faee;
|
23 |
+
text-align: center;
|
24 |
+
margin-bottom: 3rem;
|
25 |
+
}
|
26 |
+
.stButton button {
|
27 |
+
background-color: #4CAF50;
|
28 |
+
color: white;
|
29 |
+
padding: 15px 32px;
|
30 |
+
font-size: 1.2rem;
|
31 |
+
margin: 10px;
|
32 |
+
border-radius: 12px;
|
33 |
+
border: none;
|
34 |
+
cursor: pointer;
|
35 |
+
transition: background-color 0.3s ease;
|
36 |
+
}
|
37 |
+
.stButton button:hover {
|
38 |
+
background-color: #45a049;
|
39 |
+
}
|
40 |
+
.section-title {
|
41 |
+
color: #fff;
|
42 |
+
font-size: 1.5rem;
|
43 |
+
margin-bottom: 1rem;
|
44 |
+
}
|
45 |
+
.section-description {
|
46 |
+
color: #ddd;
|
47 |
+
margin-bottom: 1.5rem;
|
48 |
+
}
|
49 |
+
</style>
|
50 |
+
""", unsafe_allow_html=True)
|
51 |
+
|
52 |
+
# Main page content
|
53 |
+
st.markdown("<h1 class='main-title'>π¨ AI Music and Image Generator</h1>", unsafe_allow_html=True)
|
54 |
+
st.markdown("<p class='subtitle'>Welcome to the AI Generator app! Use the navigation menu to switch between generating music or images based on text prompts.</p>", unsafe_allow_html=True)
|
55 |
+
|
56 |
+
# Brief description of each section
|
57 |
+
st.markdown("### Available Sections:")
|
58 |
+
col1, col2 = st.columns(2)
|
59 |
+
|
60 |
+
with col1:
|
61 |
+
st.markdown("<div class='section-title'>π΅ Music Generator</div>", unsafe_allow_html=True)
|
62 |
+
st.markdown("<div class='section-description'>Transform your ideas into music using AI.</div>", unsafe_allow_html=True)
|
63 |
+
|
64 |
+
|
65 |
+
with col2:
|
66 |
+
st.markdown("<div class='section-title'>πΌοΈ Image Generator</div>", unsafe_allow_html=True)
|
67 |
+
st.markdown("<div class='section-description'>Generate stunning images from your text descriptions.</div>", unsafe_allow_html=True)
|
68 |
+
|
musicgen.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForTextToWaveform
|
2 |
+
import torch
|
3 |
+
|
4 |
+
def init_musicgen(model_size='facebook/musicgen-small'):
|
5 |
+
"""
|
6 |
+
Initialize the MusicGen model and tokenizer.
|
7 |
+
"""
|
8 |
+
try:
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_size)
|
10 |
+
model = AutoModelForTextToWaveform.from_pretrained(model_size)
|
11 |
+
return tokenizer, model
|
12 |
+
except Exception as e:
|
13 |
+
print(f"Error loading model or tokenizer: {str(e)}")
|
14 |
+
raise e
|
15 |
+
|
16 |
+
def generate_music(text_prompt, tokenizer, model):
|
17 |
+
"""
|
18 |
+
Generate music based on the input text prompt using the pre-initialized model.
|
19 |
+
"""
|
20 |
+
print(f"Generating music for prompt: {text_prompt}")
|
21 |
+
|
22 |
+
# Tokenize the input text
|
23 |
+
inputs = tokenizer(text_prompt, return_tensors='pt')
|
24 |
+
|
25 |
+
# Check if inputs are valid
|
26 |
+
if 'input_ids' not in inputs or inputs['input_ids'] is None or inputs['input_ids'].size(0) == 0:
|
27 |
+
raise ValueError("Tokenized inputs are empty or invalid. Ensure your input prompt is valid.")
|
28 |
+
|
29 |
+
# Generate the music waveform
|
30 |
+
with torch.no_grad():
|
31 |
+
generated_waveform = model.generate(**inputs)
|
32 |
+
|
33 |
+
return generated_waveform.cpu().numpy().squeeze()
|