Spaces:
Sleeping
Sleeping
updated
Browse files
app.py
CHANGED
|
@@ -3,27 +3,86 @@ from PIL import Image
|
|
| 3 |
import io
|
| 4 |
from utils import query_hf_api
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
st.
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Input prompt
|
| 11 |
-
prompt = st.text_input(
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Generate button
|
| 14 |
-
if st.button("Generate Image"):
|
| 15 |
if prompt.strip():
|
| 16 |
-
with st.spinner("Generating
|
| 17 |
try:
|
| 18 |
# Query the Hugging Face API
|
| 19 |
image_bytes = query_hf_api(prompt)
|
| 20 |
-
image = Image.open(io.BytesIO(image_bytes))
|
| 21 |
|
| 22 |
# Display the generated image
|
| 23 |
-
st.image(image, caption="Generated Image", use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
-
st.error(f"Error: {str(e)}")
|
| 26 |
else:
|
| 27 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
st.caption("Made with ❤️ by Sumit Yadav. Credits to 🤗 Spaces for Hosting this. ")
|
|
|
|
| 3 |
import io
|
| 4 |
from utils import query_hf_api
|
| 5 |
|
| 6 |
+
# Set page configuration
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="AI Image Generator",
|
| 9 |
+
page_icon="🎨",
|
| 10 |
+
layout="centered",
|
| 11 |
+
initial_sidebar_state="collapsed",
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# App Header
|
| 15 |
+
st.title("🎨 AI Image Generator")
|
| 16 |
+
st.markdown(
|
| 17 |
+
"""
|
| 18 |
+
<style>
|
| 19 |
+
.main-title {
|
| 20 |
+
font-size: 2.5rem;
|
| 21 |
+
color: #4CAF50;
|
| 22 |
+
font-weight: bold;
|
| 23 |
+
text-align: center;
|
| 24 |
+
}
|
| 25 |
+
.sub-title {
|
| 26 |
+
font-size: 1.2rem;
|
| 27 |
+
color: #555;
|
| 28 |
+
text-align: center;
|
| 29 |
+
margin-bottom: 20px;
|
| 30 |
+
}
|
| 31 |
+
.footer {
|
| 32 |
+
font-size: 0.9rem;
|
| 33 |
+
color: #888;
|
| 34 |
+
text-align: center;
|
| 35 |
+
margin-top: 50px;
|
| 36 |
+
}
|
| 37 |
+
</style>
|
| 38 |
+
""",
|
| 39 |
+
unsafe_allow_html=True,
|
| 40 |
+
)
|
| 41 |
+
st.markdown('<h1 class="main-title">AI-Powered Image Generator</h1>', unsafe_allow_html=True)
|
| 42 |
+
st.markdown('<h3 class="sub-title">Generate images from your imagination!</h3>', unsafe_allow_html=True)
|
| 43 |
|
| 44 |
# Input prompt
|
| 45 |
+
prompt = st.text_input(
|
| 46 |
+
"Enter your image prompt:",
|
| 47 |
+
placeholder="e.g., 'Astronaut riding a bike on Mars at sunset'",
|
| 48 |
+
)
|
| 49 |
|
| 50 |
# Generate button
|
| 51 |
+
if st.button("✨ Generate Image"):
|
| 52 |
if prompt.strip():
|
| 53 |
+
with st.spinner("🖌️ Generating your masterpiece... Please wait!"):
|
| 54 |
try:
|
| 55 |
# Query the Hugging Face API
|
| 56 |
image_bytes = query_hf_api(prompt)
|
| 57 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB") # Ensure it's RGB for JPEG
|
| 58 |
|
| 59 |
# Display the generated image
|
| 60 |
+
st.image(image, caption="Generated Image", use_container_width=True)
|
| 61 |
+
|
| 62 |
+
# Save the image as JPEG to a BytesIO buffer
|
| 63 |
+
buffer = io.BytesIO()
|
| 64 |
+
image.save(buffer, format="JPEG", quality=95)
|
| 65 |
+
buffer.seek(0)
|
| 66 |
+
|
| 67 |
+
# Add a download button
|
| 68 |
+
st.download_button(
|
| 69 |
+
label="📥 Download Image",
|
| 70 |
+
data=buffer,
|
| 71 |
+
file_name="generated_image.jpg",
|
| 72 |
+
mime="image/jpeg",
|
| 73 |
+
)
|
| 74 |
except Exception as e:
|
| 75 |
+
st.error(f"🚨 Error: {str(e)}")
|
| 76 |
else:
|
| 77 |
+
st.warning("Please enter a prompt to generate an image.")
|
| 78 |
+
|
| 79 |
+
# Footer
|
| 80 |
+
st.markdown(
|
| 81 |
+
"""
|
| 82 |
+
<div class="footer">
|
| 83 |
+
Made with ❤️ by <b>Sumit Yadav</b>. Credits to 🤗 Spaces for hosting this app.
|
| 84 |
+
</div>
|
| 85 |
+
""",
|
| 86 |
+
unsafe_allow_html=True,
|
| 87 |
+
)
|
| 88 |
|
|
|