Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,117 +12,192 @@ import os
|
|
| 12 |
import uuid
|
| 13 |
from datetime import datetime
|
| 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 |
-
if first_iteration and preserve_face_structure:
|
| 71 |
-
face_image = face_align.norm_crop(face, landmark=faces[0].kps, image_size=224)
|
| 72 |
-
first_iteration = False
|
| 73 |
-
|
| 74 |
-
average_embedding = torch.mean(torch.stack(faceid_all_embeds, dim=0), dim=0)
|
| 75 |
-
|
| 76 |
-
image = ip_model_plus.generate(
|
| 77 |
-
prompt=prompt,
|
| 78 |
-
faceid_embeds=average_embedding,
|
| 79 |
-
scale=likeness_strength,
|
| 80 |
-
face_image=face_image,
|
| 81 |
-
shortcut=True,
|
| 82 |
-
s_scale=face_strength,
|
| 83 |
-
width=512,
|
| 84 |
-
height=912,
|
| 85 |
-
num_inference_steps=100
|
| 86 |
-
)
|
| 87 |
-
return image
|
| 88 |
|
| 89 |
css = '''
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
'''
|
| 93 |
|
|
|
|
|
|
|
| 94 |
with gr.Blocks(css=css) as demo:
|
| 95 |
-
gr.
|
| 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 |
fn=generate_image,
|
| 122 |
inputs=[images_input, gender_input, prompt_input],
|
| 123 |
outputs=output_gallery
|
| 124 |
)
|
| 125 |
|
| 126 |
-
# Launch the interface
|
| 127 |
demo.queue()
|
| 128 |
demo.launch()
|
|
|
|
| 12 |
import uuid
|
| 13 |
from datetime import datetime
|
| 14 |
|
| 15 |
+
# Previous model initialization code remains the same...
|
| 16 |
+
|
| 17 |
+
STYLE_PRESETS = [
|
| 18 |
+
{
|
| 19 |
+
"title": "Mona Lisa",
|
| 20 |
+
"prompt": "A mesmerizing portrait in the style of Leonardo da Vinci's Mona Lisa, renaissance oil painting, soft sfumato technique, mysterious smile, Florentine background, museum quality, masterpiece",
|
| 21 |
+
"preview": "π¨"
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"title": "Iron Hero",
|
| 25 |
+
"prompt": "Hyper realistic portrait as a high-tech superhero, wearing advanced metallic suit, arc reactor glow, inside high-tech lab, dramatic lighting, cinematic composition",
|
| 26 |
+
"preview": "π¦Ύ"
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"title": "Ancient Egyptian",
|
| 30 |
+
"prompt": "Portrait as an ancient Egyptian pharaoh, wearing golden headdress and royal regalia, hieroglyphics background, dramatic desert lighting, archaeological discovery style",
|
| 31 |
+
"preview": "π"
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"title": "Sherlock Holmes",
|
| 35 |
+
"prompt": "Victorian era detective portrait, wearing deerstalker hat and cape, holding magnifying glass, foggy London background, mysterious atmosphere, detailed illustration",
|
| 36 |
+
"preview": "π"
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"title": "Star Wars Jedi",
|
| 40 |
+
"prompt": "Epic portrait as a Jedi Master, wearing traditional robes, holding lightsaber, temple background, force aura effect, cinematic lighting, movie poster quality",
|
| 41 |
+
"preview": "βοΈ"
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"title": "Van Gogh Style",
|
| 45 |
+
"prompt": "Self-portrait in the style of Vincent van Gogh, bold brushstrokes, vibrant colors, post-impressionist style, emotional intensity, starry background",
|
| 46 |
+
"preview": "π¨"
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"title": "Greek God",
|
| 50 |
+
"prompt": "Mythological portrait as an Olympian deity, wearing flowing robes, golden laurel wreath, Mount Olympus background, godly aura, classical Greek art style",
|
| 51 |
+
"preview": "β‘"
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"title": "Medieval Knight",
|
| 55 |
+
"prompt": "Noble knight portrait, wearing ornate plate armor, holding sword and shield, castle background, heraldic designs, medieval manuscript style",
|
| 56 |
+
"preview": "π‘οΈ"
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"title": "Matrix Hero",
|
| 60 |
+
"prompt": "Cyberpunk portrait in digital reality, wearing black trench coat and sunglasses, green code rain effect, dystopian atmosphere, cinematic style",
|
| 61 |
+
"preview": "πΆοΈ"
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"title": "Pirate Captain",
|
| 65 |
+
"prompt": "Swashbuckling pirate captain portrait, wearing tricorn hat and colonial coat, ship's deck background, dramatic sea storm, golden age of piracy style",
|
| 66 |
+
"preview": "π΄ββ οΈ"
|
| 67 |
+
}
|
| 68 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
css = '''
|
| 71 |
+
#component-0 {
|
| 72 |
+
max-width: 1200px;
|
| 73 |
+
margin: auto;
|
| 74 |
+
padding: 20px;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
.container {
|
| 78 |
+
background-color: #ffffff;
|
| 79 |
+
border-radius: 10px;
|
| 80 |
+
padding: 20px;
|
| 81 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
.header {
|
| 85 |
+
text-align: center;
|
| 86 |
+
margin-bottom: 2rem;
|
| 87 |
+
background: linear-gradient(90deg, #2C3E50, #3498DB);
|
| 88 |
+
padding: 2rem;
|
| 89 |
+
border-radius: 10px;
|
| 90 |
+
color: white;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
.preset-grid {
|
| 94 |
+
display: grid;
|
| 95 |
+
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
|
| 96 |
+
gap: 1rem;
|
| 97 |
+
margin: 1rem 0;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
.preset-card {
|
| 101 |
+
background: #f8f9fa;
|
| 102 |
+
padding: 1rem;
|
| 103 |
+
border-radius: 8px;
|
| 104 |
+
cursor: pointer;
|
| 105 |
+
transition: all 0.3s ease;
|
| 106 |
+
border: 1px solid #e9ecef;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
.preset-card:hover {
|
| 110 |
+
transform: translateY(-2px);
|
| 111 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
|
| 112 |
+
background: #f1f3f5;
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
.preset-emoji {
|
| 116 |
+
font-size: 2rem;
|
| 117 |
+
margin-bottom: 0.5rem;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
.input-container {
|
| 121 |
+
background: #f8f9fa;
|
| 122 |
+
padding: 1.5rem;
|
| 123 |
+
border-radius: 8px;
|
| 124 |
+
margin-bottom: 1rem;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
footer {display: none !important}
|
| 128 |
'''
|
| 129 |
|
| 130 |
+
# Generate image function remains the same...
|
| 131 |
+
|
| 132 |
with gr.Blocks(css=css) as demo:
|
| 133 |
+
with gr.Column(elem_classes="container"):
|
| 134 |
+
with gr.Column(elem_classes="header"):
|
| 135 |
+
gr.Markdown("# β¨ Magic Face")
|
| 136 |
+
gr.Markdown("### Transform Your Face Into Legendary Characters!")
|
| 137 |
+
|
| 138 |
+
with gr.Row():
|
| 139 |
+
with gr.Column(scale=1):
|
| 140 |
+
images_input = gr.Files(
|
| 141 |
+
label="πΈ Upload Your Face Photos",
|
| 142 |
+
file_types=["image"],
|
| 143 |
+
elem_classes="input-container"
|
| 144 |
+
)
|
| 145 |
+
gender_input = gr.Radio(
|
| 146 |
+
label="Select Gender",
|
| 147 |
+
choices=["Female", "Male"],
|
| 148 |
+
value="Female",
|
| 149 |
+
type="value"
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
with gr.Column(elem_classes="preset-container"):
|
| 153 |
+
gr.Markdown("### π Magic Transformations")
|
| 154 |
+
preset_grid = []
|
| 155 |
+
for idx, preset in enumerate(STYLE_PRESETS):
|
| 156 |
+
preset_button = gr.Button(
|
| 157 |
+
f"{preset['preview']} {preset['title']}",
|
| 158 |
+
elem_classes="preset-card"
|
| 159 |
+
)
|
| 160 |
+
preset_button.click(
|
| 161 |
+
lambda idx=idx: (idx, STYLE_PRESETS[idx]["prompt"]),
|
| 162 |
+
outputs=[gr.Number(visible=False), prompt_input]
|
| 163 |
+
)
|
| 164 |
+
preset_grid.append(preset_button)
|
| 165 |
+
|
| 166 |
+
prompt_input = gr.Textbox(
|
| 167 |
+
label="π¨ Custom Prompt",
|
| 168 |
+
placeholder="Describe your desired transformation in detail...",
|
| 169 |
+
lines=3
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
generate_button = gr.Button("π Generate Magic", variant="primary")
|
| 173 |
+
|
| 174 |
+
with gr.Column(scale=1):
|
| 175 |
+
output_gallery = gr.Gallery(
|
| 176 |
+
label="Magic Gallery",
|
| 177 |
+
elem_classes="output-gallery",
|
| 178 |
+
columns=2
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
with gr.Accordion("π Quick Guide", open=False):
|
| 182 |
+
gr.Markdown("""
|
| 183 |
+
### How to Use Magic Face
|
| 184 |
+
1. Upload one or more face photos
|
| 185 |
+
2. Select your gender
|
| 186 |
+
3. Choose a magical transformation or write your own prompt
|
| 187 |
+
4. Click 'Generate Magic'
|
| 188 |
+
|
| 189 |
+
### Pro Tips
|
| 190 |
+
- Upload multiple angles of your face for better results
|
| 191 |
+
- Try combining different historical or fictional characters
|
| 192 |
+
- Feel free to modify the preset prompts
|
| 193 |
+
- Click on generated images to view them in full size
|
| 194 |
+
""")
|
| 195 |
+
|
| 196 |
+
generate_button.click(
|
| 197 |
fn=generate_image,
|
| 198 |
inputs=[images_input, gender_input, prompt_input],
|
| 199 |
outputs=output_gallery
|
| 200 |
)
|
| 201 |
|
|
|
|
| 202 |
demo.queue()
|
| 203 |
demo.launch()
|