Murtaza249 commited on
Commit
1777c49
·
verified ·
1 Parent(s): 56eb993

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +189 -0
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline
4
+ from PIL import Image
5
+ import io
6
+ import base64
7
+
8
+ # Page configuration
9
+ st.set_page_config(
10
+ page_title="Text to Image Generator",
11
+ page_icon="🎨",
12
+ layout="wide",
13
+ )
14
+
15
+ # Custom CSS for better UI
16
+ st.markdown("""
17
+ <style>
18
+ .main {
19
+ background-color: #f5f7f9;
20
+ }
21
+ .stApp {
22
+ max-width: 1200px;
23
+ margin: 0 auto;
24
+ }
25
+ .css-18e3th9 {
26
+ padding-top: 2rem;
27
+ }
28
+ .css-1d391kg {
29
+ padding: 3rem 1rem;
30
+ }
31
+ .stButton>button {
32
+ background-color: #4CAF50;
33
+ color: white;
34
+ padding: 10px 24px;
35
+ border: none;
36
+ border-radius: 4px;
37
+ cursor: pointer;
38
+ font-size: 16px;
39
+ transition: all 0.3s;
40
+ }
41
+ .stButton>button:hover {
42
+ background-color: #45a049;
43
+ box-shadow: 0 4px 8px rgba(0,0,0,0.1);
44
+ }
45
+ .download-link {
46
+ display: inline-block;
47
+ margin-top: 10px;
48
+ padding: 10px 15px;
49
+ background-color: #3498db;
50
+ color: white;
51
+ text-decoration: none;
52
+ border-radius: 4px;
53
+ text-align: center;
54
+ }
55
+ h1 {
56
+ color: #2C3E50;
57
+ margin-bottom: 30px;
58
+ }
59
+ .stSlider {
60
+ padding-top: 1rem;
61
+ padding-bottom: 1rem;
62
+ }
63
+ </style>
64
+ """, unsafe_allow_html=True)
65
+
66
+ # Header
67
+ st.title("✨ Text to Image Generator")
68
+ st.markdown("Turn your imagination into images using AI! 🖼️")
69
+
70
+ @st.cache_resource
71
+ def load_model():
72
+ model_id = "runwayml/stable-diffusion-v1-5"
73
+ pipe = StableDiffusionPipeline.from_pretrained(
74
+ model_id,
75
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
76
+ )
77
+ if torch.cuda.is_available():
78
+ pipe = pipe.to("cuda")
79
+ return pipe
80
+
81
+ # Create columns for the layout
82
+ col1, col2 = st.columns([1, 1])
83
+
84
+ with col1:
85
+ # Text input for the prompt
86
+ prompt = st.text_area(
87
+ "Enter your prompt",
88
+ placeholder="A beautiful sunset over the ocean with palm trees in the foreground",
89
+ height=100
90
+ )
91
+
92
+ # Advanced options in an expander
93
+ with st.expander("Advanced Options"):
94
+ num_inference_steps = st.slider(
95
+ "Quality (Steps)",
96
+ min_value=1,
97
+ max_value=100,
98
+ value=50,
99
+ help="Higher values produce more detailed images but take longer"
100
+ )
101
+
102
+ guidance_scale = st.slider(
103
+ "Creativity",
104
+ min_value=1.0,
105
+ max_value=20.0,
106
+ value=7.5,
107
+ step=0.5,
108
+ help="Higher values make the image more closely follow the prompt"
109
+ )
110
+
111
+ seed = st.number_input(
112
+ "Random Seed",
113
+ min_value=-1,
114
+ max_value=2147483647,
115
+ value=-1,
116
+ help="Set a specific seed for reproducible results, or -1 for random"
117
+ )
118
+
119
+ image_width = st.select_slider(
120
+ "Image Width",
121
+ options=[512, 576, 640, 704, 768],
122
+ value=512
123
+ )
124
+
125
+ image_height = st.select_slider(
126
+ "Image Height",
127
+ options=[512, 576, 640, 704, 768],
128
+ value=512
129
+ )
130
+
131
+ # Generate button
132
+ generate_button = st.button("Generate Image")
133
+
134
+ # Function to get a download link
135
+ def get_image_download_link(img, filename="generated_image.png", text="Download Image"):
136
+ buffered = io.BytesIO()
137
+ img.save(buffered, format="PNG")
138
+ img_str = base64.b64encode(buffered.getvalue()).decode()
139
+ href = f'<a href="data:image/png;base64,{img_str}" download="{filename}" class="download-link">{text}</a>'
140
+ return href
141
+
142
+ with col2:
143
+ # Display the generated image and provide a download link
144
+ st.markdown("### Generated Image")
145
+
146
+ result_container = st.empty()
147
+
148
+ if generate_button and prompt:
149
+ try:
150
+ with st.spinner("Generating your image..."):
151
+ # Load the model
152
+ pipe = load_model()
153
+
154
+ # Set the seed for reproducibility
155
+ generator = None
156
+ if seed != -1:
157
+ generator = torch.Generator().manual_seed(seed)
158
+
159
+ # Generate the image
160
+ image = pipe(
161
+ prompt=prompt,
162
+ num_inference_steps=num_inference_steps,
163
+ guidance_scale=guidance_scale,
164
+ generator=generator,
165
+ width=image_width,
166
+ height=image_height
167
+ ).images[0]
168
+
169
+ # Display the image
170
+ result_container.image(image, caption=prompt, use_column_width=True)
171
+
172
+ # Provide a download link
173
+ st.markdown(get_image_download_link(image), unsafe_allow_html=True)
174
+
175
+ # Display the parameters used
176
+ st.markdown("### Parameters Used")
177
+ st.text(f"Steps: {num_inference_steps}")
178
+ st.text(f"Guidance Scale: {guidance_scale}")
179
+ st.text(f"Seed: {seed}")
180
+ st.text(f"Dimensions: {image_width}x{image_height}")
181
+
182
+ except Exception as e:
183
+ result_container.error(f"An error occurred: {str(e)}")
184
+ else:
185
+ result_container.markdown("Your image will appear here after generation.")
186
+
187
+ # Add footer
188
+ st.markdown("---")
189
+ st.markdown("Powered by Hugging Face Diffusers and Streamlit")