naxemCDA commited on
Commit
72dc3d9
Β·
1 Parent(s): fd58a5d

ProfileBGGen_1.0

Browse files
Files changed (2) hide show
  1. app.py +48 -0
  2. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ from rembg import remove
4
+ from PIL import Image
5
+ import torch
6
+ from diffusers import StableDiffusionPipeline
7
+
8
+ # Load the Stable Diffusion pipeline (requires GPU)
9
+ pipe = StableDiffusionPipeline.from_pretrained(
10
+ "runwayml/stable-diffusion-v1-5",
11
+ torch_dtype=torch.float16
12
+ ).to("cuda")
13
+
14
+ @spaces.GPU # Decorate the function to enable ZeroGPU dynamic GPU allocation
15
+ def replace_background(profile_img, prompt):
16
+ if profile_img is None or not prompt.strip():
17
+ return "Please upload a profile picture and enter a background prompt."
18
+
19
+ # Step 1: Remove background (keeps person only)
20
+ profile_img = profile_img.convert("RGBA")
21
+ person_only = remove(profile_img).resize((512, 512))
22
+
23
+ # Step 2: Generate new background image from prompt
24
+ background = pipe(prompt, height=512, width=512).images[0].convert("RGBA")
25
+
26
+ # Step 3: Composite person onto new background
27
+ final_image = Image.alpha_composite(background, person_only)
28
+ return final_image
29
+
30
+ with gr.Blocks() as demo:
31
+ gr.Markdown("# 🧍 AI Background Replacement App")
32
+ gr.Markdown(
33
+ "Upload a profile picture, enter a prompt like 'tropical island at sunset', and get a version with a new AI-generated background!"
34
+ )
35
+
36
+ with gr.Row():
37
+ image_input = gr.Image(label="Upload Profile Image", type="pil")
38
+ prompt_input = gr.Textbox(label="Background Prompt", placeholder="e.g., a starry night in Paris")
39
+
40
+ output_image = gr.Image(label="Result", type="pil")
41
+ generate_button = gr.Button("Generate New Background")
42
+
43
+ generate_button.click(fn=replace_background, inputs=[image_input, prompt_input], outputs=output_image)
44
+
45
+ gr.Markdown("⚠️ Works best with a portrait image and short background prompts.\nπŸš€ GPU required for best performance.")
46
+
47
+ if __name__ == "__main__":
48
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+
2
+ gradio
3
+ diffusers
4
+ transformers
5
+ rembg
6
+ torch
7
+ torchvision
8
+ onnxruntime