Spaces:
Sleeping
Sleeping
Readme.MD
Browse files- README.md +114 -1
- app copy.py +0 -226
- app.py.bak +0 -204
README.md
CHANGED
|
@@ -11,4 +11,117 @@ license: mit
|
|
| 11 |
short_description: FaceForgeAI_ZeroGPU
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
short_description: FaceForgeAI_ZeroGPU
|
| 12 |
---
|
| 13 |
|
| 14 |
+
# 🎨 FaceForge AI – ZeroGPU Gradio Edition
|
| 15 |
+
[](https://huggingface.co/spaces/VcRlAgent/FaceForgeAI_ZeroGPU)
|
| 16 |
+
|
| 17 |
+
**Author:** Vijay S. Chaudhari
|
| 18 |
+
**Runtime:** Hugging Face Spaces (ZeroGPU) 🚀
|
| 19 |
+
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
## 🧠 Overview
|
| 23 |
+
**FaceForge AI** transforms your uploaded photo into professional-quality images powered by open-source generative AI:
|
| 24 |
+
- 🪄 **Background Remover** – clean gradient background for profile photos
|
| 25 |
+
- 🛂 **Passport Photo** – compliant 600×600 white-background image (Requires picture with frontal face)
|
| 26 |
+
- 🎭 **Stylized AI Avatar** – realistic yet personalized stylization
|
| 27 |
+
|
| 28 |
+
This edition is optimized for **ZeroGPU Spaces** — efficient, on-demand GPU execution using CPU offload and attention slicing for lightweight inference.
|
| 29 |
+
|
| 30 |
+
---
|
| 31 |
+
|
| 32 |
+
## ⚙️ Key Features
|
| 33 |
+
✅ **Three Modes**
|
| 34 |
+
- **Background Remover** – removes background and enhances clarity
|
| 35 |
+
- **Passport** – white background, standard size
|
| 36 |
+
- **Avatar** – realistic stylization via Stable Diffusion Img2Img
|
| 37 |
+
|
| 38 |
+
✅ **User Control for Avatar Generation**
|
| 39 |
+
- Preset prompt styles + custom text input
|
| 40 |
+
- Adjustable `strength` & `guidance_scale` sliders
|
| 41 |
+
|
| 42 |
+
---
|
| 43 |
+
|
| 44 |
+
## 🧩 Tech Stack
|
| 45 |
+
|
| 46 |
+
| Component | Purpose |
|
| 47 |
+
|------------|----------|
|
| 48 |
+
| **Python 3.10+** | Core runtime |
|
| 49 |
+
| **Gradio 4.x** | Web UI framework |
|
| 50 |
+
| **Stable Diffusion v1.5** | Img2Img stylization |
|
| 51 |
+
| **GFPGAN 1.3.8** | Facial restoration |
|
| 52 |
+
| **Real-ESRGAN 0.3.0** | Super-resolution |
|
| 53 |
+
| **Rembg 2.x** | Background removal |
|
| 54 |
+
| **Pillow / OpenCV / Torch** | Image processing + GPU acceleration |
|
| 55 |
+
|
| 56 |
+
---
|
| 57 |
+
|
| 58 |
+
## 🖥️ UI Preview
|
| 59 |
+
|
| 60 |
+
| Upload → Choose → Generate |
|
| 61 |
+
|-----------------------------|
|
| 62 |
+
|  |
|
| 63 |
+
|
| 64 |
+
> _Example UI layout with independent buttons for Background Remover, Passport, and Avatar generation._
|
| 65 |
+
|
| 66 |
+
---
|
| 67 |
+
|
| 68 |
+
## 🧰 Installation
|
| 69 |
+
|
| 70 |
+
### 1️⃣ Clone Repository
|
| 71 |
+
```bash
|
| 72 |
+
git clone https://github.com/agentofAI/FaceForgeAI.git
|
| 73 |
+
cd FaceForgeAI_ZeroGPU
|
| 74 |
+
|
| 75 |
+
### 2️⃣ Install Dependencies
|
| 76 |
+
|
| 77 |
+
pip install -r requirements.txt
|
| 78 |
+
|
| 79 |
+
### 3️⃣ Run Locally
|
| 80 |
+
python app.py
|
| 81 |
+
|
| 82 |
+
Open the local Gradio URL (typically http://127.0.0.1:7860) in your browser.
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
|
| 86 |
+
Avatar Prompt Presets
|
| 87 |
+
Style Label Prompt
|
| 88 |
+
🎬 Cinematic Portrait highly detailed, digital portrait, professional lighting, cinematic style, artistic AI avatar
|
| 89 |
+
🎨 Stylized Realism stylized yet realistic portrait, balanced lighting, subtle gradient background, sharp
|
| 90 |
+
focus on face
|
| 91 |
+
🏢 Studio Professional studio portrait, even lighting, neutral background, realistic skin, confident pose
|
| 92 |
+
🤵 Natural Headshot realistic professional headshot, soft studio lighting, neutral background, crisp details,
|
| 93 |
+
natural skin tone
|
| 94 |
+
|
| 95 |
+
---
|
| 96 |
+
|
| 97 |
+
## 🧾 Model Credits
|
| 98 |
+
| Model | Source / License |
|
| 99 |
+
| ------------------------- | ------------------------------------------------------------------------------------------------------------- |
|
| 100 |
+
| **Stable Diffusion v1.5** | [runwayml/stable-diffusion-v1-5](https://huggingface.co/runwayml/stable-diffusion-v1-5) (CompVis / Runway ML) |
|
| 101 |
+
| **GFPGAN v1.3** | [TencentARC/GFPGAN](https://github.com/TencentARC/GFPGAN) (MIT License) |
|
| 102 |
+
| **Real-ESRGAN x2Plus** | [xinntao/Real-ESRGAN](https://github.com/xinntao/Real-ESRGAN) (BSD License) |
|
| 103 |
+
| **Rembg** | [danielgatis/rembg](https://github.com/danielgatis/rembg) |
|
| 104 |
+
| **Gradio** | [gradio-app/gradio](https://github.com/gradio-app/gradio) |
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
## 🧠 Project Structure
|
| 109 |
+
faceforge-ai/
|
| 110 |
+
│
|
| 111 |
+
├── app.py # Main application
|
| 112 |
+
├── requirements.txt # Dependencies
|
| 113 |
+
├── assets/ # Optional screenshots and samples
|
| 114 |
+
└── README.md # Documentation
|
| 115 |
+
|
| 116 |
+
---
|
| 117 |
+
|
| 118 |
+
## 📜 License
|
| 119 |
+
|
| 120 |
+
This project is for educational and demonstration purposes.
|
| 121 |
+
Each model used retains its original open-source license.
|
| 122 |
+
|
| 123 |
+
---
|
| 124 |
+
## 👨💻 Author
|
| 125 |
+
|
| 126 |
+
Vijay S. Chaudhari
|
| 127 |
+
🔗 LinkedIn Profile
|
app copy.py
DELETED
|
@@ -1,226 +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 |
-
from diffusers import StableDiffusionImg2ImgPipeline
|
| 14 |
-
import io
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
import torchvision
|
| 18 |
-
print("Printing Torch and TorchVision versions:")
|
| 19 |
-
print(torch.__version__)
|
| 20 |
-
print(torchvision.__version__)
|
| 21 |
-
|
| 22 |
-
# GPU libraries
|
| 23 |
-
from gfpgan import GFPGANer
|
| 24 |
-
from basicsr.archs.rrdbnet_arch import RRDBNet
|
| 25 |
-
from realesrgan import RealESRGANer
|
| 26 |
-
|
| 27 |
-
# ------------------------------------------
|
| 28 |
-
# Model Loading (Outside GPU decorator)
|
| 29 |
-
# ------------------------------------------
|
| 30 |
-
|
| 31 |
-
def load_models():
|
| 32 |
-
"""Load models once at startup"""
|
| 33 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 34 |
-
|
| 35 |
-
# RealESRGAN upsampler
|
| 36 |
-
model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2)
|
| 37 |
-
upsampler = RealESRGANer(
|
| 38 |
-
scale=2,
|
| 39 |
-
model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth',
|
| 40 |
-
model=model,
|
| 41 |
-
tile=400,
|
| 42 |
-
tile_pad=10,
|
| 43 |
-
pre_pad=0,
|
| 44 |
-
half=True,
|
| 45 |
-
device=device
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
# GFPGAN enhancer
|
| 49 |
-
face_enhancer = GFPGANer(
|
| 50 |
-
model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth',
|
| 51 |
-
upscale=2,
|
| 52 |
-
arch='clean',
|
| 53 |
-
channel_multiplier=2,
|
| 54 |
-
bg_upsampler=upsampler,
|
| 55 |
-
device=device
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
# Stable Diffusion Img2Img pipeline (public model)
|
| 59 |
-
sd_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
| 60 |
-
"runwayml/stable-diffusion-v1-5",
|
| 61 |
-
torch_dtype=torch.float16
|
| 62 |
-
).to(device)
|
| 63 |
-
|
| 64 |
-
# Optimize for ZeroGPU memory
|
| 65 |
-
sd_pipe.enable_attention_slicing()
|
| 66 |
-
sd_pipe.enable_model_cpu_offload()
|
| 67 |
-
|
| 68 |
-
return face_enhancer, sd_pipe
|
| 69 |
-
|
| 70 |
-
# Load models globally
|
| 71 |
-
face_enhancer, sd_pipe = load_models()
|
| 72 |
-
|
| 73 |
-
# ------------------------------------------
|
| 74 |
-
# GPU-Accelerated Functions
|
| 75 |
-
# ------------------------------------------
|
| 76 |
-
|
| 77 |
-
@spaces.GPU
|
| 78 |
-
def enhance_face(img: Image.Image) -> Image.Image:
|
| 79 |
-
"""Enhance face using GFPGAN (GPU)"""
|
| 80 |
-
img_cv = cv2.cvtColor(np.array(img.convert('RGB')), cv2.COLOR_RGB2BGR)
|
| 81 |
-
|
| 82 |
-
with torch.no_grad():
|
| 83 |
-
_, _, restored_img = face_enhancer.enhance(
|
| 84 |
-
img_cv,
|
| 85 |
-
has_aligned=False,
|
| 86 |
-
only_center_face=False,
|
| 87 |
-
paste_back=True,
|
| 88 |
-
weight=0.5
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB)
|
| 92 |
-
return Image.fromarray(restored_img)
|
| 93 |
-
|
| 94 |
-
# ------------------------------------------
|
| 95 |
-
# Image Processing Functions
|
| 96 |
-
# ------------------------------------------
|
| 97 |
-
|
| 98 |
-
def enhance_image(img: Image.Image) -> Image.Image:
|
| 99 |
-
"""Basic enhancement"""
|
| 100 |
-
img = ImageEnhance.Contrast(img).enhance(1.15)
|
| 101 |
-
img = ImageEnhance.Sharpness(img).enhance(1.1)
|
| 102 |
-
return img
|
| 103 |
-
|
| 104 |
-
@spaces.GPU
|
| 105 |
-
def create_headshot(img: Image.Image) -> Image.Image:
|
| 106 |
-
"""Professional headshot with gradient background"""
|
| 107 |
-
# Enhance face
|
| 108 |
-
img_enhanced = enhance_face(img)
|
| 109 |
-
|
| 110 |
-
# Remove background
|
| 111 |
-
img_no_bg = remove(img_enhanced)
|
| 112 |
-
|
| 113 |
-
# Gradient background
|
| 114 |
-
bg = Image.new("RGB", img_no_bg.size, (200, 210, 230))
|
| 115 |
-
if img_no_bg.mode == 'RGBA':
|
| 116 |
-
bg.paste(img_no_bg, mask=img_no_bg.split()[3])
|
| 117 |
-
|
| 118 |
-
return enhance_image(bg)
|
| 119 |
-
|
| 120 |
-
@spaces.GPU
|
| 121 |
-
def create_passport(img: Image.Image) -> Image.Image:
|
| 122 |
-
"""Passport photo with white background"""
|
| 123 |
-
# Enhance face
|
| 124 |
-
img_enhanced = enhance_face(img)
|
| 125 |
-
|
| 126 |
-
# Remove background
|
| 127 |
-
img_no_bg = remove(img_enhanced)
|
| 128 |
-
|
| 129 |
-
# White background (600x600)
|
| 130 |
-
bg = Image.new("RGB", (600, 600), (255, 255, 255))
|
| 131 |
-
img_no_bg.thumbnail((550, 550), Image.Resampling.LANCZOS)
|
| 132 |
-
offset = ((600 - img_no_bg.width) // 2, (600 - img_no_bg.height) // 2)
|
| 133 |
-
|
| 134 |
-
if img_no_bg.mode == 'RGBA':
|
| 135 |
-
bg.paste(img_no_bg, offset, mask=img_no_bg.split()[3])
|
| 136 |
-
|
| 137 |
-
return bg
|
| 138 |
-
|
| 139 |
-
@spaces.GPU
|
| 140 |
-
def create_avatar(img: Image.Image) -> Image.Image:
|
| 141 |
-
"""Stylized AI avatar"""
|
| 142 |
-
# Enhance face
|
| 143 |
-
img_enhanced = enhance_face(img)
|
| 144 |
-
|
| 145 |
-
# Resize for SD (512x512)
|
| 146 |
-
img_resized = img_enhanced.convert("RGB").resize((512, 512))
|
| 147 |
-
|
| 148 |
-
# Stylize with SD prompt
|
| 149 |
-
#prompt = "highly detailed, digital portrait, professional lighting, cinematic style, artistic AI avatar"
|
| 150 |
-
#prompt = "stylized yet realistic portrait, balanced lighting, subtle gradient background, sharp focus on face"
|
| 151 |
-
#prompt = "studio portrait, even lighting, neutral background, realistic skin, confident pose"
|
| 152 |
-
prompt = "realistic professional headshot, soft studio lighting, neutral background, crisp details, natural skin tone"
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
with torch.autocast("cuda"):
|
| 157 |
-
result = sd_pipe(prompt=prompt, image=img_resized, strength=0.4, guidance_scale=5.0)
|
| 158 |
-
|
| 159 |
-
avatar = result.images[0]
|
| 160 |
-
|
| 161 |
-
return avatar
|
| 162 |
-
|
| 163 |
-
@spaces.GPU
|
| 164 |
-
def process_all(img: Image.Image):
|
| 165 |
-
"""Process all three types at once"""
|
| 166 |
-
headshot = create_headshot(img)
|
| 167 |
-
passport = create_passport(img)
|
| 168 |
-
avatar = create_avatar(img)
|
| 169 |
-
return headshot, passport, avatar
|
| 170 |
-
|
| 171 |
-
# ------------------------------------------
|
| 172 |
-
# Gradio Interface
|
| 173 |
-
# ------------------------------------------
|
| 174 |
-
|
| 175 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="FaceForge AI") as demo:
|
| 176 |
-
gr.Markdown(
|
| 177 |
-
"""
|
| 178 |
-
# 🎨 FaceForge AI
|
| 179 |
-
### GPU-Accelerated Professional Headshot & Avatar Generator
|
| 180 |
-
Upload your photo and generate professional headshots, passport photos, and AI avatars instantly!
|
| 181 |
-
"""
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
-
with gr.Row():
|
| 185 |
-
with gr.Column():
|
| 186 |
-
input_image = gr.Image(type="pil", label="📷 Upload Your Photo")
|
| 187 |
-
process_btn = gr.Button("✨ Generate All Images", variant="primary", size="lg")
|
| 188 |
-
|
| 189 |
-
with gr.Column():
|
| 190 |
-
gr.Markdown("### Results")
|
| 191 |
-
|
| 192 |
-
with gr.Row():
|
| 193 |
-
output_headshot = gr.Image(label="💼 Professional Headshot", type="pil")
|
| 194 |
-
output_passport = gr.Image(label="🛂 Passport Photo", type="pil")
|
| 195 |
-
output_avatar = gr.Image(label="🎭 AI Avatar", type="pil")
|
| 196 |
-
|
| 197 |
-
# Process button
|
| 198 |
-
process_btn.click(
|
| 199 |
-
fn=process_all,
|
| 200 |
-
inputs=input_image,
|
| 201 |
-
outputs=[output_headshot, output_passport, output_avatar]
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
# Examples
|
| 205 |
-
gr.Examples(
|
| 206 |
-
examples=[], # Add example image paths if available
|
| 207 |
-
inputs=input_image
|
| 208 |
-
)
|
| 209 |
-
|
| 210 |
-
gr.Markdown(
|
| 211 |
-
"""
|
| 212 |
-
---
|
| 213 |
-
### Features
|
| 214 |
-
- 💼 **Professional Headshots**: Perfect for LinkedIn and business profiles
|
| 215 |
-
- 🛂 **Passport Photos**: Standard 600x600px with white background
|
| 216 |
-
- 🎭 **AI Avatars**: Stylized versions for social media
|
| 217 |
-
- ⚡ **GPU-Accelerated**: Fast processing with GFPGAN enhancement
|
| 218 |
-
|
| 219 |
-
© 2025 Vijay S. Chaudhari | Powered by ZeroGPU 🚀
|
| 220 |
-
"""
|
| 221 |
-
)
|
| 222 |
-
|
| 223 |
-
# Launch
|
| 224 |
-
if __name__ == "__main__":
|
| 225 |
-
demo.queue(max_size=20)
|
| 226 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py.bak
DELETED
|
@@ -1,204 +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 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|