Upload 2 files
Browse files- pages/Image_Generator.py +109 -0
- pages/Music_Generator.py +108 -0
pages/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("---")
|
pages/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("---")
|