Mavhas commited on
Commit
fef886b
·
verified ·
1 Parent(s): 688a1d9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from diffusers import StableDiffusionPipeline
3
+ from PIL import Image
4
+ import torch
5
+ import io
6
+ import base64
7
+
8
+ # Model loading (cached)
9
+ @st.cache_resource
10
+ def load_model():
11
+ pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16).to("cuda" if torch.cuda.is_available() else "cpu")
12
+ return pipe
13
+
14
+ pipe = load_model()
15
+
16
+ st.title("AI Image Generator")
17
+
18
+ prompt = st.text_area("Enter your prompt:", height=150)
19
+ num_images = st.slider("Number of Images", 1, 4, 1)
20
+
21
+ if st.button("Generate"):
22
+ if prompt:
23
+ with st.spinner("Generating images..."):
24
+ images = pipe(prompt, num_images=num_images).images
25
+
26
+ for i, image in enumerate(images):
27
+ col1, col2 = st.columns([1, 2])
28
+
29
+ with col1:
30
+ st.image(image, caption=f"Image {i+1}")
31
+
32
+ with col2:
33
+ buffered = io.BytesIO()
34
+ image.save(buffered, format="PNG")
35
+ img_str = base64.b64encode(buffered.getvalue()).decode()
36
+ href = f'<a href="data:image/png;base64,{img_str}" download="image_{i+1}.png">Download Image {i+1}</a>'
37
+ st.markdown(href, unsafe_allow_html=True)
38
+
39
+ else:
40
+ st.warning("Please enter a prompt.")
41
+
42
+ # Styling (optional)
43
+ st.markdown("""
44
+ <style>
45
+ body {
46
+ font-family: sans-serif;
47
+ }
48
+ .stButton>button {
49
+ background-color: #4CAF50;
50
+ border: none;
51
+ color: white;
52
+ padding: 10px 20px;
53
+ text-align: center;
54
+ text-decoration: none;
55
+ display: inline-block;
56
+ font-size: 16px;
57
+ margin: 4px 2px;
58
+ cursor: pointer;
59
+ border-radius: 5px;
60
+ }
61
+ </style>
62
+ """, unsafe_allow_html=True)