ilsa15 commited on
Commit
80b2ebe
·
verified ·
1 Parent(s): efb2e46

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from diffusers import StableDiffusionPipeline
3
+ import torch
4
+ from PIL import Image
5
+ import io
6
+
7
+ st.set_page_config(page_title="AI Book Cover Designer", layout="centered")
8
+ st.title("📚 AI Book Cover Designer")
9
+
10
+ @st.cache_resource
11
+ def load_model():
12
+ model_id = "stabilityai/stable-diffusion-2"
13
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
14
+ pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
15
+ return pipe
16
+
17
+ pipe = load_model()
18
+
19
+ title = st.text_input("Book Title", "The Last Moonwalker")
20
+ desc = st.text_area("Short Description", "An astronaut trapped on the moon battles loneliness and discovers a strange portal.")
21
+ genre = st.selectbox("Genre", ["Fantasy", "Romance", "Sci-Fi", "Horror", "Mystery", "Adventure", "Historical Fiction"])
22
+
23
+ if st.button("🎨 Generate Cover"):
24
+ with st.spinner("Generating your beautiful book cover..."):
25
+ prompt = f"A book cover for a {genre} novel titled '{title}': {desc}"
26
+ image = pipe(prompt, num_inference_steps=25).images[0]
27
+ st.image(image, caption="Your AI-generated book cover", use_column_width=True)
28
+
29
+ # Save button
30
+ img_bytes = io.BytesIO()
31
+ image.save(img_bytes, format='PNG')
32
+ st.download_button(label="📥 Download Cover", data=img_bytes.getvalue(), file_name="book_cover.png", mime="image/png")