VcRlAgent commited on
Commit
ef10721
Β·
1 Parent(s): 6e07159

SD based Avatar

Browse files
Files changed (2) hide show
  1. app.py +21 -6
  2. app.py.bak +204 -0
app.py CHANGED
@@ -54,10 +54,20 @@ def load_models():
54
  device=device
55
  )
56
 
57
- return face_enhancer
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Load models globally
60
- face_enhancer = load_models()
61
 
62
  # ------------------------------------------
63
  # GPU-Accelerated Functions
@@ -131,10 +141,15 @@ def create_avatar(img: Image.Image) -> Image.Image:
131
  # Enhance face
132
  img_enhanced = enhance_face(img)
133
 
134
- # Stylize
135
- avatar = ImageOps.posterize(img_enhanced, bits=4)
136
- avatar = ImageEnhance.Color(avatar).enhance(1.8)
137
- avatar = ImageEnhance.Contrast(avatar).enhance(1.2)
 
 
 
 
 
138
 
139
  return avatar
140
 
 
54
  device=device
55
  )
56
 
57
+ # Stable Diffusion Img2Img pipeline (public model)
58
+ sd_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
59
+ "runwayml/stable-diffusion-v1-5",
60
+ torch_dtype=torch.float16
61
+ ).to(device)
62
+
63
+ # Optimize for ZeroGPU memory
64
+ sd_pipe.enable_attention_slicing()
65
+ sd_pipe.enable_model_cpu_offload()
66
+
67
+ return face_enhancer, sd_pipe
68
 
69
  # Load models globally
70
+ face_enhancer, sd_pipe = load_models()
71
 
72
  # ------------------------------------------
73
  # GPU-Accelerated Functions
 
141
  # Enhance face
142
  img_enhanced = enhance_face(img)
143
 
144
+ # Resize for SD (512x512)
145
+ img_resized = img_enhanced.convert("RGB").resize((512, 512))
146
+
147
+ # Stylize with SD prompt
148
+ prompt = "highly detailed, digital portrait, professional lighting, cinematic style, artistic AI avatar"
149
+ with torch.autocast("cuda"):
150
+ result = sd_pipe(prompt=prompt, image=img_resized, strength=0.75, guidance_scale=7.5)
151
+
152
+ avatar = result.images[0]
153
 
154
  return avatar
155
 
