Bagda commited on
Commit
ad2b604
Β·
verified Β·
1 Parent(s): 1718733

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -23
app.py CHANGED
@@ -2,8 +2,9 @@ import streamlit as st
2
  from diffusers import StableDiffusionPipeline
3
  import torch
4
  from PIL import Image
 
5
 
6
- # Load model (cached so it doesn't reload every time)
7
  @st.cache_resource
8
  def load_model():
9
  model_id = "runwayml/stable-diffusion-v1-5"
@@ -15,30 +16,21 @@ def load_model():
15
  pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
16
  return pipe
17
 
18
- # Page config
19
- st.set_page_config(page_title="AI Thumbnail Generator", layout="centered")
20
- st.title("πŸ–ΌοΈ AI Thumbnail Generator (Stable Diffusion)")
21
 
22
- # Load model
23
  pipe = load_model()
24
 
25
- # Prompt input
26
- prompt = st.text_input("πŸ“ Enter Thumbnail Prompt:", value="Minecraft NOOB vs PRO | fire background | bold text")
27
 
28
- # Generate button
29
- if st.button("🎨 Generate Thumbnail"):
30
- with st.spinner("Generating image... Please wait"):
31
- result = pipe(prompt, guidance_scale=7.5)
32
- image = result.images[0]
33
- st.image(image, caption="πŸŽ‰ Your AI Thumbnail", use_column_width=True)
34
 
35
- # Download option
36
- st.download_button("πŸ“₯ Download Image", image_to_bytes(image), file_name="thumbnail.jpg", mime="image/jpeg")
37
-
38
- # Utility function
39
- def image_to_bytes(img: Image.Image):
40
- from io import BytesIO
41
- buf = BytesIO()
42
- img.save(buf, format="JPEG")
43
- byte_im = buf.getvalue()
44
- return byte_im
 
2
  from diffusers import StableDiffusionPipeline
3
  import torch
4
  from PIL import Image
5
+ from io import BytesIO
6
 
7
+ # Load the model only once
8
  @st.cache_resource
9
  def load_model():
10
  model_id = "runwayml/stable-diffusion-v1-5"
 
16
  pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
17
  return pipe
18
 
19
+ # Streamlit UI Code
20
+ st.set_page_config(page_title="Thumbnail Generator", layout="centered")
21
+ st.title("πŸ–ΌοΈ AI Thumbnail Generator")
22
 
 
23
  pipe = load_model()
24
 
25
+ prompt = st.text_input("πŸ“₯ Enter your prompt:", value="Minecraft NOOB vs PRO | bold text | fire background")
 
26
 
27
+ if st.button("πŸš€ Generate"):
28
+ with st.spinner("Generating thumbnail..."):
29
+ image = pipe(prompt, guidance_scale=7.5).images[0]
30
+ st.image(image, caption="Generated Thumbnail", use_column_width=True)
 
 
31
 
32
+ # Download button
33
+ buf = BytesIO()
34
+ image.save(buf, format="JPEG")
35
+ byte_im = buf.getvalue()
36
+ st.download_button("Download Thumbnail", byte_im, file_name="thumbnail.jpg", mime="image/jpeg")