editor
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import gradio as gr
|
|
| 5 |
import grpc
|
| 6 |
from PIL import Image
|
| 7 |
from cachetools import LRUCache
|
| 8 |
-
import
|
| 9 |
|
| 10 |
from inference_pb2 import GuideAndRescaleRequest, GuideAndRescaleResponse
|
| 11 |
from inference_pb2_grpc import GuideAndRescaleServiceStub
|
|
@@ -16,7 +16,7 @@ def get_bytes(img):
|
|
| 16 |
return img
|
| 17 |
|
| 18 |
buffered = BytesIO()
|
| 19 |
-
img.save(buffered, format="
|
| 20 |
return buffered.getvalue()
|
| 21 |
|
| 22 |
|
|
@@ -25,24 +25,27 @@ def bytes_to_image(image: bytes) -> Image.Image:
|
|
| 25 |
return image
|
| 26 |
|
| 27 |
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
img = img.resize((512, 512), Image.Resampling.LANCZOS)
|
| 31 |
|
| 32 |
-
return img
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def edit(image, source_prompt, target_prompt, config, progress=gr.Progress(track_tqdm=True)):
|
| 36 |
if not image or not source_prompt or not target_prompt:
|
| 37 |
raise ValueError("Need to upload an image and enter init and edit prompts")
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
image_bytes = get_bytes(image)
|
| 40 |
with grpc.insecure_channel(os.environ['SERVER']) as channel:
|
| 41 |
stub = GuideAndRescaleServiceStub(channel)
|
| 42 |
|
| 43 |
output: GuideAndRescaleResponse = stub.swap(
|
| 44 |
GuideAndRescaleRequest(image=image_bytes, source_prompt=source_prompt, target_prompt=target_prompt,
|
| 45 |
-
|
| 46 |
)
|
| 47 |
|
| 48 |
output = bytes_to_image(output.image)
|
|
@@ -55,29 +58,35 @@ def get_demo():
|
|
| 55 |
gr.Markdown(
|
| 56 |
'<div style="display: flex; align-items: center; gap: 10px;">'
|
| 57 |
'<span>Official Guide-and-Rescale Gradio demo:</span>'
|
| 58 |
-
|
|
|
|
| 59 |
'<a href="https://colab.research.google.com/drive/1noKOOcDBBL_m5_UqU15jBBqiM8piLZ1O?usp=sharing"><img src="https://colab.research.google.com/assets/colab-badge.svg" height=22.5></a>'
|
| 60 |
'</div>'
|
| 61 |
)
|
| 62 |
with gr.Row():
|
| 63 |
with gr.Column():
|
| 64 |
with gr.Row():
|
| 65 |
-
image = gr.
|
|
|
|
| 66 |
with gr.Row():
|
| 67 |
source_prompt = gr.Textbox(label="Init Prompt", info="Describs the content on the original image.")
|
| 68 |
-
target_prompt = gr.Textbox(label="Edit Prompt",
|
|
|
|
| 69 |
config = gr.Radio(["non-stylisation", "stylisation"], value='non-stylisation',
|
| 70 |
-
|
| 71 |
with gr.Row():
|
| 72 |
btn = gr.Button("Edit image")
|
| 73 |
with gr.Column():
|
| 74 |
with gr.Row():
|
| 75 |
output = gr.Image(label="Result: edited image")
|
| 76 |
|
| 77 |
-
gr.Examples(examples=[["input/1.png", 'A photo of a tiger', 'A photo of a lion', 'non-stylisation'],
|
|
|
|
|
|
|
|
|
|
| 78 |
outputs=output)
|
| 79 |
|
| 80 |
-
image.upload(
|
| 81 |
|
| 82 |
btn.click(fn=edit, inputs=[image, source_prompt, target_prompt, config], outputs=output)
|
| 83 |
|
|
|
|
| 5 |
import grpc
|
| 6 |
from PIL import Image
|
| 7 |
from cachetools import LRUCache
|
| 8 |
+
from gradio.image_utils import crop_scale
|
| 9 |
|
| 10 |
from inference_pb2 import GuideAndRescaleRequest, GuideAndRescaleResponse
|
| 11 |
from inference_pb2_grpc import GuideAndRescaleServiceStub
|
|
|
|
| 16 |
return img
|
| 17 |
|
| 18 |
buffered = BytesIO()
|
| 19 |
+
img.save(buffered, format="PNG")
|
| 20 |
return buffered.getvalue()
|
| 21 |
|
| 22 |
|
|
|
|
| 25 |
return image
|
| 26 |
|
| 27 |
|
| 28 |
+
def edit(editor, source_prompt, target_prompt, config, progress=gr.Progress(track_tqdm=True)):
|
| 29 |
+
image = editor['layers'][0]
|
|
|
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if not image or not source_prompt or not target_prompt:
|
| 32 |
raise ValueError("Need to upload an image and enter init and edit prompts")
|
| 33 |
|
| 34 |
+
width, height = image.size
|
| 35 |
+
if width != height:
|
| 36 |
+
size = min(width, height)
|
| 37 |
+
image = crop_scale(image, size, size)
|
| 38 |
+
|
| 39 |
+
if image.size != (512, 512):
|
| 40 |
+
image = image.resize((512, 512), Image.Resampling.LANCZOS)
|
| 41 |
+
|
| 42 |
image_bytes = get_bytes(image)
|
| 43 |
with grpc.insecure_channel(os.environ['SERVER']) as channel:
|
| 44 |
stub = GuideAndRescaleServiceStub(channel)
|
| 45 |
|
| 46 |
output: GuideAndRescaleResponse = stub.swap(
|
| 47 |
GuideAndRescaleRequest(image=image_bytes, source_prompt=source_prompt, target_prompt=target_prompt,
|
| 48 |
+
config=config, use_cache=True)
|
| 49 |
)
|
| 50 |
|
| 51 |
output = bytes_to_image(output.image)
|
|
|
|
| 58 |
gr.Markdown(
|
| 59 |
'<div style="display: flex; align-items: center; gap: 10px;">'
|
| 60 |
'<span>Official Guide-and-Rescale Gradio demo:</span>'
|
| 61 |
+
'<a href=""><img src="https://img.shields.io/badge/arXiv-2408.12345-b31b1b.svg" height=22.5></a>'
|
| 62 |
+
'<a href="https://github.com/AIRI-Institute/Guide-and-Rescale"><img src="https://img.shields.io/badge/github-%23121011.svg?style=for-the-badge&logo=github&logoColor=white" height=22.5></a>'
|
| 63 |
'<a href="https://colab.research.google.com/drive/1noKOOcDBBL_m5_UqU15jBBqiM8piLZ1O?usp=sharing"><img src="https://colab.research.google.com/assets/colab-badge.svg" height=22.5></a>'
|
| 64 |
'</div>'
|
| 65 |
)
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column():
|
| 68 |
with gr.Row():
|
| 69 |
+
image = gr.ImageEditor(label="Image that you want to edit", type="pil", layers=False,
|
| 70 |
+
interactive=True, crop_size="1:1", eraser=False, brush=False)
|
| 71 |
with gr.Row():
|
| 72 |
source_prompt = gr.Textbox(label="Init Prompt", info="Describs the content on the original image.")
|
| 73 |
+
target_prompt = gr.Textbox(label="Edit Prompt",
|
| 74 |
+
info="Describs what is expected in the output image.")
|
| 75 |
config = gr.Radio(["non-stylisation", "stylisation"], value='non-stylisation',
|
| 76 |
+
label="Type of Editing", info="Selects a config for editing.")
|
| 77 |
with gr.Row():
|
| 78 |
btn = gr.Button("Edit image")
|
| 79 |
with gr.Column():
|
| 80 |
with gr.Row():
|
| 81 |
output = gr.Image(label="Result: edited image")
|
| 82 |
|
| 83 |
+
gr.Examples(examples=[["input/1.png", 'A photo of a tiger', 'A photo of a lion', 'non-stylisation'],
|
| 84 |
+
["input/zebra.jpeg", 'A photo of a zebra', 'A photo of a white horse', 'non-stylisation'],
|
| 85 |
+
["input/13.png", 'A photo', 'Anime style face', 'stylisation']],
|
| 86 |
+
inputs=[image, source_prompt, target_prompt, config],
|
| 87 |
outputs=output)
|
| 88 |
|
| 89 |
+
image.upload(inputs=[image], outputs=image)
|
| 90 |
|
| 91 |
btn.click(fn=edit, inputs=[image, source_prompt, target_prompt, config], outputs=output)
|
| 92 |
|