app.py.bak ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ==========================================
2
+ # FaceForge AI – ZeroGPU Gradio Version
3
+ # Author: Vijay S. Chaudhari | 2025
4
+ # ==========================================
5
+
6
+ import gradio as gr
7
+ import spaces
8
+ import torch
9
+ import cv2
10
+ import numpy as np
11
+ from PIL import Image, ImageEnhance, ImageOps
12
+ from rembg import remove
13
+ import io
14
+
15
+
16
+ import torchvision
17
+ print("Printing Torch and TorchVision versions:")
18
+ print(torch.__version__)
19
+ print(torchvision.__version__)
20
+
21
+ # GPU libraries
22
+ from gfpgan import GFPGANer
23
+ from basicsr.archs.rrdbnet_arch import RRDBNet
24
+ from realesrgan import RealESRGANer
25
+
26
+ # ------------------------------------------
27
+ # Model Loading (Outside GPU decorator)
28
+ # ------------------------------------------
29
+
30
+ def load_models():
31
+ """Load models once at startup"""
32
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
33
+
34
+ # RealESRGAN upsampler
35
+ model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
36
+ upsampler = RealESRGANer(
37
+ scale=2,
38
+ model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
39
+ model=model,
40
+ tile=400,
41
+ tile_pad=10,
42
+ pre_pad=0,
43
+ half=True,
44
+ device=device
45
+ )
46
+
47
+ # GFPGAN enhancer
48
+ face_enhancer = GFPGANer(
49
+ model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth',
50
+ upscale=2,
51
+ arch='clean',
52
+ channel_multiplier=2,
53
+ bg_upsampler=upsampler,
54
+ device=device
55
+ )
56
+
57
+ return face_enhancer
58
+
59
+ # Load models globally
60
+ face_enhancer = load_models()
61
+
62
+ # ------------------------------------------
63
+ # GPU-Accelerated Functions
64
+ # ------------------------------------------
65
+
66
+ @spaces.GPU
67
+ def enhance_face(img: Image.Image) -> Image.Image:
68
+ """Enhance face using GFPGAN (GPU)"""
69
+ img_cv = cv2.cvtColor(np.array(img.convert('RGB')), cv2.COLOR_RGB2BGR)
70
+
71
+ with torch.no_grad():
72
+ _, _, restored_img = face_enhancer.enhance(
73
+ img_cv,
74
+ has_aligned=False,
75
+ only_center_face=False,
76
+ paste_back=True,
77
+ weight=0.5
78
+ )
79
+
80
+ restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
81
+ return Image.fromarray(restored_img)
82
+
83
+ # ------------------------------------------
84
+ # Image Processing Functions
85
+ # ------------------------------------------
86
+
87
+ def enhance_image(img: Image.Image) -> Image.Image:
88
+ """Basic enhancement"""
89
+ img = ImageEnhance.Contrast(img).enhance(1.15)
90
+ img = ImageEnhance.Sharpness(img).enhance(1.1)
91
+ return img
92
+
93
+ @spaces.GPU
94
+ def create_headshot(img: Image.Image) -> Image.Image:
95
+ """Professional headshot with gradient background"""
96
+ # Enhance face
97
+ img_enhanced = enhance_face(img)
98
+
99
+ # Remove background
100
+ img_no_bg = remove(img_enhanced)
101
+
102
+ # Gradient background
103
+ bg = Image.new("RGB", img_no_bg.size, (200, 210, 230))
104
+ if img_no_bg.mode == 'RGBA':
105
+ bg.paste(img_no_bg, mask=img_no_bg.split()[3])
106
+
107
+ return enhance_image(bg)
108
+
109
+ @spaces.GPU
110
+ def create_passport(img: Image.Image) -> Image.Image:
111
+ """Passport photo with white background"""
112
+ # Enhance face
113
+ img_enhanced = enhance_face(img)
114
+
115
+ # Remove background
116
+ img_no_bg = remove(img_enhanced)
117
+
118
+ # White background (600x600)
119
+ bg = Image.new("RGB", (600, 600), (255, 255, 255))
120
+ img_no_bg.thumbnail((550, 550), Image.Resampling.LANCZOS)
121
+ offset = ((600 - img_no_bg.width) // 2, (600 - img_no_bg.height) // 2)
122
+
123
+ if img_no_bg.mode == 'RGBA':
124
+ bg.paste(img_no_bg, offset, mask=img_no_bg.split()[3])
125
+
126
+ return bg
127
+
128
+ @spaces.GPU
129
+ def create_avatar(img: Image.Image) -> Image.Image:
130
+ """Stylized AI avatar"""
131
+ # Enhance face
132
+ img_enhanced = enhance_face(img)
133
+
134
+ # Stylize
135
+ avatar = ImageOps.posterize(img_enhanced, bits=4)
136
+ avatar = ImageEnhance.Color(avatar).enhance(1.8)
137
+ avatar = ImageEnhance.Contrast(avatar).enhance(1.2)
138
+
139
+ return avatar
140
+
141
+ @spaces.GPU
142
+ def process_all(img: Image.Image):
143
+ """Process all three types at once"""
144
+ headshot = create_headshot(img)
145
+ passport = create_passport(img)
146
+ avatar = create_avatar(img)
147
+ return headshot, passport, avatar
148
+
149
+ # ------------------------------------------
150
+ # Gradio Interface
151
+ # ------------------------------------------
152
+
153
+ with gr.Blocks(theme=gr.themes.Soft(), title="FaceForge AI") as demo:
154
+ gr.Markdown(
155
+ """
156
+ # 🎨 FaceForge AI
157
+ ### GPU-Accelerated Professional Headshot & Avatar Generator
158
+ Upload your photo and generate professional headshots, passport photos, and AI avatars instantly!
159
+ """
160
+ )
161
+
162
+ with gr.Row():
163
+ with gr.Column():
164
+ input_image = gr.Image(type="pil", label="πŸ“· Upload Your Photo")
165
+ process_btn = gr.Button("✨ Generate All Images", variant="primary", size="lg")
166
+
167
+ with gr.Column():
168
+ gr.Markdown("### Results")
169
+
170
+ with gr.Row():
171
+ output_headshot = gr.Image(label="πŸ’Ό Professional Headshot", type="pil")
172
+ output_passport = gr.Image(label="πŸ›‚ Passport Photo", type="pil")
173
+ output_avatar = gr.Image(label="🎭 AI Avatar", type="pil")
174
+
175
+ # Process button
176
+ process_btn.click(
177
+ fn=process_all,
178
+ inputs=input_image,
179
+ outputs=[output_headshot, output_passport, output_avatar]
180
+ )
181
+
182
+ # Examples
183
+ gr.Examples(
184
+ examples=[], # Add example image paths if available
185
+ inputs=input_image
186
+ )
187
+
188
+ gr.Markdown(
189
+ """
190
+ ---
191
+ ### Features
192
+ - πŸ’Ό **Professional Headshots**: Perfect for LinkedIn and business profiles
193
+ - πŸ›‚ **Passport Photos**: Standard 600x600px with white background
194
+ - 🎭 **AI Avatars**: Stylized versions for social media
195
+ - ⚑ **GPU-Accelerated**: Fast processing with GFPGAN enhancement
196
+
197
+ Β© 2025 Vijay S. Chaudhari | Powered by ZeroGPU πŸš€
198
+ """
199
+ )
200
+
201
+ # Launch
202
+ if __name__ == "__main__":
203
+ demo.queue(max_size=20)
204
+ demo.launch()