Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from PIL import Image
|
3 |
+
import gradio as gr
|
4 |
+
from diffusers import StableDiffusionImg2ImgPipeline
|
5 |
+
|
6 |
+
# Load the Stable Diffusion img2img pipeline
|
7 |
+
device = "cuda"
|
8 |
+
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
|
9 |
+
"runwayml/stable-diffusion-v1-5",
|
10 |
+
torch_dtype=torch.float16,
|
11 |
+
).to(device)
|
12 |
+
pipe.enable_attention_slicing()
|
13 |
+
|
14 |
+
# Predefined prompts
|
15 |
+
PROMPTS = {
|
16 |
+
"Photorealistic": "A heavily corroded metal pipeline stretching across the ocean floor, covered in algae and barnacles, deep underwater with ambient blue lighting and floating particles, photorealistic",
|
17 |
+
"Cinematic": "An old rusted pipeline submerged in the sea, encrusted with marine growth and decay, surrounded by dark water and shafts of light from the surface, cinematic, moody atmosphere"
|
18 |
+
}
|
19 |
+
|
20 |
+
# Inference function
|
21 |
+
def generate_image(init_image, prompt_choice, strength, guidance_scale):
|
22 |
+
# Resize and convert the input image
|
23 |
+
init_image = init_image.convert("RGB").resize((768, 512))
|
24 |
+
|
25 |
+
# Get the selected prompt
|
26 |
+
prompt = PROMPTS[prompt_choice]
|
27 |
+
|
28 |
+
# Run the pipeline
|
29 |
+
result = pipe(
|
30 |
+
prompt=prompt,
|
31 |
+
image=init_image,
|
32 |
+
strength=strength,
|
33 |
+
guidance_scale=guidance_scale
|
34 |
+
).images[0]
|
35 |
+
|
36 |
+
return result
|
37 |
+
|
38 |
+
# Gradio interface
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
gr.Markdown("# 🦑 Corroded Pipeline Generator - Underwater Img2Img")
|
41 |
+
with gr.Row():
|
42 |
+
with gr.Column():
|
43 |
+
init_image = gr.Image(label="Upload Initial Image", type="pil")
|
44 |
+
prompt_choice = gr.Radio(choices=list(PROMPTS.keys()), label="Select Prompt", value="Photorealistic")
|
45 |
+
strength = gr.Slider(minimum=0.2, maximum=1.0, value=0.75, step=0.05, label="Transformation Strength")
|
46 |
+
guidance_scale = gr.Slider(minimum=1, maximum=15, value=7.5, step=0.5, label="Prompt Guidance Scale")
|
47 |
+
generate_btn = gr.Button("Generate")
|
48 |
+
with gr.Column():
|
49 |
+
output_image = gr.Image(label="Generated Image")
|
50 |
+
|
51 |
+
generate_btn.click(fn=generate_image, inputs=[init_image, prompt_choice, strength, guidance_scale], outputs=output_image)
|
52 |
+
|
53 |
+
# Launch the app
|
54 |
+
demo.launch()
|