File size: 10,692 Bytes
0cf1f72 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
import gradio as gr
import sys
import os
# Add the dist directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'dist'))
# Import obfuscated module
try:
import core_logic
from core_logic import *
data_manager = DataManager()
except ImportError as e:
print(f"Error: Obfuscated module not found: {e}")
print("Current directory:", os.getcwd())
print("Files in dist:", os.listdir('dist') if os.path.exists('dist') else 'dist not found')
sys.exit(1)
def virtual_tryon(human_img, garment_img, garment_type, request: gr.Request):
if human_img is None:
raise gr.Error("Please upload a person image first!")
if garment_img is None:
raise gr.Error("Please upload a garment image first!")
user_context = get_user_context(request)
print(f"User context: {user_context}")
current_attempts = data_manager.get_attempts(user_context)
print(f"Current attempts for {user_context}: {current_attempts}")
if current_attempts >= MAX_FREE_TRIALS:
raise gr.Error(
f"You've used {MAX_FREE_TRIALS} free daily credits. "
f"Please visit https://miragic.ai/ to sign up for unlimited access!"
)
try:
# Process the images
result_img = process_tryon_request(human_img, garment_img, garment_type)
# Increment attempts after successful generation
new_count = data_manager.increment_attempts(user_context)
print(f"New attempt count for {user_context}: {new_count}")
return result_img
except Exception as e:
print(f"Error in virtual_tryon: {str(e)}")
raise e
def create_footer():
return f"""
<div style="text-align: center; margin-top: 30px; padding: 15px; border-radius: 8px;">
<p style="margin: 5px 0;">Β© 2025 {COMPANY_NAME}. All rights reserved.</p>
<p style="margin: 5px 0;">
<a href="{COMPANY_URL}" target="_blank" style="color: #0984e3; text-decoration: none;">Website</a> |
<a href="mailto:{CONTACT_EMAIL}" style="color: #0984e3; text-decoration: none;">Contact Us</a> |
<a href="{COMPANY_URL}/privacy-policy" target="_blank" style="color: #0984e3; text-decoration: none;">Privacy Policy</a>
</p>
</div>
"""
def create_instructions():
return """
<div style="padding: 15px; border-radius: 8px; margin-bottom: 20px;">
<h2 style="margin-top: 0;">How to use:</h2>
<ol style="color: #636e72;">
<li>Upload a clear photo of a person (front view works best)</li>
<li>Upload an image of the garment you want to try on</li>
<li>Click "Run Virtual Try-On" to see the result</li>
</ol>
<p style="margin-bottom: 0;"><strong>Tip:</strong> For best results, use images with plain backgrounds.</p>
</div>
"""
# Custom CSS
css = """
footer {visibility: hidden}
.banner {
background-color: #f8f9fa;
padding: 10px;
border-radius: 5px;
margin-bottom: 20px;
text-align: center;
}
.button-gradient {
background: linear-gradient(45deg, #ff416c, #ff4b2b, #ff9b00, #ff416c);
background-size: 400% 400%;
border: none;
padding: 14px 28px;
font-size: 16px;
font-weight: bold;
color: white;
border-radius: 10px;
cursor: pointer;
transition: 0.3s ease-in-out;
animation: gradientAnimation 2s infinite linear;
box-shadow: 0 4px 10px rgba(255, 65, 108, 0.6);
}
@keyframes gradientAnimation {
0% { background-position: 0% 50%; }
100% { background-position: 100% 50%; }
}
.button-gradient:hover {
transform: scale(1.05);
box-shadow: 0 6px 15px rgba(255, 75, 43, 0.8);
}
.signup-container {
text-align: center;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 8px;
margin-top: 20px;
color: white;
box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}
.signup-container h3 {
margin-bottom: 10px;
color: white;
}
.signup-container p {
margin-bottom: 15px;
color: #f0f0f0;
}
.signup-button {
background: linear-gradient(45deg, #ff416c, #ff4b2b);
border: none;
padding: 12px 25px;
font-size: 16px;
font-weight: bold;
color: white;
border-radius: 8px;
text-decoration: none;
display: inline-block;
transition: all 0.3s ease;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}
.signup-button:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}
.attempts-counter {
background: #e3f2fd;
padding: 10px;
border-radius: 5px;
margin: 10px 0;
text-align: center;
font-weight: bold;
color: #1976d2;
}
#col-container {
max-width: 1400px;
margin: 0 auto;
}
.step-column {
padding: 20px;
border-radius: 8px;
box-shadow: var(--card-shadow);
margin: 10px;
}
.step-title {
color: var(--primary-color);
text-align: center;
margin-bottom: 15px;
}
.image-preview {
border-radius: 8px;
overflow: hidden;
box-shadow: var(--card-shadow);
}
.result-section {
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: var(--card-shadow);
}
.footer {
margin-top: 30px;
text-align: center;
}
"""
# Example images setup
example_path = os.path.join(os.path.dirname(__file__), 'assets')
human_examples = [
os.path.join(example_path, "human", f) for f in os.listdir(os.path.join(example_path, "human"))
if f.endswith(('.jpg', '.jpeg', '.png'))
] if os.path.exists(os.path.join(example_path, "human")) else []
garment_examples = [
os.path.join(example_path, "garment", f) for f in os.listdir(os.path.join(example_path, "garment"))
if f.endswith(('.jpg', '.jpeg', '.png'))
] if os.path.exists(os.path.join(example_path, "garment")) else []
with gr.Blocks(css=css, title=f"{COMPANY_NAME} Virtual Try-On", theme=gr.themes.Ocean()) as demo:
gr.Markdown("""
<div style="display: flex; align-items: center;">
<img src="https://avatars.githubusercontent.com/u/211682198?s=200&v=4" style="width: 80px; margin-right: 20px;"/>
<div>
<h1 style="margin-bottom: 0;">Miragic Virtual Try-On π π</h1>
<p style="margin-top: 0; color: #636e72;">Try on complete outfits with our AI-powered virtual try-on technology</p>
</div>
</div>
""")
# Instructions
gr.HTML(create_instructions())
with gr.Row(elem_id="col-container"):
# Step 1: Person Image
with gr.Column(elem_classes="step-column"):
gr.HTML("""
<div class="step-title">
<span style="font-size: 24px;">1. Upload Person Image</span><br>
</div>
""")
human_img = gr.Image(
label="Person Image",
sources='upload',
type="numpy",
elem_classes="image-preview",
height=400
)
if human_examples:
gr.Examples(
examples=human_examples,
inputs=human_img,
label="Example Person Images",
examples_per_page=12
)
# Step 2: Garment Image
with gr.Column(elem_classes="step-column"):
gr.HTML("""
<div class="step-title">
<span style="font-size: 24px;">2. Upload Garment Image</span><br>
</div>
""")
garment_img = gr.Image(
label="Garment Image",
sources='upload',
type="numpy",
elem_classes="image-preview",
height=400
)
garment_type = gr.Dropdown(
choices=["Dress/Suit", "Top", "Bottom"],
value="Dress/Suit",
label="Garment Type",
info="Select the type of garment you want to try on"
)
if garment_examples:
gr.Examples(
examples=garment_examples,
inputs=garment_img,
label="Example Garment Images",
examples_per_page=12
)
# Step 3: Results
with gr.Column(elem_classes="step-column"):
gr.HTML("""
<div class="step-title">
<span style="font-size: 24px;">3. Virtual Try-On Result</span><br>
</div>
""")
result_img = gr.Image(
label="Try-On Result",
interactive=False,
elem_classes="image-preview",
height=400
)
try_button = gr.Button(
"Run Virtual Try-On π",
elem_classes="button-gradient"
)
signup_prompt = gr.HTML(
visible=True,
value="""<div class="signup-container">
<h3>π Want unlimited generations?</h3>
<p>Please sign up at Miragic.ai for unlimited access to all our AI tools!</p>
<a href='https://miragic.ai/products/virtual-try-on' target='_blank' class="signup-button">
SignUp for Free π
</a>
</div>"""
)
if os.path.exists("assets/examples"):
with gr.Row():
gr.Examples(
examples=[
["assets/examples/model1.jpg", "assets/examples/garment1.png", "Dress/Suit", "assets/examples/result1.jpg"],
["assets/examples/model2.png", "assets/examples/garment2.jpg", "Dress/Suit", "assets/examples/result2.jpg"],
["assets/examples/model3.jpg", "assets/examples/garment3.jpg", "Dress/Suit", "assets/examples/result3.jpg"],
],
inputs=[human_img, garment_img, garment_type, result_img],
label=None,
examples_per_page=3
)
# Button action
try_button.click(
fn=virtual_tryon,
inputs=[human_img, garment_img, garment_type],
outputs=result_img,
api_name="virtual_tryon"
)
gr.HTML('<a href="https://visitorbadge.io/status?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiragic-AI%2FMiragic-Virtual-Try-On"><img src="https://api.visitorbadge.io/api/combined?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2FMiragic-AI%2FMiragic-Virtual-Try-On&label=VISITORS&labelColor=%2337d67a&countColor=%23f47373&style=plastic&labelStyle=upper" /></a>')
# Footer
gr.HTML(create_footer())
if __name__ == "__main__":
demo.launch(
show_api=False,
server_name="0.0.0.0",
server_port=7860
) |