Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
print(f"Loading model: {MODEL_ID} on {DEVICE}")
|
| 3 |
if "inpaint" in MODEL_ID or "img2img" in MODEL_ID:
|
| 4 |
-
# اگر مدل مخصوص اینپینت باشه از InpaintPipeline استفاده کن
|
| 5 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 6 |
MODEL_ID,
|
| 7 |
revision="fp16",
|
|
@@ -23,9 +44,6 @@ return pipe
|
|
| 23 |
pipe = load_pipelines()
|
| 24 |
|
| 25 |
|
| 26 |
-
# توابع تولید / ویرایش
|
| 27 |
-
|
| 28 |
-
|
| 29 |
def generate_image(prompt: str, negative_prompt: str, steps: int, guidance: float):
|
| 30 |
if not prompt:
|
| 31 |
return None
|
|
@@ -40,9 +58,7 @@ def edit_image(init_image, mask, prompt: str, negative_prompt: str, steps: int,
|
|
| 40 |
if init_image is None:
|
| 41 |
return None
|
| 42 |
if mask is None:
|
| 43 |
-
# اگر ماسک نبود، از تصویر اولیه به عنوان ماسک استفاده نکن — کاربر باید ماسک بدهد
|
| 44 |
return None
|
| 45 |
-
# تبدیل به قالب مورد نیاز
|
| 46 |
init_img = init_image.convert("RGB")
|
| 47 |
mask_img = mask.convert("L")
|
| 48 |
with torch.autocast("cuda") if DEVICE == "cuda" else torch.no_grad():
|
|
@@ -50,20 +66,19 @@ out = pipe(prompt=prompt, image=init_img, mask_image=mask_img, guidance_scale=gu
|
|
| 50 |
return out.images[0]
|
| 51 |
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
gr.Markdown("# Prompt Image Editor — JumpLander (جامپلندر)")
|
| 56 |
with gr.Row():
|
| 57 |
with gr.Column(scale=2):
|
| 58 |
mode = gr.Radio(["Generate", "Edit / Inpaint"], value="Generate", label="Mode")
|
| 59 |
-
prompt = gr.Textbox(lines=3, label="Prompt
|
| 60 |
-
negative_prompt = gr.Textbox(lines=2, label="Negative prompt (
|
| 61 |
steps = gr.Slider(minimum=10, maximum=60, step=5, value=28, label="Steps")
|
| 62 |
guidance = gr.Slider(minimum=1.0, maximum=20.0, step=0.5, value=7.5, label="Guidance Scale")
|
| 63 |
run = gr.Button("Run")
|
| 64 |
with gr.Column(scale=3):
|
| 65 |
-
input_image = gr.Image(type="pil", label="Initial image (
|
| 66 |
-
mask_image = gr.Image(type="pil", label="Mask (
|
| 67 |
output = gr.Image(label="Output")
|
| 68 |
|
| 69 |
|
|
@@ -75,10 +90,4 @@ else:
|
|
| 75 |
return edit_image(input_image, mask_image, prompt, negative_prompt, steps, guidance)
|
| 76 |
except Exception as e:
|
| 77 |
return Image.new('RGB', (512,512), color=(255,0,0))
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
run.click(_run, inputs=[mode, prompt, negative_prompt, steps, guidance, input_image, mask_image], outputs=[output])
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
if __name__ == "__main__":
|
| 84 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# Prompt Image Editor — Hugging Face Space
|
| 3 |
+
# Minimal branding in source so the repo can be published under a subsidiary page
|
| 4 |
+
|
| 5 |
+
|
| 6 |
import os
|
| 7 |
+
import gradio as gr
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import torch
|
| 10 |
+
from diffusers import StableDiffusionPipeline, StableDiffusionInpaintPipeline
|
| 11 |
+
from transformers import logging
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
logging.set_verbosity_error()
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Environment settings (Spaces: Variables & Secrets)
|
| 18 |
+
MODEL_ID = os.getenv("MODEL_ID", "runwayml/stable-diffusion-v1-5")
|
| 19 |
+
HF_TOKEN = os.getenv("HF_API_TOKEN") # set as a Secret in your Space if required
|
| 20 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def load_pipelines():
|
| 24 |
print(f"Loading model: {MODEL_ID} on {DEVICE}")
|
| 25 |
if "inpaint" in MODEL_ID or "img2img" in MODEL_ID:
|
|
|
|
| 26 |
pipe = StableDiffusionInpaintPipeline.from_pretrained(
|
| 27 |
MODEL_ID,
|
| 28 |
revision="fp16",
|
|
|
|
| 44 |
pipe = load_pipelines()
|
| 45 |
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
def generate_image(prompt: str, negative_prompt: str, steps: int, guidance: float):
|
| 48 |
if not prompt:
|
| 49 |
return None
|
|
|
|
| 58 |
if init_image is None:
|
| 59 |
return None
|
| 60 |
if mask is None:
|
|
|
|
| 61 |
return None
|
|
|
|
| 62 |
init_img = init_image.convert("RGB")
|
| 63 |
mask_img = mask.convert("L")
|
| 64 |
with torch.autocast("cuda") if DEVICE == "cuda" else torch.no_grad():
|
|
|
|
| 66 |
return out.images[0]
|
| 67 |
|
| 68 |
|
| 69 |
+
with gr.Blocks(title="Prompt Image Editor") as demo:
|
| 70 |
+
gr.Markdown("# Prompt Image Editor")
|
|
|
|
| 71 |
with gr.Row():
|
| 72 |
with gr.Column(scale=2):
|
| 73 |
mode = gr.Radio(["Generate", "Edit / Inpaint"], value="Generate", label="Mode")
|
| 74 |
+
prompt = gr.Textbox(lines=3, label="Prompt")
|
| 75 |
+
negative_prompt = gr.Textbox(lines=2, label="Negative prompt (optional)")
|
| 76 |
steps = gr.Slider(minimum=10, maximum=60, step=5, value=28, label="Steps")
|
| 77 |
guidance = gr.Slider(minimum=1.0, maximum=20.0, step=0.5, value=7.5, label="Guidance Scale")
|
| 78 |
run = gr.Button("Run")
|
| 79 |
with gr.Column(scale=3):
|
| 80 |
+
input_image = gr.Image(type="pil", label="Initial image (for editing)")
|
| 81 |
+
mask_image = gr.Image(type="pil", label="Mask (white = edit)")
|
| 82 |
output = gr.Image(label="Output")
|
| 83 |
|
| 84 |
|
|
|
|
| 90 |
return edit_image(input_image, mask_image, prompt, negative_prompt, steps, guidance)
|
| 91 |
except Exception as e:
|
| 92 |
return Image.new('RGB', (512,512), color=(255,0,0))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
demo.launch()
|