Spaces:
Sleeping
Sleeping
Download app.py from ilsa15/Book-Cover-Generator: direct link, hf CLI and curl.
- Browser
- Download file 1.38 kB
-
https://huggingface.co/spaces/ilsa15/Book-Cover-Generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/ilsa15/Book-Cover-Generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ilsa15/Book-Cover-Generator/resolve/main/app.py
1.38 kB
| import streamlit as st | |
| from diffusers import StableDiffusionPipeline | |
| import torch | |
| from PIL import Image | |
| import io | |
| st.set_page_config(page_title="AI Book Cover Designer", layout="centered") | |
| st.title("๐ AI Book Cover Designer") | |
| def load_model(): | |
| model_id = "stabilityai/stable-diffusion-2" | |
| pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16) | |
| pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu") | |
| return pipe | |
| pipe = load_model() | |
| title = st.text_input("Book Title", "The Last Moonwalker") | |
| desc = st.text_area("Short Description", "An astronaut trapped on the moon battles loneliness and discovers a strange portal.") | |
| genre = st.selectbox("Genre", ["Fantasy", "Romance", "Sci-Fi", "Horror", "Mystery", "Adventure", "Historical Fiction"]) | |
| if st.button("๐จ Generate Cover"): | |
| with st.spinner("Generating your beautiful book cover..."): | |
| prompt = f"A book cover for a {genre} novel titled '{title}': {desc}" | |
| image = pipe(prompt, num_inference_steps=25).images[0] | |
| st.image(image, caption="Your AI-generated book cover", use_column_width=True) | |
| # Save button | |
| img_bytes = io.BytesIO() | |
| image.save(img_bytes, format='PNG') | |
| st.download_button(label="๐ฅ Download Cover", data=img_bytes.getvalue(), file_name="book_cover.png", mime="image/png") | |