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|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import base64
|
| 6 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 7 |
+
import io
|
| 8 |
+
import numpy as np
|
| 9 |
+
import cv2
|
| 10 |
+
from typing import List, Tuple, Optional
|
| 11 |
+
import logging
|
| 12 |
+
from datetime import datetime
|
| 13 |
+
import tempfile
|
| 14 |
+
|
| 15 |
+
# Configure logging
|
| 16 |
+
logging.basicConfig(level=logging.INFO)
|
| 17 |
+
logger = logging.getLogger(__name__)
|
| 18 |
+
|
| 19 |
+
class SalesforceConnector:
|
| 20 |
+
"""Handle Salesforce API connections and data retrieval"""
|
| 21 |
+
|
| 22 |
+
def __init__(self):
|
| 23 |
+
self.access_token = None
|
| 24 |
+
self.instance_url = None
|
| 25 |
+
self.session = requests.Session()
|
| 26 |
+
|
| 27 |
+
def authenticate(self, client_id: str, client_secret: str, username: str, password: str, security_token: str = ""):
|
| 28 |
+
"""Authenticate with Salesforce using OAuth 2.0"""
|
| 29 |
+
try:
|
| 30 |
+
auth_url = "https://login.salesforce.com/services/oauth2/token"
|
| 31 |
+
auth_data = {
|
| 32 |
+
'grant_type': 'password',
|
| 33 |
+
'client_id': client_id,
|
| 34 |
+
'client_secret': client_secret,
|
| 35 |
+
'username': username,
|
| 36 |
+
'password': password + security_token
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
response = self.session.post(auth_url, data=auth_data)
|
| 40 |
+
response.raise_for_status()
|
| 41 |
+
|
| 42 |
+
auth_result = response.json()
|
| 43 |
+
self.access_token = auth_result['access_token']
|
| 44 |
+
self.instance_url = auth_result['instance_url']
|
| 45 |
+
|
| 46 |
+
self.session.headers.update({
|
| 47 |
+
'Authorization': f'Bearer {self.access_token}',
|
| 48 |
+
'Content-Type': 'application/json'
|
| 49 |
+
})
|
| 50 |
+
|
| 51 |
+
logger.info("Successfully authenticated with Salesforce")
|
| 52 |
+
return True
|
| 53 |
+
|
| 54 |
+
except requests.exceptions.RequestException as e:
|
| 55 |
+
logger.error(f"Salesforce authentication failed: {e}")
|
| 56 |
+
return False
|
| 57 |
+
|
| 58 |
+
def fetch_dress_catalog(self) -> List[dict]:
|
| 59 |
+
"""Fetch dress catalog from Salesforce"""
|
| 60 |
+
try:
|
| 61 |
+
# Query for dress products (adjust SOQL query based on your Salesforce schema)
|
| 62 |
+
query = """
|
| 63 |
+
SELECT Id, Name, Description, Product_Image_URL__c, Category__c, Price__c, Size__c, Color__c
|
| 64 |
+
FROM Product2
|
| 65 |
+
WHERE Category__c = 'Dress' OR Category__c = 'Clothing'
|
| 66 |
+
ORDER BY Name
|
| 67 |
+
LIMIT 50
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
query_url = f"{self.instance_url}/services/data/v58.0/query"
|
| 71 |
+
params = {'q': query}
|
| 72 |
+
|
| 73 |
+
response = self.session.get(query_url, params=params)
|
| 74 |
+
response.raise_for_status()
|
| 75 |
+
|
| 76 |
+
result = response.json()
|
| 77 |
+
dresses = result.get('records', [])
|
| 78 |
+
|
| 79 |
+
logger.info(f"Fetched {len(dresses)} dresses from Salesforce")
|
| 80 |
+
return dresses
|
| 81 |
+
|
| 82 |
+
except requests.exceptions.RequestException as e:
|
| 83 |
+
logger.error(f"Failed to fetch dress catalog: {e}")
|
| 84 |
+
return self._get_sample_dresses()
|
| 85 |
+
|
| 86 |
+
def _get_sample_dresses(self) -> List[dict]:
|
| 87 |
+
"""Return sample dress data for demo purposes"""
|
| 88 |
+
return [
|
| 89 |
+
{
|
| 90 |
+
"Id": "dress_001",
|
| 91 |
+
"Name": "Elegant Evening Dress",
|
| 92 |
+
"Description": "Beautiful black evening dress perfect for special occasions",
|
| 93 |
+
"Product_Image_URL__c": "https://images.pexels.com/photos/1926769/pexels-photo-1926769.jpeg",
|
| 94 |
+
"Category__c": "Dress",
|
| 95 |
+
"Price__c": 299.99,
|
| 96 |
+
"Size__c": "M",
|
| 97 |
+
"Color__c": "Black"
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"Id": "dress_002",
|
| 101 |
+
"Name": "Summer Floral Dress",
|
| 102 |
+
"Description": "Light and airy floral dress for summer occasions",
|
| 103 |
+
"Product_Image_URL__c": "https://images.pexels.com/photos/1926769/pexels-photo-1926769.jpeg",
|
| 104 |
+
"Category__c": "Dress",
|
| 105 |
+
"Price__c": 149.99,
|
| 106 |
+
"Size__c": "S",
|
| 107 |
+
"Color__c": "Floral"
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"Id": "dress_003",
|
| 111 |
+
"Name": "Business Casual Dress",
|
| 112 |
+
"Description": "Professional dress suitable for office wear",
|
| 113 |
+
"Product_Image_URL__c": "https://images.pexels.com/photos/1926769/pexels-photo-1926769.jpeg",
|
| 114 |
+
"Category__c": "Dress",
|
| 115 |
+
"Price__c": 199.99,
|
| 116 |
+
"Size__c": "L",
|
| 117 |
+
"Color__c": "Navy"
|
| 118 |
+
}
|
| 119 |
+
]
|
| 120 |
+
|
| 121 |
+
class VirtualTryOnEngine:
|
| 122 |
+
"""Handle virtual try-on processing"""
|
| 123 |
+
|
| 124 |
+
def __init__(self):
|
| 125 |
+
self.model_loaded = False
|
| 126 |
+
|
| 127 |
+
def process_tryon(self, person_image: Image.Image, dress_image: Image.Image, dress_info: dict) -> Tuple[Image.Image, str]:
|
| 128 |
+
"""Process virtual try-on between person and dress images"""
|
| 129 |
+
try:
|
| 130 |
+
# For demo purposes, we'll create a simple overlay effect
|
| 131 |
+
# In production, you would use a proper virtual try-on model
|
| 132 |
+
result_image = self._create_demo_tryon(person_image, dress_image, dress_info)
|
| 133 |
+
|
| 134 |
+
confidence_score = "85%"
|
| 135 |
+
return result_image, confidence_score
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
logger.error(f"Try-on processing failed: {e}")
|
| 139 |
+
return person_image, "Error"
|
| 140 |
+
|
| 141 |
+
def _create_demo_tryon(self, person_image: Image.Image, dress_image: Image.Image, dress_info: dict) -> Image.Image:
|
| 142 |
+
"""Create a demo try-on result by overlaying dress info"""
|
| 143 |
+
# Resize images to standard size
|
| 144 |
+
person_img = person_image.convert("RGB")
|
| 145 |
+
person_img = person_img.resize((512, 768))
|
| 146 |
+
|
| 147 |
+
# Create a copy for the result
|
| 148 |
+
result_img = person_img.copy()
|
| 149 |
+
draw = ImageDraw.Draw(result_img)
|
| 150 |
+
|
| 151 |
+
# Add dress information overlay
|
| 152 |
+
try:
|
| 153 |
+
font = ImageFont.load_default()
|
| 154 |
+
except:
|
| 155 |
+
font = None
|
| 156 |
+
|
| 157 |
+
# Add semi-transparent overlay
|
| 158 |
+
overlay = Image.new('RGBA', result_img.size, (0, 0, 0, 128))
|
| 159 |
+
result_img = Image.alpha_composite(result_img.convert('RGBA'), overlay).convert('RGB')
|
| 160 |
+
|
| 161 |
+
# Add dress information text
|
| 162 |
+
dress_name = dress_info.get('Name', 'Selected Dress')
|
| 163 |
+
price = dress_info.get('Price__c', 0)
|
| 164 |
+
color = dress_info.get('Color__c', 'N/A')
|
| 165 |
+
size = dress_info.get('Size__c', 'N/A')
|
| 166 |
+
|
| 167 |
+
text_lines = [
|
| 168 |
+
f"Virtual Try-On Result",
|
| 169 |
+
f"Dress: {dress_name}",
|
| 170 |
+
f"Price: ${price}",
|
| 171 |
+
f"Color: {color}",
|
| 172 |
+
f"Size: {size}",
|
| 173 |
+
f"Confidence: 85%"
|
| 174 |
+
]
|
| 175 |
+
|
| 176 |
+
y_offset = 20
|
| 177 |
+
for line in text_lines:
|
| 178 |
+
draw.text((20, y_offset), line, fill="white", font=font)
|
| 179 |
+
y_offset += 25
|
| 180 |
+
|
| 181 |
+
return result_img
|
| 182 |
+
|
| 183 |
+
class VirtualTryOnApp:
|
| 184 |
+
"""Main application class"""
|
| 185 |
+
|
| 186 |
+
def __init__(self):
|
| 187 |
+
self.sf_connector = SalesforceConnector()
|
| 188 |
+
self.tryon_engine = VirtualTryOnEngine()
|
| 189 |
+
self.dress_catalog = []
|
| 190 |
+
self.selected_dress = None
|
| 191 |
+
|
| 192 |
+
def initialize_salesforce(self, client_id: str, client_secret: str, username: str, password: str, security_token: str = ""):
|
| 193 |
+
"""Initialize Salesforce connection"""
|
| 194 |
+
success = self.sf_connector.authenticate(client_id, client_secret, username, password, security_token)
|
| 195 |
+
if success:
|
| 196 |
+
self.dress_catalog = self.sf_connector.fetch_dress_catalog()
|
| 197 |
+
return "โ
Successfully connected to Salesforce and loaded dress catalog!", self._get_dress_options()
|
| 198 |
+
else:
|
| 199 |
+
# Load sample data for demo
|
| 200 |
+
self.dress_catalog = self.sf_connector._get_sample_dresses()
|
| 201 |
+
return "โ ๏ธ Using sample data (Salesforce connection failed)", self._get_dress_options()
|
| 202 |
+
|
| 203 |
+
def _get_dress_options(self) -> List[str]:
|
| 204 |
+
"""Get dress options for dropdown"""
|
| 205 |
+
return [f"{dress['Name']} - ${dress['Price__c']} ({dress['Color__c']})" for dress in self.dress_catalog]
|
| 206 |
+
|
| 207 |
+
def select_dress(self, dress_selection: str) -> Tuple[str, str]:
|
| 208 |
+
"""Handle dress selection"""
|
| 209 |
+
if not dress_selection or not self.dress_catalog:
|
| 210 |
+
return "Please select a dress", ""
|
| 211 |
+
|
| 212 |
+
# Find selected dress
|
| 213 |
+
dress_name = dress_selection.split(' - ')[0]
|
| 214 |
+
self.selected_dress = next((dress for dress in self.dress_catalog if dress['Name'] == dress_name), None)
|
| 215 |
+
|
| 216 |
+
if self.selected_dress:
|
| 217 |
+
dress_info = f"""
|
| 218 |
+
**Selected Dress Details:**
|
| 219 |
+
- **Name:** {self.selected_dress['Name']}
|
| 220 |
+
- **Description:** {self.selected_dress['Description']}
|
| 221 |
+
- **Price:** ${self.selected_dress['Price__c']}
|
| 222 |
+
- **Color:** {self.selected_dress['Color__c']}
|
| 223 |
+
- **Size:** {self.selected_dress['Size__c']}
|
| 224 |
+
"""
|
| 225 |
+
|
| 226 |
+
image_url = self.selected_dress.get('Product_Image_URL__c', '')
|
| 227 |
+
return dress_info, image_url
|
| 228 |
+
|
| 229 |
+
return "Dress not found", ""
|
| 230 |
+
|
| 231 |
+
def process_virtual_tryon(self, person_image: Image.Image) -> Tuple[Image.Image, str]:
|
| 232 |
+
"""Process the virtual try-on"""
|
| 233 |
+
if person_image is None:
|
| 234 |
+
return None, "Please upload a person image"
|
| 235 |
+
|
| 236 |
+
if self.selected_dress is None:
|
| 237 |
+
return person_image, "Please select a dress first"
|
| 238 |
+
|
| 239 |
+
try:
|
| 240 |
+
# Load dress image from URL
|
| 241 |
+
dress_url = self.selected_dress.get('Product_Image_URL__c', '')
|
| 242 |
+
if dress_url:
|
| 243 |
+
dress_response = requests.get(dress_url, timeout=10)
|
| 244 |
+
dress_image = Image.open(io.BytesIO(dress_response.content))
|
| 245 |
+
else:
|
| 246 |
+
# Create placeholder dress image
|
| 247 |
+
dress_image = Image.new('RGB', (300, 400), color='lightblue')
|
| 248 |
+
|
| 249 |
+
# Process virtual try-on
|
| 250 |
+
result_image, confidence = self.tryon_engine.process_tryon(person_image, dress_image, self.selected_dress)
|
| 251 |
+
|
| 252 |
+
status_message = f"โ
Virtual try-on completed! Confidence: {confidence}"
|
| 253 |
+
return result_image, status_message
|
| 254 |
+
|
| 255 |
+
except Exception as e:
|
| 256 |
+
logger.error(f"Virtual try-on failed: {e}")
|
| 257 |
+
return person_image, f"โ Try-on failed: {str(e)}"
|
| 258 |
+
|
| 259 |
+
# Initialize the app
|
| 260 |
+
app = VirtualTryOnApp()
|
| 261 |
+
|
| 262 |
+
def setup_salesforce_connection(client_id, client_secret, username, password, security_token):
|
| 263 |
+
"""Setup Salesforce connection and return status and dress options"""
|
| 264 |
+
status, dress_options = app.initialize_salesforce(client_id, client_secret, username, password, security_token)
|
| 265 |
+
return status, gr.Dropdown(choices=dress_options, value=None)
|
| 266 |
+
|
| 267 |
+
def handle_dress_selection(dress_selection):
|
| 268 |
+
"""Handle dress selection and return dress info and image"""
|
| 269 |
+
dress_info, image_url = app.select_dress(dress_selection)
|
| 270 |
+
|
| 271 |
+
# Try to load dress image
|
| 272 |
+
dress_image = None
|
| 273 |
+
if image_url:
|
| 274 |
+
try:
|
| 275 |
+
response = requests.get(image_url, timeout=10)
|
| 276 |
+
dress_image = Image.open(io.BytesIO(response.content))
|
| 277 |
+
except:
|
| 278 |
+
dress_image = None
|
| 279 |
+
|
| 280 |
+
return dress_info, dress_image
|
| 281 |
+
|
| 282 |
+
def process_tryon(person_image):
|
| 283 |
+
"""Process virtual try-on"""
|
| 284 |
+
return app.process_virtual_tryon(person_image)
|
| 285 |
+
|
| 286 |
+
# Custom CSS for better styling
|
| 287 |
+
custom_css = """
|
| 288 |
+
.gradio-container {
|
| 289 |
+
font-family: 'Inter', sans-serif;
|
| 290 |
+
max-width: 1200px !important;
|
| 291 |
+
margin: 0 auto;
|
| 292 |
+
}
|
| 293 |
+
|
| 294 |
+
.header-text {
|
| 295 |
+
text-align: center;
|
| 296 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 297 |
+
-webkit-background-clip: text;
|
| 298 |
+
-webkit-text-fill-color: transparent;
|
| 299 |
+
font-size: 2.5rem;
|
| 300 |
+
font-weight: bold;
|
| 301 |
+
margin-bottom: 1rem;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
+
.section-header {
|
| 305 |
+
background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
|
| 306 |
+
padding: 1rem;
|
| 307 |
+
border-radius: 8px;
|
| 308 |
+
color: white;
|
| 309 |
+
font-weight: bold;
|
| 310 |
+
margin: 1rem 0;
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
.status-success {
|
| 314 |
+
background-color: #d4edda;
|
| 315 |
+
color: #155724;
|
| 316 |
+
padding: 1rem;
|
| 317 |
+
border-radius: 8px;
|
| 318 |
+
border: 1px solid #c3e6cb;
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
.status-error {
|
| 322 |
+
background-color: #f8d7da;
|
| 323 |
+
color: #721c24;
|
| 324 |
+
padding: 1rem;
|
| 325 |
+
border-radius: 8px;
|
| 326 |
+
border: 1px solid #f5c6cb;
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
.image-container {
|
| 330 |
+
border: 2px solid #e1e5e9;
|
| 331 |
+
border-radius: 12px;
|
| 332 |
+
padding: 1rem;
|
| 333 |
+
background: white;
|
| 334 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
.tryon-results {
|
| 338 |
+
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
|
| 339 |
+
padding: 2rem;
|
| 340 |
+
border-radius: 12px;
|
| 341 |
+
margin-top: 1rem;
|
| 342 |
+
}
|
| 343 |
+
"""
|
| 344 |
+
|
| 345 |
+
# Create the Gradio interface
|
| 346 |
+
with gr.Blocks(css=custom_css, title="Virtual Try-On for Clothing") as demo:
|
| 347 |
+
|
| 348 |
+
# Header
|
| 349 |
+
gr.HTML("""
|
| 350 |
+
<div class="header-text">
|
| 351 |
+
๐ Virtual Try-On for Clothing
|
| 352 |
+
</div>
|
| 353 |
+
<p style="text-align: center; font-size: 1.2rem; color: #666; margin-bottom: 2rem;">
|
| 354 |
+
Experience the future of fashion with AI-powered virtual try-on technology
|
| 355 |
+
</p>
|
| 356 |
+
""")
|
| 357 |
+
|
| 358 |
+
# Salesforce Configuration Section
|
| 359 |
+
with gr.Accordion("๐ง Salesforce Configuration", open=True):
|
| 360 |
+
gr.HTML('<div class="section-header">Connect to Salesforce to access dress catalog</div>')
|
| 361 |
+
|
| 362 |
+
with gr.Row():
|
| 363 |
+
with gr.Column(scale=2):
|
| 364 |
+
sf_client_id = gr.Textbox(
|
| 365 |
+
label="Client ID",
|
| 366 |
+
placeholder="Enter your Salesforce Client ID",
|
| 367 |
+
type="password"
|
| 368 |
+
)
|
| 369 |
+
sf_client_secret = gr.Textbox(
|
| 370 |
+
label="Client Secret",
|
| 371 |
+
placeholder="Enter your Salesforce Client Secret",
|
| 372 |
+
type="password"
|
| 373 |
+
)
|
| 374 |
+
with gr.Column(scale=2):
|
| 375 |
+
sf_username = gr.Textbox(
|
| 376 |
+
label="Username",
|
| 377 |
+
placeholder="Enter your Salesforce username"
|
| 378 |
+
)
|
| 379 |
+
sf_password = gr.Textbox(
|
| 380 |
+
label="Password",
|
| 381 |
+
placeholder="Enter your Salesforce password",
|
| 382 |
+
type="password"
|
| 383 |
+
)
|
| 384 |
+
sf_security_token = gr.Textbox(
|
| 385 |
+
label="Security Token (Optional)",
|
| 386 |
+
placeholder="Enter security token if required"
|
| 387 |
+
)
|
| 388 |
+
|
| 389 |
+
sf_connect_btn = gr.Button("๐ Connect to Salesforce", variant="primary", size="lg")
|
| 390 |
+
sf_status = gr.Textbox(label="Connection Status", interactive=False)
|
| 391 |
+
|
| 392 |
+
# Main Application Interface
|
| 393 |
+
with gr.Row():
|
| 394 |
+
# Left Column - Input Section
|
| 395 |
+
with gr.Column(scale=1):
|
| 396 |
+
gr.HTML('<div class="section-header">๐ท Upload Your Photo</div>')
|
| 397 |
+
|
| 398 |
+
person_image = gr.Image(
|
| 399 |
+
label="Upload your photo",
|
| 400 |
+
type="pil",
|
| 401 |
+
height=400,
|
| 402 |
+
elem_classes=["image-container"]
|
| 403 |
+
)
|
| 404 |
+
|
| 405 |
+
gr.HTML('<div class="section-header">๐ Select Dress</div>')
|
| 406 |
+
|
| 407 |
+
dress_dropdown = gr.Dropdown(
|
| 408 |
+
label="Choose a dress from catalog",
|
| 409 |
+
choices=[],
|
| 410 |
+
interactive=True
|
| 411 |
+
)
|
| 412 |
+
|
| 413 |
+
dress_info = gr.Markdown(
|
| 414 |
+
label="Dress Information",
|
| 415 |
+
value="Select a dress to see details"
|
| 416 |
+
)
|
| 417 |
+
|
| 418 |
+
dress_image = gr.Image(
|
| 419 |
+
label="Selected Dress",
|
| 420 |
+
type="pil",
|
| 421 |
+
height=300,
|
| 422 |
+
elem_classes=["image-container"]
|
| 423 |
+
)
|
| 424 |
+
|
| 425 |
+
# Right Column - Results Section
|
| 426 |
+
with gr.Column(scale=1):
|
| 427 |
+
gr.HTML('<div class="section-header">โจ Virtual Try-On Results</div>')
|
| 428 |
+
|
| 429 |
+
tryon_btn = gr.Button(
|
| 430 |
+
"๐ญ Generate Virtual Try-On",
|
| 431 |
+
variant="primary",
|
| 432 |
+
size="lg",
|
| 433 |
+
elem_classes=["tryon-button"]
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
+
result_image = gr.Image(
|
| 437 |
+
label="Try-On Result",
|
| 438 |
+
type="pil",
|
| 439 |
+
height=500,
|
| 440 |
+
elem_classes=["image-container", "tryon-results"]
|
| 441 |
+
)
|
| 442 |
+
|
| 443 |
+
result_status = gr.Textbox(
|
| 444 |
+
label="Processing Status",
|
| 445 |
+
interactive=False
|
| 446 |
+
)
|
| 447 |
+
|
| 448 |
+
# Action buttons
|
| 449 |
+
with gr.Row():
|
| 450 |
+
download_btn = gr.DownloadButton(
|
| 451 |
+
"๐พ Download Result",
|
| 452 |
+
variant="secondary"
|
| 453 |
+
)
|
| 454 |
+
share_btn = gr.Button(
|
| 455 |
+
"๐ค Share Result",
|
| 456 |
+
variant="secondary"
|
| 457 |
+
)
|
| 458 |
+
|
| 459 |
+
# Advanced Options
|
| 460 |
+
with gr.Accordion("โ๏ธ Advanced Options", open=False):
|
| 461 |
+
with gr.Row():
|
| 462 |
+
fit_adjustment = gr.Slider(
|
| 463 |
+
label="Fit Adjustment",
|
| 464 |
+
minimum=-10,
|
| 465 |
+
maximum=10,
|
| 466 |
+
value=0,
|
| 467 |
+
step=1
|
| 468 |
+
)
|
| 469 |
+
brightness_adjustment = gr.Slider(
|
| 470 |
+
label="Brightness",
|
| 471 |
+
minimum=-50,
|
| 472 |
+
maximum=50,
|
| 473 |
+
value=0,
|
| 474 |
+
step=5
|
| 475 |
+
)
|
| 476 |
+
pose_adjustment = gr.Dropdown(
|
| 477 |
+
label="Pose Adjustment",
|
| 478 |
+
choices=["Natural", "Straighten", "Slight Turn"],
|
| 479 |
+
value="Natural"
|
| 480 |
+
)
|
| 481 |
+
|
| 482 |
+
# Footer Information
|
| 483 |
+
gr.HTML("""
|
| 484 |
+
<div style="margin-top: 3rem; padding: 2rem; background: #f8f9fa; border-radius: 12px; text-align: center;">
|
| 485 |
+
<h3>๐ Features</h3>
|
| 486 |
+
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem; margin-top: 1rem;">
|
| 487 |
+
<div>
|
| 488 |
+
<strong>๐ Salesforce Integration</strong><br>
|
| 489 |
+
Direct access to your dress catalog
|
| 490 |
+
</div>
|
| 491 |
+
<div>
|
| 492 |
+
<strong>๐ค AI-Powered Try-On</strong><br>
|
| 493 |
+
Advanced virtual try-on technology
|
| 494 |
+
</div>
|
| 495 |
+
<div>
|
| 496 |
+
<strong>๐ฑ Responsive Design</strong><br>
|
| 497 |
+
Works on all devices
|
| 498 |
+
</div>
|
| 499 |
+
<div>
|
| 500 |
+
<strong>๐พ Export Options</strong><br>
|
| 501 |
+
Save and share your results
|
| 502 |
+
</div>
|
| 503 |
+
</div>
|
| 504 |
+
<p style="margin-top: 1.5rem; color: #666;">
|
| 505 |
+
Upload your photo, select a dress from your Salesforce catalog, and see how it looks on you instantly!
|
| 506 |
+
</p>
|
| 507 |
+
</div>
|
| 508 |
+
""")
|
| 509 |
+
|
| 510 |
+
# Event handlers
|
| 511 |
+
sf_connect_btn.click(
|
| 512 |
+
fn=setup_salesforce_connection,
|
| 513 |
+
inputs=[sf_client_id, sf_client_secret, sf_username, sf_password, sf_security_token],
|
| 514 |
+
outputs=[sf_status, dress_dropdown]
|
| 515 |
+
)
|
| 516 |
+
|
| 517 |
+
dress_dropdown.change(
|
| 518 |
+
fn=handle_dress_selection,
|
| 519 |
+
inputs=[dress_dropdown],
|
| 520 |
+
outputs=[dress_info, dress_image]
|
| 521 |
+
)
|
| 522 |
+
|
| 523 |
+
tryon_btn.click(
|
| 524 |
+
fn=process_tryon,
|
| 525 |
+
inputs=[person_image],
|
| 526 |
+
outputs=[result_image, result_status]
|
| 527 |
+
)
|
| 528 |
+
|
| 529 |
+
# Initialize with sample data on load
|
| 530 |
+
demo.load(
|
| 531 |
+
fn=lambda: app.initialize_salesforce("", "", "", "", ""),
|
| 532 |
+
outputs=[sf_status, dress_dropdown]
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
# Additional utility functions
|
| 536 |
+
def create_sample_person_image():
|
| 537 |
+
"""Create a sample person image for testing"""
|
| 538 |
+
img = Image.new('RGB', (512, 768), color='lightgray')
|
| 539 |
+
draw = ImageDraw.Draw(img)
|
| 540 |
+
|
| 541 |
+
# Draw a simple person silhouette
|
| 542 |
+
draw.ellipse([206, 100, 306, 200], fill='darkgray') # Head
|
| 543 |
+
draw.rectangle([231, 200, 281, 400], fill='darkgray') # Body
|
| 544 |
+
draw.rectangle([206, 400, 231, 600], fill='darkgray') # Left leg
|
| 545 |
+
draw.rectangle([281, 400, 306, 600], fill='darkgray') # Right leg
|
| 546 |
+
draw.rectangle([181, 220, 206, 350], fill='darkgray') # Left arm
|
| 547 |
+
draw.rectangle([306, 220, 331, 350], fill='darkgray') # Right arm
|
| 548 |
+
|
| 549 |
+
return img
|
| 550 |
+
|
| 551 |
+
def validate_image_input(image):
|
| 552 |
+
"""Validate uploaded image"""
|
| 553 |
+
if image is None:
|
| 554 |
+
return False, "No image uploaded"
|
| 555 |
+
|
| 556 |
+
# Check image size
|
| 557 |
+
if image.size[0] < 100 or image.size[1] < 100:
|
| 558 |
+
return False, "Image too small (minimum 100x100 pixels)"
|
| 559 |
+
|
| 560 |
+
# Check image format
|
| 561 |
+
if image.format not in ['JPEG', 'PNG', 'JPG']:
|
| 562 |
+
return False, "Unsupported image format (use JPEG or PNG)"
|
| 563 |
+
|
| 564 |
+
return True, "Image is valid"
|
| 565 |
+
|
| 566 |
+
def enhance_image_quality(image: Image.Image) -> Image.Image:
|
| 567 |
+
"""Enhance image quality for better try-on results"""
|
| 568 |
+
try:
|
| 569 |
+
# Convert to RGB if necessary
|
| 570 |
+
if image.mode != 'RGB':
|
| 571 |
+
image = image.convert('RGB')
|
| 572 |
+
|
| 573 |
+
# Resize to optimal dimensions
|
| 574 |
+
max_size = 1024
|
| 575 |
+
if max(image.size) > max_size:
|
| 576 |
+
ratio = max_size / max(image.size)
|
| 577 |
+
new_size = tuple(int(dim * ratio) for dim in image.size)
|
| 578 |
+
image = image.resize(new_size, Image.Resampling.LANCZOS)
|
| 579 |
+
|
| 580 |
+
# Enhance contrast and sharpness
|
| 581 |
+
from PIL import ImageEnhance
|
| 582 |
+
|
| 583 |
+
enhancer = ImageEnhance.Contrast(image)
|
| 584 |
+
image = enhancer.enhance(1.1)
|
| 585 |
+
|
| 586 |
+
enhancer = ImageEnhance.Sharpness(image)
|
| 587 |
+
image = enhancer.enhance(1.1)
|
| 588 |
+
|
| 589 |
+
return image
|
| 590 |
+
|
| 591 |
+
except Exception as e:
|
| 592 |
+
logger.error(f"Image enhancement failed: {e}")
|
| 593 |
+
return image
|
| 594 |
+
|
| 595 |
+
# Error handling and logging
|
| 596 |
+
def log_user_interaction(action: str, details: dict = None):
|
| 597 |
+
"""Log user interactions for analytics"""
|
| 598 |
+
timestamp = datetime.now().isoformat()
|
| 599 |
+
log_entry = {
|
| 600 |
+
'timestamp': timestamp,
|
| 601 |
+
'action': action,
|
| 602 |
+
'details': details or {}
|
| 603 |
+
}
|
| 604 |
+
logger.info(f"User interaction: {json.dumps(log_entry)}")
|
| 605 |
+
|
| 606 |
+
# Performance monitoring
|
| 607 |
+
def measure_processing_time(func):
|
| 608 |
+
"""Decorator to measure processing time"""
|
| 609 |
+
def wrapper(*args, **kwargs):
|
| 610 |
+
start_time = datetime.now()
|
| 611 |
+
result = func(*args, **kwargs)
|
| 612 |
+
end_time = datetime.now()
|
| 613 |
+
processing_time = (end_time - start_time).total_seconds()
|
| 614 |
+
logger.info(f"Processing time for {func.__name__}: {processing_time:.2f} seconds")
|
| 615 |
+
return result
|
| 616 |
+
return wrapper
|
| 617 |
+
|
| 618 |
+
# Security utilities
|
| 619 |
+
def sanitize_user_input(input_text: str) -> str:
|
| 620 |
+
"""Sanitize user input to prevent security issues"""
|
| 621 |
+
if not input_text:
|
| 622 |
+
return ""
|
| 623 |
+
|
| 624 |
+
# Remove potentially dangerous characters
|
| 625 |
+
dangerous_chars = ['<', '>', '"', "'", '&', ';']
|
| 626 |
+
for char in dangerous_chars:
|
| 627 |
+
input_text = input_text.replace(char, '')
|
| 628 |
+
|
| 629 |
+
return input_text.strip()
|
| 630 |
+
|
| 631 |
+
def validate_salesforce_credentials(client_id: str, client_secret: str, username: str, password: str) -> bool:
|
| 632 |
+
"""Validate Salesforce credentials format"""
|
| 633 |
+
if not all([client_id, client_secret, username, password]):
|
| 634 |
+
return False
|
| 635 |
+
|
| 636 |
+
# Basic format validation
|
| 637 |
+
if len(client_id) < 10 or len(client_secret) < 10:
|
| 638 |
+
return False
|
| 639 |
+
|
| 640 |
+
if '@' not in username:
|
| 641 |
+
return False
|
| 642 |
+
|
| 643 |
+
return True
|
| 644 |
+
|
| 645 |
+
# Configuration management
|
| 646 |
+
class AppConfig:
|
| 647 |
+
"""Application configuration management"""
|
| 648 |
+
|
| 649 |
+
def __init__(self):
|
| 650 |
+
self.max_image_size = 10 * 1024 * 1024 # 10MB
|
| 651 |
+
self.supported_formats = ['JPEG', 'PNG', 'JPG']
|
| 652 |
+
self.max_processing_time = 30 # seconds
|
| 653 |
+
self.cache_duration = 3600 # 1 hour
|
| 654 |
+
|
| 655 |
+
def get_huggingface_config(self):
|
| 656 |
+
"""Get configuration for Hugging Face deployment"""
|
| 657 |
+
return {
|
| 658 |
+
'title': 'Virtual Try-On for Clothing',
|
| 659 |
+
'description': 'AI-powered virtual try-on application with Salesforce integration',
|
| 660 |
+
'tags': ['fashion', 'ai', 'virtual-try-on', 'salesforce'],
|
| 661 |
+
'requirements': 'requirements.txt'
|
| 662 |
+
}
|
| 663 |
+
|
| 664 |
+
# Initialize configuration
|
| 665 |
+
config = AppConfig()
|
| 666 |
+
|
| 667 |
+
# Main execution
|
| 668 |
+
if __name__ == "__main__":
|
| 669 |
+
# Launch the Gradio app
|
| 670 |
+
demo.launch(
|
| 671 |
+
server_name="0.0.0.0",
|
| 672 |
+
server_port=7860,
|
| 673 |
+
share=True,
|
| 674 |
+
debug=True,
|
| 675 |
+
show_error=True,
|
| 676 |
+
favicon_path=None,
|
| 677 |
+
ssl_verify=False
|
| 678 |
+
)
|