Update app.py
Browse files
app.py
CHANGED
|
@@ -2,62 +2,73 @@ import torch
|
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionPipeline, StableDiffusionInpaintPipeline
|
| 4 |
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
from huggingface_hub import login
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Check device
|
| 13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
).to(device)
|
| 27 |
|
| 28 |
-
# Image
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Gradio UI
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
gr.Markdown("## π¨ AI Image Generator & Editor")
|
| 47 |
|
| 48 |
with gr.Tab("πΌοΈ Generate Image from Text"):
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
gen_output = gr.Image(label="Generated Image")
|
| 52 |
-
gen_btn.
|
|
|
|
| 53 |
|
| 54 |
-
with gr.Tab("πͺ Manipulate
|
| 55 |
-
|
|
|
|
| 56 |
img_input = gr.Image(label="Upload Image", type="pil", tool="editor")
|
| 57 |
-
mask_input = gr.Image(label="Draw Mask
|
| 58 |
-
|
| 59 |
-
status = gr.Textbox(label="Status")
|
| 60 |
edited_img = gr.Image(label="Edited Image")
|
| 61 |
-
edit_btn
|
|
|
|
| 62 |
|
| 63 |
-
demo.launch(
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from diffusers import StableDiffusionPipeline, StableDiffusionInpaintPipeline
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
from huggingface_hub import login
|
| 6 |
|
| 7 |
+
# Globals
|
| 8 |
+
text2img_pipe = None
|
| 9 |
+
inpaint_pipe = None
|
|
|
|
|
|
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
+
# Text-to-image generation
|
| 13 |
+
def generate_image(hf_token, prompt):
|
| 14 |
+
global text2img_pipe
|
| 15 |
+
try:
|
| 16 |
+
if text2img_pipe is None:
|
| 17 |
+
login(hf_token)
|
| 18 |
+
text2img_pipe = StableDiffusionPipeline.from_pretrained(
|
| 19 |
+
"stabilityai/stable-diffusion-2-1",
|
| 20 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 21 |
+
use_auth_token=hf_token
|
| 22 |
+
).to(device)
|
| 23 |
|
| 24 |
+
image = text2img_pipe(prompt).images[0]
|
| 25 |
+
return "β
Image generated", image
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"β Error: {str(e)}", None
|
|
|
|
| 28 |
|
| 29 |
+
# Image manipulation (inpainting)
|
| 30 |
+
def manipulate_image(hf_token, prompt, image, mask):
|
| 31 |
+
global inpaint_pipe
|
| 32 |
+
try:
|
| 33 |
+
if image is None or mask is None:
|
| 34 |
+
return "β οΈ Please upload both image and mask", None
|
| 35 |
|
| 36 |
+
if inpaint_pipe is None:
|
| 37 |
+
login(hf_token)
|
| 38 |
+
inpaint_pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 39 |
+
"stable-diffusion-v1-5/stable-diffusion-inpainting",
|
| 40 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 41 |
+
use_auth_token=hf_token
|
| 42 |
+
).to(device)
|
| 43 |
|
| 44 |
+
image = image.convert("RGB").resize((512, 512))
|
| 45 |
+
mask = mask.convert("L").resize((512, 512))
|
| 46 |
|
| 47 |
+
result = inpaint_pipe(prompt=prompt, image=image, mask_image=mask).images[0]
|
| 48 |
+
return "β
Image updated", result
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"β Error: {str(e)}", None
|
| 51 |
|
| 52 |
# Gradio UI
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
+
gr.Markdown("## π¨ AI Image Generator & Editor (Stable Diffusion)")
|
| 55 |
|
| 56 |
with gr.Tab("πΌοΈ Generate Image from Text"):
|
| 57 |
+
token1 = gr.Textbox(label="π Hugging Face Token", type="password")
|
| 58 |
+
prompt1 = gr.Textbox(label="Prompt (e.g., 'a sunset over mountains')")
|
| 59 |
+
status1 = gr.Textbox(label="Status")
|
| 60 |
gen_output = gr.Image(label="Generated Image")
|
| 61 |
+
gen_btn = gr.Button("Generate Image")
|
| 62 |
+
gen_btn.click(fn=generate_image, inputs=[token1, prompt1], outputs=[status1, gen_output])
|
| 63 |
|
| 64 |
+
with gr.Tab("πͺ Manipulate Uploaded Image"):
|
| 65 |
+
token2 = gr.Textbox(label="π Hugging Face Token", type="password")
|
| 66 |
+
prompt2 = gr.Textbox(label="Edit Prompt (e.g., 'remove the background')")
|
| 67 |
img_input = gr.Image(label="Upload Image", type="pil", tool="editor")
|
| 68 |
+
mask_input = gr.Image(label="Draw Mask", type="pil", tool="sketch")
|
| 69 |
+
status2 = gr.Textbox(label="Status")
|
|
|
|
| 70 |
edited_img = gr.Image(label="Edited Image")
|
| 71 |
+
edit_btn = gr.Button("Apply Edit")
|
| 72 |
+
edit_btn.click(fn=manipulate_image, inputs=[token2, prompt2, img_input, mask_input], outputs=[status2, edited_img])
|
| 73 |
|
| 74 |
+
demo.launch()
|