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