Spaces:
Sleeping
Sleeping
File size: 9,924 Bytes
f34be1f d7c5b1f f34be1f d7c5b1f cb8152f d7c5b1f cb8152f 54ec4ec cb8152f d7c5b1f cb8152f d7c5b1f ef10721 d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f e0b1280 d7c5b1f cc10554 ef10721 d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f 3c8b597 cb8152f d7c5b1f 3c8b597 d7c5b1f 3c8b597 d7c5b1f 3c8b597 d7c5b1f 3c8b597 d7c5b1f cb8152f d7c5b1f cb8152f d7c5b1f ad5f092 cb8152f ad5f092 cb8152f f34be1f cb8152f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
import gradio as gr
import yaml
import torch
from PIL import Image
from rembg import remove
from rate_limiter import RateLimiter
from replicate_handler import ReplicateHandler
# ------------------------------------------
# Load Configuration
# ------------------------------------------
with open("config.yaml", "r") as f:
config = yaml.safe_load(f)
# Initialize modules
rate_limiter = RateLimiter(
session_file=config["rate_limit"]["session_file"],
daily_limit=config["rate_limit"]["default_daily_limit"],
dev_daily_limit=config["rate_limit"]["dev_daily_limit"]
)
replicate_handler = ReplicateHandler(
model=config["replicate"]["model"],
default_settings=config["replicate"]["default_settings"]
)
# ------------------------------------------
# Core Functions
# ------------------------------------------
def remove_background(img: Image.Image) -> Image.Image:
"""Remove background from image"""
if img is None:
raise gr.Error("Please upload an image first")
return remove(img)
def generate_headshot(
img: Image.Image,
headshot_style: str,
use_advanced: bool,
cfg: float,
steps: int,
denoise: float,
request: gr.Request
) -> Image.Image:
"""Generate professional headshot with selected style"""
# Check rate limit
allowed, remaining, reset_time = rate_limiter.check_limit(request)
if not allowed:
raise gr.Error(rate_limiter.get_limit_message(remaining, reset_time))
if img is None:
raise gr.Error("Please upload an image first")
# Get prompt from config
style_config = config["headshots"][headshot_style]
prompt = style_config["prompt"]
negative_prompt = style_config["negative"]
# Prepare settings
custom_settings = None
if use_advanced:
custom_settings = {
"cfg": cfg,
"steps": steps,
"denoise": denoise
}
# Generate
try:
result = replicate_handler.generate(
input_image=img,
prompt=prompt,
negative_prompt=negative_prompt,
custom_settings=custom_settings
)
# Increment usage
rate_limiter.increment(request)
return result
except Exception as e:
raise gr.Error(f"Generation failed: {str(e)}")
def generate_scene(
img: Image.Image,
scene_style: str,
use_advanced: bool,
cfg: float,
steps: int,
denoise: float,
request: gr.Request
) -> Image.Image:
"""Generate scene change with selected style"""
# Check rate limit
allowed, remaining, reset_time = rate_limiter.check_limit(request)
if not allowed:
raise gr.Error(rate_limiter.get_limit_message(remaining, reset_time))
if img is None:
raise gr.Error("Please upload an image first")
# Get prompt from config
style_config = config["scenes"][scene_style]
prompt = style_config["prompt"]
negative_prompt = style_config["negative"]
# Prepare settings
custom_settings = None
if use_advanced:
custom_settings = {
"cfg": cfg,
"steps": steps,
"denoise": denoise
}
# Generate
try:
result = replicate_handler.generate(
input_image=img,
prompt=prompt,
negative_prompt=negative_prompt,
custom_settings=custom_settings
)
# Increment usage
rate_limiter.increment(request)
return result
except Exception as e:
raise gr.Error(f"Generation failed: {str(e)}")
def get_rate_limit_status(request: gr.Request) -> str:
"""Get current rate limit status"""
allowed, remaining, reset_time = rate_limiter.check_limit(request)
return rate_limiter.get_limit_message(remaining, reset_time)
# ------------------------------------------
# Gradio Interface
# ------------------------------------------
with gr.Blocks(theme=gr.themes.Soft(), title="FaceForge AI") as demo:
gr.Markdown(
"""
# π¨ FaceForge AI
### AI-Powered Professional Photo Generator
Upload your photo and transform it into professional headshots or place yourself in different scenes.
"""
)
# Rate limit status
rate_status = gr.Textbox(
label="π Usage Status",
interactive=False,
value="Loading..."
)
with gr.Row():
# Left Column - Input
with gr.Column(scale=1):
input_image = gr.Image(type="pil", label="π· Upload Your Photo")
gr.Markdown("### βοΈ Advanced Settings (Optional)")
use_advanced = gr.Checkbox(
label="Enable Advanced Settings",
value=False
)
with gr.Group(visible=True) as advanced_group:
cfg_slider = gr.Slider(
label="ποΈ CFG Scale",
minimum=config["replicate"]["advanced_ranges"]["cfg"]["min"],
maximum=config["replicate"]["advanced_ranges"]["cfg"]["max"],
value=config["replicate"]["default_settings"]["cfg"],
step=config["replicate"]["advanced_ranges"]["cfg"]["step"]
)
steps_slider = gr.Slider(
label="π Steps",
minimum=config["replicate"]["advanced_ranges"]["steps"]["min"],
maximum=config["replicate"]["advanced_ranges"]["steps"]["max"],
value=config["replicate"]["default_settings"]["steps"],
step=config["replicate"]["advanced_ranges"]["steps"]["step"]
)
denoise_slider = gr.Slider(
label="β¨ Denoise",
minimum=config["replicate"]["advanced_ranges"]["denoise"]["min"],
maximum=config["replicate"]["advanced_ranges"]["denoise"]["max"],
value=config["replicate"]["default_settings"]["denoise"],
step=config["replicate"]["advanced_ranges"]["denoise"]["step"]
)
# Toggle visibility of advanced settings
use_advanced.change(
fn=lambda x: gr.update(visible=x),
inputs=[use_advanced],
outputs=[advanced_group]
)
# Right Column - Outputs
with gr.Column(scale=1):
# Background Remover Section
gr.Markdown("### π Background Remover")
output_bg_removed = gr.Image(label="Background Removed", type="pil")
btn_bg_remove = gr.Button("ποΈ Remove Background", variant="secondary")
gr.Markdown("---")
# Professional Headshots Section
gr.Markdown("### πΌ Professional Headshots")
headshot_style = gr.Dropdown(
label="Choose Headshot Style",
choices=list(config["headshots"].keys()),
value=list(config["headshots"].keys())[0]
)
output_headshot = gr.Image(label="Professional Headshot", type="pil")
btn_headshot = gr.Button("πΈ Generate Headshot", variant="primary")
gr.Markdown("---")
# Scene Changer Section
gr.Markdown("### π Scene Changer")
scene_style = gr.Dropdown(
label="Choose Scene",
choices=list(config["scenes"].keys()),
value=list(config["scenes"].keys())[0]
)
output_scene = gr.Image(label="Scene Changed", type="pil")
btn_scene = gr.Button("π¬ Change Scene", variant="primary")
# Button Actions
btn_bg_remove.click(
fn=remove_background,
inputs=[input_image],
outputs=[output_bg_removed]
)
btn_headshot.click(
fn=generate_headshot,
inputs=[
input_image,
headshot_style,
use_advanced,
cfg_slider,
steps_slider,
denoise_slider
],
outputs=[output_headshot]
)
btn_scene.click(
fn=generate_scene,
inputs=[
input_image,
scene_style,
use_advanced,
cfg_slider,
steps_slider,
denoise_slider
],
outputs=[output_scene]
)
# Update rate limit status on load and after each generation
demo.load(fn=get_rate_limit_status, inputs=None, outputs=[rate_status])
btn_headshot.click(fn=get_rate_limit_status, inputs=None, outputs=[rate_status])
btn_scene.click(fn=get_rate_limit_status, inputs=None, outputs=[rate_status])
gr.Markdown(
"""
---
### β¨ Features
- π **Background Remover**: Clean background removal for any image
- πΌ **Professional Headshots**: Studio-quality headshots with multiple styles
- π **Scene Changer**: Place yourself in different environments
- β‘ **AI-Powered**: InstantID technology for identity preservation
- π **Rate Limited**: Fair usage with daily limits
"""
)
gr.Markdown(
"""
---
### β οΈ Notice
- This is a **personal experimental project** and has undergone **limited testing**. Please use with caution.
- Although **InstantID** is designed to preserve identity, outputs may vary based on **prompts, model weights, and other influencing factors**.
Β© 2025 Vijay S. Chaudhari | Powered by Replicate, InstantID and HuggingFace π
"""
)
# Launch
if __name__ == "__main__":
demo.queue(max_size=20)
demo.launch() |