Spaces:
Sleeping
Sleeping
Refactor to enable users to chose Avatar generation modes
Browse filesFiles changed (4)
- app copy.pyAdded+226
- app.py+68-27
- app.py.origDeleted-14
- requirement.txt.bkDeleted-34
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226# ==========================================# FaceForge AI β ZeroGPU Gradio Version# Author: Vijay S. Chaudhari | 2025# ==========================================
import gradio as grimport spacesimport torchimport cv2import numpy as npfrom PIL import Image, ImageEnhance, ImageOpsfrom rembg import removefrom diffusers import StableDiffusionImg2ImgPipelineimport io
import torchvisionprint("Printing Torch and TorchVision versions:")print(torch.__version__)print(torchvision.__version__)
# GPU librariesfrom gfpgan import GFPGANerfrom basicsr.archs.rrdbnet_arch import RRDBNetfrom realesrgan import RealESRGANer
# ------------------------------------------# Model Loading (Outside GPU decorator)# ------------------------------------------
def load_models(): """Load models once at startup""" device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # RealESRGAN upsampler model = RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=23, num_grow_ch=32, scale=2) upsampler = RealESRGANer( scale=2, model_path='https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.1/RealESRGAN_x2plus.pth', model=model, tile=400, tile_pad=10, pre_pad=0, half=True, device=device ) # GFPGAN enhancer face_enhancer = GFPGANer( model_path='https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.3.pth', upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=upsampler, device=device ) # Stable Diffusion Img2Img pipeline (public model) sd_pipe = StableDiffusionImg2ImgPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16 ).to(device)
# Optimize for ZeroGPU memory sd_pipe.enable_attention_slicing() sd_pipe.enable_model_cpu_offload()
return face_enhancer, sd_pipe
# Load models globallyface_enhancer, sd_pipe = load_models()
# ------------------------------------------# GPU-Accelerated Functions# ------------------------------------------
@spaces.GPUdef enhance_face(img: Image.Image) -> Image.Image: """Enhance face using GFPGAN (GPU)""" img_cv = cv2.cvtColor(np.array(img.convert('RGB')), cv2.COLOR_RGB2BGR) with torch.no_grad(): _, _, restored_img = face_enhancer.enhance( img_cv, has_aligned=False, only_center_face=False, paste_back=True, weight=0.5 ) restored_img = cv2.cvtColor(restored_img, cv2.COLOR_BGR2RGB) return Image.fromarray(restored_img)
# ------------------------------------------# Image Processing Functions# ------------------------------------------
def enhance_image(img: Image.Image) -> Image.Image: """Basic enhancement""" img = ImageEnhance.Contrast(img).enhance(1.15) img = ImageEnhance.Sharpness(img).enhance(1.1) return img
@spaces.GPUdef create_headshot(img: Image.Image) -> Image.Image: """Professional headshot with gradient background""" # Enhance face img_enhanced = enhance_face(img) # Remove background img_no_bg = remove(img_enhanced) # Gradient background bg = Image.new("RGB", img_no_bg.size, (200, 210, 230)) if img_no_bg.mode == 'RGBA': bg.paste(img_no_bg, mask=img_no_bg.split()[3]) return enhance_image(bg)
@spaces.GPUdef create_passport(img: Image.Image) -> Image.Image: """Passport photo with white background""" # Enhance face img_enhanced = enhance_face(img) # Remove background img_no_bg = remove(img_enhanced) # White background (600x600) bg = Image.new("RGB", (600, 600), (255, 255, 255)) img_no_bg.thumbnail((550, 550), Image.Resampling.LANCZOS) offset = ((600 - img_no_bg.width) // 2, (600 - img_no_bg.height) // 2) if img_no_bg.mode == 'RGBA': bg.paste(img_no_bg, offset, mask=img_no_bg.split()[3]) return bg
@spaces.GPUdef create_avatar(img: Image.Image) -> Image.Image: """Stylized AI avatar""" # Enhance face img_enhanced = enhance_face(img) # Resize for SD (512x512) img_resized = img_enhanced.convert("RGB").resize((512, 512))
# Stylize with SD prompt #prompt = "highly detailed, digital portrait, professional lighting, cinematic style, artistic AI avatar" #prompt = "stylized yet realistic portrait, balanced lighting, subtle gradient background, sharp focus on face" #prompt = "studio portrait, even lighting, neutral background, realistic skin, confident pose" prompt = "realistic professional headshot, soft studio lighting, neutral background, crisp details, natural skin tone"
with torch.autocast("cuda"): result = sd_pipe(prompt=prompt, image=img_resized, strength=0.4, guidance_scale=5.0)
avatar = result.images[0] return avatar
@spaces.GPUdef process_all(img: Image.Image): """Process all three types at once""" headshot = create_headshot(img) passport = create_passport(img) avatar = create_avatar(img) return headshot, passport, avatar
# ------------------------------------------# Gradio Interface# ------------------------------------------
with gr.Blocks(theme=gr.themes.Soft(), title="FaceForge AI") as demo: gr.Markdown( """ # π¨ FaceForge AI ### GPU-Accelerated Professional Headshot & Avatar Generator Upload your photo and generate professional headshots, passport photos, and AI avatars instantly! """ ) with gr.Row(): with gr.Column(): input_image = gr.Image(type="pil", label="π· Upload Your Photo") process_btn = gr.Button("β¨ Generate All Images", variant="primary", size="lg") with gr.Column(): gr.Markdown("### Results") with gr.Row(): output_headshot = gr.Image(label="πΌ Professional Headshot", type="pil") output_passport = gr.Image(label="π Passport Photo", type="pil") output_avatar = gr.Image(label="π AI Avatar", type="pil") # Process button process_btn.click( fn=process_all, inputs=input_image, outputs=[output_headshot, output_passport, output_avatar] ) # Examples gr.Examples( examples=[], # Add example image paths if available inputs=input_image ) gr.Markdown( """ --- ### Features - πΌ **Professional Headshots**: Perfect for LinkedIn and business profiles - π **Passport Photos**: Standard 600x600px with white background - π **AI Avatars**: Stylized versions for social media - β‘ **GPU-Accelerated**: Fast processing with GFPGAN enhancement Β© 2025 Vijay S. Chaudhari | Powered by ZeroGPU π """ )
# Launchif __name__ == "__main__": demo.queue(max_size=20) demo.launch()137 unmodified lines13813914014114214314414514614714814915015115215315415515615715815916015 unmodified lines176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212137 unmodified lines@spaces.GPUdef create_avatar(img: Image.Image) -> Image.Image:"""Stylized AI avatar"""# Enhance faceimg_enhanced = enhance_face(img)# Resize for SD (512x512)img_resized = img_enhanced.convert("RGB").resize((512, 512))# Stylize with SD prompt#prompt = "highly detailed, digital portrait, professional lighting, cinematic style, artistic AI avatar"#prompt = "stylized yet realistic portrait, balanced lighting, subtle gradient background, sharp focus on face"#prompt = "studio portrait, even lighting, neutral background, realistic skin, confident pose"prompt = "realistic professional headshot, soft studio lighting, neutral background, crisp details, natural skin tone"with torch.autocast("cuda"):result = sd_pipe(prompt=prompt, image=img_resized, strength=0.4, guidance_scale=5.0)avatar = result.images[0]15 unmodified linesgr.Markdown("""# π¨ FaceForge AI### GPU-Accelerated Professional Headshot & Avatar GeneratorUpload your photo and generate professional headshots, passport photos, and AI avatars instantly!""")with gr.Row():with gr.Column():input_image = gr.Image(type="pil", label="π· Upload Your Photo")process_btn = gr.Button("β¨ Generate All Images", variant="primary", size="lg")with gr.Column():gr.Markdown("### Results")with gr.Row():output_headshot = gr.Image(label="πΌ Professional Headshot", type="pil")output_passport = gr.Image(label="π Passport Photo", type="pil")output_avatar = gr.Image(label="π AI Avatar", type="pil")# Process buttonprocess_btn.click(fn=process_all,inputs=input_image,outputs=[output_headshot, output_passport, output_avatar])# Examplesgr.Examples(examples=[], # Add example image paths if availableinputs=input_image)gr.Markdown("""---137 unmodified lines13813914014114214314414514614714814915015115215315415515615715815 unmodified lines174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253137 unmodified lines@spaces.GPUdef create_avatar(img: Image.Image) -> Image.Image:"""Stylized AI avatar using Stable Diffusion Img2Img with user inputs"""# Enhance faceimg_enhanced = enhance_face(img)# Resize for SD (512x512)img_resized = img_enhanced.convert("RGB").resize((512, 512))# Stylize with SD prompt. We are selecting these from UI now.#prompt = "highly detailed, digital portrait, professional lighting, cinematic style, artistic AI avatar"#prompt = "stylized yet realistic portrait, balanced lighting, subtle gradient background, sharp focus on face"#prompt = "studio portrait, even lighting, neutral background, realistic skin, confident pose"#prompt = "realistic professional headshot, soft studio lighting, neutral background, crisp details, natural skin tone"with torch.autocast("cuda"):result = sd_pipe(prompt=prompt, image=img_resized, strength=strength, guidance_scale=guidance_scale)avatar = result.images[0]15 unmodified linesgr.Markdown("""# π¨ FaceForge AI### GPU-Accelerated Professional Headshot & Avatar GeneratorUpload your photo and choose or customize how your AI avatar is generated.""")# --- Define a mapping: Short Label -> Full Prompt Text ---PROMPT_MAP = {"π¬ Cinematic Portrait": "highly detailed, digital portrait, professional lighting, cinematic style, artistic AI avatar","π¨ Stylized Realism": "stylized yet realistic portrait, balanced lighting, subtle gradient background, sharp focus on face","π’ Studio Professional": "studio portrait, even lighting, neutral background, realistic skin, confident pose","π€΅ Natural Headshot": "realistic professional headshot, soft studio lighting, neutral background, crisp details, natural skin tone"}with gr.Row():with gr.Column(scale=1):input_image = gr.Image(type="pil", label="π· Upload Your Photo")gr.Markdown("### βοΈ Avatar Generation Settings")# Dropdown shows short labels onlypreset_prompt = gr.Dropdown(label="π¨ Choose Avatar Style Preset",choices=list(PROMPT_MAP.keys()),value="π€΅ Natural Headshot")# Optional custom prompt box for flexibilitycustom_prompt = gr.Textbox(label="βοΈ Custom Prompt (optional)",placeholder="Enter your own prompt or leave blank to use preset...",lines=2)strength_slider = gr.Slider(label="ποΈ Style Strength (0.0 = keep original, 1.0 = full restyle)",minimum=0.1,maximum=1.0,value=0.45,step=0.05)guidance_slider = gr.Slider(label="π― Prompt Guidance Scale (higher = more prompt influence)",minimum=1.0,maximum=10.0,value=5.5,step=0.5)process_btn = gr.Button("β¨ Generate All Images", variant="primary", size="lg")with gr.Column(scale=1):gr.Markdown("### Results")output_headshot = gr.Image(label="πΌ Professional Headshot", type="pil")output_passport = gr.Image(label="π Passport Photo", type="pil")output_avatar = gr.Image(label="π AI Avatar", type="pil")# --- Updated process function mapping with label β full prompt translation ---def process_all_with_params(img, preset_label, custom, strength, guidance):# Map the selected label to its full prompt textpreset_prompt = PROMPT_MAP[preset_label]# Use custom prompt if provided, otherwise fallback to presetfinal_prompt = custom.strip() if custom and custom.strip() != "" else preset_promptheadshot = create_headshot(img)passport = create_passport(img)avatar = create_avatar(img, final_prompt, strength, guidance)return headshot, passport, avatarprocess_btn.click(fn=process_all_with_params,inputs=[input_image, preset_prompt, custom_prompt, strength_slider, guidance_slider],outputs=[output_headshot, output_passport, output_avatar])gr.Markdown("""---
1234567891011121314import gradio as grimport spacesimport torch
zero = torch.Tensor([0]).cuda()print(zero.device) # <-- 'cpu' π€
@spaces.GPUdef greet(n): print(zero.device) # <-- 'cuda:0' π€ return f"Hello {zero + n} Tensor"
demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())demo.launch()12345678910111213141516171819202122232425262728293031323334# ---------- Core AI / Diffusion Stack ----------opencv-python-headless==4.9.0.80torch==2.2.1torchvision==0.17.1torchaudio==2.2.1diffusers==0.27.2transformers==4.39.0accelerate==0.28.0safetensors==0.4.2
# ---------- Image Enhancement / Face Models ----------gfpgan==1.3.8realesrgan==0.3.0basicsr==1.4.2facexlib==0.3.0
# ---------- Utility / Image Processing ----------opencv-python-headless==4.9.0.80rembg==2.0.57Pillow==10.2.0numpy==1.26.4torchvision==0.17.1torch==2.2.1torchaudio==2.2.1
# ---------- Gradio / Hugging Face Runtime ----------gradio==4.39.0spaces==0.24.0huggingface-hub==0.24.0
# ---------- Optional Stability / Safety ----------einops==0.7.0timm==0.9.12
app copy.pyAdded+226
app.py+68-27
app.py.origDeleted-14
requirement.txt.bkDeleted-34