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