MaxMilan1
commited on
Commit
·
86ea5fd
1
Parent(s):
d778d19
changeees
Browse files- app.py +24 -3
- util/text_img.py +20 -5
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
from util.instantmesh import generate_mvs, make3d, preprocess, check_input_image
|
| 5 |
-
from util.text_img import
|
| 6 |
|
| 7 |
_CITE_ = r"""
|
| 8 |
```bibtex
|
|
@@ -24,6 +24,27 @@ theme = gr.themes.Soft(
|
|
| 24 |
|
| 25 |
|
| 26 |
with gr.Blocks(theme=theme) as GenDemo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
with gr.Tab("Text to Image Generator"):
|
| 28 |
with gr.Row(variant="panel"):
|
| 29 |
with gr.Column():
|
|
@@ -39,10 +60,10 @@ with gr.Blocks(theme=theme) as GenDemo:
|
|
| 39 |
cache_examples=False,
|
| 40 |
)
|
| 41 |
with gr.Column():
|
| 42 |
-
|
| 43 |
gen_image = gr.Image(label="Generated Image", image_mode="RGBA", type='pil', show_download_button=True, show_label=False)
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
with gr.Tab("Image to 3D Model Generator"):
|
| 48 |
with gr.Row(variant="panel"):
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
from util.instantmesh import generate_mvs, make3d, preprocess, check_input_image
|
| 5 |
+
from util.text_img import generate_txttoimg, check_prompt, generate_imgtoimg
|
| 6 |
|
| 7 |
_CITE_ = r"""
|
| 8 |
```bibtex
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
with gr.Blocks(theme=theme) as GenDemo:
|
| 27 |
+
with gr.Tab("Image to Image Generator"):
|
| 28 |
+
button_choice = gr.Radio(label="Choose a model", choices=["Text to Image", "Image to Image"], default="Text to Image")
|
| 29 |
+
with gr.Row(variant="panel"):
|
| 30 |
+
with gr.Column():
|
| 31 |
+
prompt = gr.Textbox(label="Enter a discription of a shoe")
|
| 32 |
+
image = gr.Image(label="Enter an image of a shoe, that you want to use as a reference", type='pil')
|
| 33 |
+
strength = gr.Slider(label="Strength", minimum=0.1, maximum=1.0, value=0.5, step=0.1)
|
| 34 |
+
gr.Examples(
|
| 35 |
+
examples=[
|
| 36 |
+
os.path.join("examples", img_name) for img_name in sorted(os.listdir("examples"))
|
| 37 |
+
],
|
| 38 |
+
inputs=[image],
|
| 39 |
+
label="Examples",
|
| 40 |
+
cache_examples=False,
|
| 41 |
+
)
|
| 42 |
+
with gr.Column():
|
| 43 |
+
button_img = gr.Button("Generate Image", elem_id="generateIm", variant="primary")
|
| 44 |
+
gen_image = gr.Image(label="Generated Image", image_mode="RGBA", type='pil', show_download_button=True, show_label=False)
|
| 45 |
+
|
| 46 |
+
button_img.click(check_prompt, inputs=[prompt]).success(generate_imgtoimg, inputs=[prompt, image, strength], outputs=[gen_image])
|
| 47 |
+
|
| 48 |
with gr.Tab("Text to Image Generator"):
|
| 49 |
with gr.Row(variant="panel"):
|
| 50 |
with gr.Column():
|
|
|
|
| 60 |
cache_examples=False,
|
| 61 |
)
|
| 62 |
with gr.Column():
|
| 63 |
+
button_txt = gr.Button("Generate Image", elem_id="generateIm", variant="primary")
|
| 64 |
gen_image = gr.Image(label="Generated Image", image_mode="RGBA", type='pil', show_download_button=True, show_label=False)
|
| 65 |
|
| 66 |
+
button_txt.click(check_prompt, inputs=[prompt]).success(generate_txttoimg, inputs=[prompt, controlNet_image, select], outputs=[gen_image])
|
| 67 |
|
| 68 |
with gr.Tab("Image to 3D Model Generator"):
|
| 69 |
with gr.Row(variant="panel"):
|
util/text_img.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import spaces
|
| 2 |
import rembg
|
| 3 |
import torch
|
| 4 |
-
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel,
|
| 5 |
import cv2
|
| 6 |
from transformers import pipeline
|
| 7 |
import numpy as np
|
|
@@ -14,6 +14,10 @@ import gradio as gr
|
|
| 14 |
def check_prompt(prompt):
|
| 15 |
if prompt is None:
|
| 16 |
raise gr.Error("Please enter a prompt!")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
controlNet_normal = ControlNetModel.from_pretrained(
|
| 19 |
"fusing/stable-diffusion-v1-5-controlnet-normal",
|
|
@@ -30,30 +34,41 @@ controlNet_MAP = {"Normal": controlNet_normal, "Depth": controlNet_depth}
|
|
| 30 |
|
| 31 |
# Function to generate an image from text using diffusion
|
| 32 |
@spaces.GPU
|
| 33 |
-
def
|
| 34 |
prompt += "no background, side view, minimalist shot, single shoe, no legs, product photo"
|
| 35 |
|
| 36 |
-
|
| 37 |
"runwayml/stable-diffusion-v1-5",
|
| 38 |
controlnet=controlNet_MAP[controlnet],
|
| 39 |
torch_dtype=torch.float16,
|
| 40 |
safety_checker = None
|
| 41 |
)
|
| 42 |
|
| 43 |
-
|
| 44 |
|
| 45 |
if controlnet == "Normal":
|
| 46 |
control_image = get_normal(control_image)
|
| 47 |
elif controlnet == "Depth":
|
| 48 |
control_image = get_depth(control_image)
|
| 49 |
|
| 50 |
-
image =
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
image2 = rembg.remove(image)
|
| 53 |
|
| 54 |
return image2
|
| 55 |
|
| 56 |
|
|
|
|
| 57 |
def get_normal(image):
|
| 58 |
depth_estimator = pipeline("depth-estimation", model ="Intel/dpt-hybrid-midas" )
|
| 59 |
|
|
|
|
| 1 |
import spaces
|
| 2 |
import rembg
|
| 3 |
import torch
|
| 4 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, AutoPipelineForImage2Image
|
| 5 |
import cv2
|
| 6 |
from transformers import pipeline
|
| 7 |
import numpy as np
|
|
|
|
| 14 |
def check_prompt(prompt):
|
| 15 |
if prompt is None:
|
| 16 |
raise gr.Error("Please enter a prompt!")
|
| 17 |
+
|
| 18 |
+
imagepipe = AutoPipelineForImage2Image.from_pretrained(
|
| 19 |
+
"stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
|
| 20 |
+
)
|
| 21 |
|
| 22 |
controlNet_normal = ControlNetModel.from_pretrained(
|
| 23 |
"fusing/stable-diffusion-v1-5-controlnet-normal",
|
|
|
|
| 34 |
|
| 35 |
# Function to generate an image from text using diffusion
|
| 36 |
@spaces.GPU
|
| 37 |
+
def generate_txttoimg(prompt, control_image, controlnet):
|
| 38 |
prompt += "no background, side view, minimalist shot, single shoe, no legs, product photo"
|
| 39 |
|
| 40 |
+
textpipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 41 |
"runwayml/stable-diffusion-v1-5",
|
| 42 |
controlnet=controlNet_MAP[controlnet],
|
| 43 |
torch_dtype=torch.float16,
|
| 44 |
safety_checker = None
|
| 45 |
)
|
| 46 |
|
| 47 |
+
textpipe.to("cuda")
|
| 48 |
|
| 49 |
if controlnet == "Normal":
|
| 50 |
control_image = get_normal(control_image)
|
| 51 |
elif controlnet == "Depth":
|
| 52 |
control_image = get_depth(control_image)
|
| 53 |
|
| 54 |
+
image = textpipe(prompt, image=control_image).images[0]
|
| 55 |
+
|
| 56 |
+
image2 = rembg.remove(image)
|
| 57 |
+
|
| 58 |
+
return image2
|
| 59 |
|
| 60 |
+
@spaces.GPU
|
| 61 |
+
def generate_imgtoimg(prompt, image, strength=0.5):
|
| 62 |
+
prompt += "no background, side view, minimalist shot, single shoe, no legs, product photo"
|
| 63 |
+
|
| 64 |
+
image = pipeline(prompt, image=image, strength=strength).images[0]
|
| 65 |
+
|
| 66 |
image2 = rembg.remove(image)
|
| 67 |
|
| 68 |
return image2
|
| 69 |
|
| 70 |
|
| 71 |
+
|
| 72 |
def get_normal(image):
|
| 73 |
depth_estimator = pipeline("depth-estimation", model ="Intel/dpt-hybrid-midas" )
|
| 74 |
|