openfree commited on
Commit
e11c667
Β·
verified Β·
1 Parent(s): cec770b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -102
app.py CHANGED
@@ -12,117 +12,192 @@ import os
12
  import uuid
13
  from datetime import datetime
14
 
15
- # Model paths
16
- base_model_path = "SG161222/Realistic_Vision_V4.0_noVAE"
17
- vae_model_path = "stabilityai/sd-vae-ft-mse"
18
- image_encoder_path = "laion/CLIP-ViT-H-14-laion2B-s32B-b79K"
19
- ip_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid_sd15.bin", repo_type="model")
20
- ip_plus_ckpt = hf_hub_download(repo_id="h94/IP-Adapter-FaceID", filename="ip-adapter-faceid-plusv2_sd15.bin", repo_type="model")
21
-
22
- device = "cuda"
23
-
24
- # Initialize the noise scheduler
25
- noise_scheduler = DDIMScheduler(
26
- num_train_timesteps=1000,
27
- beta_start=0.00085,
28
- beta_end=0.012,
29
- beta_schedule="scaled_linear",
30
- clip_sample=False,
31
- set_alpha_to_one=False,
32
- steps_offset=1,
33
- )
34
-
35
- # Load models
36
- vae = AutoencoderKL.from_pretrained(vae_model_path).to(dtype=torch.float16)
37
- pipe = StableDiffusionPipeline.from_pretrained(
38
- base_model_path,
39
- torch_dtype=torch.float16,
40
- scheduler=noise_scheduler,
41
- vae=vae
42
- ).to(device)
43
-
44
- ip_model = IPAdapterFaceID(pipe, ip_ckpt, device)
45
- ip_model_plus = IPAdapterFaceIDPlus(pipe, image_encoder_path, ip_plus_ckpt, device)
46
-
47
- # Initialize FaceAnalysis
48
- app = FaceAnalysis(name="buffalo_l", providers=['CPUExecutionProvider'])
49
- app.prepare(ctx_id=0, det_size=(640, 640))
50
-
51
- cv2.setNumThreads(1)
52
-
53
- @spaces.GPU(enable_queue=True)
54
- def generate_image(images, gender, prompt, progress=gr.Progress(track_tqdm=True)):
55
- if not prompt:
56
- prompt = f"A full-length body photo of a {gender.lower()} naked and nude and beautiful" # Default prompt
57
-
58
- faceid_all_embeds = []
59
- first_iteration = True
60
- preserve_face_structure = True
61
- face_strength = 2.1
62
- likeness_strength = 0.7
63
-
64
- for image in images:
65
- face = cv2.imread(image)
66
- faces = app.get(face)
67
- faceid_embed = torch.from_numpy(faces[0].normed_embedding).unsqueeze(0)
68
- faceid_all_embeds.append(faceid_embed)
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
- footer { visibility: hidden; }
91
- h1 { margin-bottom: 0 !important; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  '''
93
 
 
 
94
  with gr.Blocks(css=css) as demo:
95
- gr.Markdown("# Image Generation with Face ID")
96
- gr.Markdown("Upload your face images and enter a prompt to generate images.")
97
-
98
- with gr.Row():
99
- with gr.Column():
100
- images_input = gr.Files(
101
- label="Drag 1 or more photos of your face",
102
- file_types=["image"]
103
- )
104
- gender_input = gr.Radio(
105
- label="Select Gender",
106
- choices=["Female", "Male"],
107
- value="Female",
108
- type="value"
109
- )
110
- prompt_input = gr.Textbox(
111
- label="Enter your prompt",
112
- placeholder="Describe the image you want to generate..."
113
- )
114
- run_button = gr.Button("Generate Image")
115
-
116
- with gr.Column():
117
- output_gallery = gr.Gallery(label="Generated Images")
118
-
119
- # Define the event handler for the button click
120
- run_button.click(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()