Tejasva-Maurya commited on
Commit
fe9854b
·
verified ·
1 Parent(s): cbfaa3c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +302 -0
app.py ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_js_eval import streamlit_js_eval
3
+ from PIL import Image
4
+ import io
5
+ import mongo_db
6
+
7
+ # Functions for image and prompt generation
8
+ from image_model import (
9
+ flux,
10
+ stable_diffusion,
11
+ )
12
+ from prompt_model import Qwen_72b, microsoft_phi, Mixtral
13
+
14
+
15
+ # Set page config with a custom title and layout
16
+ st.set_page_config(
17
+ page_title="Welcome to ImagiGen: AI-Powered Image Generation for Automated Fashion Design",
18
+ layout="wide",
19
+ initial_sidebar_state="expanded",
20
+ )
21
+ # Custom CSS for fonts, background, and animations
22
+ st.markdown(
23
+ """
24
+ <style>
25
+ /* Add a custom font */
26
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
27
+ body {
28
+ font-family: 'Poppins', sans-serif;
29
+ background: linear-gradient(135deg, #f8f9fa, #e9ecef);
30
+ animation: gradient-animation 10s ease infinite;
31
+ }
32
+
33
+ /* Header and title styles */
34
+ h1, h2, h3 {
35
+ color: #343a40;
36
+ text-align: center;
37
+ }
38
+
39
+ /* Button styles */
40
+ .stButton button {
41
+ background-color: #007bff;
42
+ color: white;
43
+ border-radius: 8px;
44
+ transition: all 0.3s ease;
45
+ }
46
+ .stButton button:hover {
47
+ background-color: #0056b3;
48
+ transform: scale(1.05);
49
+ }
50
+
51
+ /* Smooth gradient animation */
52
+ @keyframes gradient-animation {
53
+ 0% { background: linear-gradient(135deg, #f8f9fa, #e9ecef); }
54
+ 50% { background: linear-gradient(135deg, #e9ecef, #f8f9fa); }
55
+ 100% { background: linear-gradient(135deg, #f8f9fa, #e9ecef); }
56
+ }
57
+
58
+ /* Custom input box styling */
59
+ input, textarea {
60
+ border: 2px solid #ced4da;
61
+ border-radius: 5px;
62
+ padding: 10px;
63
+ }
64
+
65
+ /* Sidebar customization */
66
+ .sidebar .sidebar-content {
67
+ background-color: #ffffff;
68
+ border-radius: 10px;
69
+ padding: 20px;
70
+ }
71
+ </style>
72
+ """,
73
+ unsafe_allow_html=True,
74
+ )
75
+
76
+ # List of functions
77
+ image_functions = [flux, stable_diffusion]
78
+ prompt_functions = [Qwen_72b, microsoft_phi, Mixtral]
79
+
80
+ # Initialize session state for login status
81
+ if "logged_in" not in st.session_state:
82
+ st.session_state.logged_in = False
83
+
84
+
85
+ def register():
86
+ col1, col2, col3 = st.columns([1, 2, 1]) # Left, Center (main content), Right
87
+ with col2:
88
+ with st.expander("Register New Account", expanded=False):
89
+ st.subheader("Create a New Account", divider="rainbow")
90
+ new_email_id = st.text_input("email_id", placeholder="Enter an Email ID")
91
+ new_password = st.text_input(
92
+ "New Password", type="password", placeholder="Choose a password"
93
+ )
94
+ confirm_password = st.text_input(
95
+ "Confirm Password",
96
+ type="password",
97
+ placeholder="Re-enter your password",
98
+ )
99
+
100
+ if st.button("Register"):
101
+ if new_password != confirm_password:
102
+ st.error("Passwords do not match. Please try again.")
103
+ elif not new_email_id or not new_password:
104
+ st.error("All fields are required.")
105
+ else:
106
+ response = mongo_db.register(new_email_id, new_password)
107
+ print(response)
108
+ if "Invalid Email Id" == response :
109
+ st.error(
110
+ "Invalid Email Id. Please check and try again."
111
+ )
112
+ elif "Email ID already Registered" == response:
113
+ st.error(
114
+ "Email ID already Registered. Please choose a different email id."
115
+ )
116
+ elif "Registration successful" == response:
117
+ st.success("Registration successful! You can now log in.")
118
+ st.balloons()
119
+ else:
120
+ st.error("Unexpected error occur. Please Retry....")
121
+
122
+ # Function to handle login
123
+ def login():
124
+ col1, col2, col3 = st.columns([1, 2, 1]) # Left, Center (main content), Right
125
+ with col2:
126
+ _, centre, _ = st.columns([1, 2, 1])
127
+ with centre:
128
+ st.image("ImagiGen--Logo.svg", use_container_width=True)
129
+ st.header("Login Page", divider="rainbow")
130
+ st.subheader("Please Login to Continue")
131
+ email_id = st.text_input("Email Id", placeholder="Enter your Email Id")
132
+ password = st.text_input(
133
+ "Password", type="password", placeholder="Enter your password"
134
+ )
135
+ if st.button("Login"):
136
+ response = mongo_db.login(email_id, password)
137
+ if response == "Login successful":
138
+ st.session_state.logged_in = True
139
+ st.success("Login successful! Redirecting...")
140
+ st.rerun()
141
+ else:
142
+ st.error("Invalid email id or password")
143
+
144
+
145
+ # Function for the platform/dashboard page
146
+ def platform_page():
147
+ _, centre, _ = st.columns([3, 4, 3])
148
+ with centre:
149
+ st.image("ImagiGen--Logo.svg", use_container_width=True)
150
+ st.header("Welcome to ImagiGen: AI-Powered Image Generation for Automated Fashion Design", divider="rainbow")
151
+ if st.button("Logout"):
152
+ st.session_state.logged_in = False
153
+ st.rerun()
154
+
155
+ st.markdown("### Describe Your Creative Vision")
156
+
157
+ # Initialize session state for prompts
158
+ if "model_1" not in st.session_state:
159
+ st.session_state.model_1 = ""
160
+ if "model_2" not in st.session_state:
161
+ st.session_state.model_2 = ""
162
+ if "model_3" not in st.session_state:
163
+ st.session_state.model_3 = ""
164
+ # User input for the prompt
165
+ user_input = st.text_input(
166
+ "Enter your description for prompt generation:",
167
+ "",
168
+ placeholder="E.g., A vivid image of a flowing red silk dress in a windy desert...",
169
+ )
170
+
171
+ if st.button("Generate Prompt"):
172
+ with st.spinner("Generating Prompts..."):
173
+ st.session_state.model_1 = Qwen_72b(user_input)
174
+ st.session_state.model_2 = microsoft_phi(user_input)
175
+ st.session_state.model_3 = Mixtral(user_input)
176
+
177
+ # Display text areas to edit each generated prompt
178
+ cols = st.columns(3)
179
+ with cols[0]:
180
+ st.session_state.model_1 = st.text_area(
181
+ "Customize Prompt 1 :", st.session_state.model_1, height=200
182
+ )
183
+ with cols[1]:
184
+ st.session_state.model_2 = st.text_area(
185
+ "Customize Prompt 2:", st.session_state.model_2, height=200
186
+ )
187
+ with cols[2]:
188
+ st.session_state.model_3 = st.text_area(
189
+ "Customize Prompt 3:", st.session_state.model_3, height=200
190
+ )
191
+
192
+ if st.button("Generate Images"):
193
+ prompts = [
194
+ st.session_state.model_1,
195
+ st.session_state.model_2,
196
+ st.session_state.model_3,
197
+ ]
198
+ cols = st.columns(3)
199
+ with cols[0]:
200
+ with st.spinner(f"Generating Image {1}..."):
201
+ try:
202
+ image_bytes = image_functions[0](prompts[0])
203
+ image = Image.open(io.BytesIO(image_bytes))
204
+ st.image(
205
+ image,
206
+ caption=f"Prompt {1}, Model {1}",
207
+ use_container_width=True,
208
+ )
209
+ except:
210
+ st.image(
211
+ "error.jpg",
212
+ caption="Server error occur",
213
+ )
214
+ with cols[1]:
215
+ with st.spinner(f"Generating Image {2}..."):
216
+ try:
217
+ image_bytes = image_functions[0](prompts[1])
218
+ image = Image.open(io.BytesIO(image_bytes))
219
+ st.image(
220
+ image,
221
+ caption=f"Prompt {2}, Model {1}",
222
+ use_container_width=True,
223
+ )
224
+ except:
225
+ st.image(
226
+ "error.jpg",
227
+ caption="Server error occur",
228
+ )
229
+ with cols[2]:
230
+ with st.spinner(f"Generating Image {3}..."):
231
+ try:
232
+ image_bytes = image_functions[0](prompts[2])
233
+ image = Image.open(io.BytesIO(image_bytes))
234
+ st.image(
235
+ image,
236
+ caption=f"Prompt {3}, Model {1}",
237
+ use_container_width=True,
238
+ )
239
+ except:
240
+ st.image(
241
+ "error.jpg",
242
+ caption="Server error occur",
243
+ )
244
+ with cols[0]:
245
+ with st.spinner(f"Generating Image {4}..."):
246
+ try:
247
+ image_bytes = image_functions[1](prompts[0])
248
+ image = Image.open(io.BytesIO(image_bytes))
249
+ st.image(
250
+ image,
251
+ caption=f"Prompt {1}, Model {2}",
252
+ use_container_width=True,
253
+ )
254
+ except:
255
+ st.image(
256
+ "error.jpg",
257
+ caption="Server error occur",
258
+ )
259
+ with cols[1]:
260
+ with st.spinner(f"Generating Image {5}..."):
261
+ try:
262
+ image_bytes = image_functions[1](prompts[1])
263
+ image = Image.open(io.BytesIO(image_bytes))
264
+ st.image(
265
+ image,
266
+ caption=f"Prompt {2}, Model {2}",
267
+ use_container_width=True,
268
+ )
269
+ except:
270
+ st.image(
271
+ "error.jpg",
272
+ caption="Server error occur",
273
+ )
274
+ with cols[2]:
275
+ with st.spinner(f"Generating Image {6}..."):
276
+ try:
277
+ image_bytes = image_functions[1](prompts[2])
278
+ image = Image.open(io.BytesIO(image_bytes))
279
+ st.image(
280
+ image,
281
+ caption=f"Prompt {3}, Model {2}",
282
+ use_container_width=True,
283
+ )
284
+ except:
285
+ st.image(
286
+ "error.jpg",
287
+ caption="Server error occur",
288
+ )
289
+
290
+ if st.button("Clear/Regenerate"):
291
+ st.session_state.model_1 = ""
292
+ st.session_state.model_2 = ""
293
+ st.session_state.model_3 = ""
294
+ st.rerun()
295
+
296
+
297
+ # Main logic to control which page to display
298
+ if st.session_state.logged_in:
299
+ platform_page()
300
+ else:
301
+ login()
302
+ register()