Spaces:
Runtime error
Runtime error
Sophia Yang
commited on
Commit
•
ab316d0
1
Parent(s):
bf9e12c
inital commit
Browse files- Dockerfile +12 -0
- app.py +105 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin",
|
12 |
+
"sophiamyang-panel-InstructPix2Pix.hf.space", "--allow-websocket-origin", "0.0.0.0:7860"]
|
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
|
3 |
+
import hvplot.pandas
|
4 |
+
import numpy as np
|
5 |
+
import panel as pn
|
6 |
+
import param
|
7 |
+
import PIL
|
8 |
+
import requests
|
9 |
+
import torch
|
10 |
+
|
11 |
+
from diffusers import StableDiffusionInstructPix2PixPipeline
|
12 |
+
|
13 |
+
pn.extension(template="bootstrap")
|
14 |
+
pn.state.template.main_max_width = "690px"
|
15 |
+
pn.state.template.accent_base_color = "#F08080"
|
16 |
+
pn.state.template.header_background = "#F08080"
|
17 |
+
|
18 |
+
|
19 |
+
# Model
|
20 |
+
model_id = "timbrooks/instruct-pix2pix"
|
21 |
+
pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained(
|
22 |
+
model_id, torch_dtype=torch.float16
|
23 |
+
).to("cuda")
|
24 |
+
|
25 |
+
|
26 |
+
def new_image(prompt, image, img_guidance, guidance, steps):
|
27 |
+
edit = pipe(
|
28 |
+
prompt,
|
29 |
+
image=image,
|
30 |
+
image_guidance_scale=img_guidance,
|
31 |
+
guidance_scale=guidance,
|
32 |
+
num_inference_steps=steps,
|
33 |
+
).images[0]
|
34 |
+
return edit
|
35 |
+
|
36 |
+
|
37 |
+
# Panel widgets
|
38 |
+
file_input = pn.widgets.FileInput(width=600)
|
39 |
+
prompt = pn.widgets.TextInput(
|
40 |
+
value="", placeholder="Enter image editing instruction here...", width=600
|
41 |
+
)
|
42 |
+
img_guidance = pn.widgets.DiscreteSlider(
|
43 |
+
name="Image guidance scale", options=list(np.arange(1, 10.5, 0.5)), value=1.5
|
44 |
+
)
|
45 |
+
guidance = pn.widgets.DiscreteSlider(
|
46 |
+
name="Guidance scale", options=list(np.arange(1, 10.5, 0.5)), value=7
|
47 |
+
)
|
48 |
+
steps = pn.widgets.IntSlider(name="Inference Steps", start=1, end=100, step=1, value=20)
|
49 |
+
run_button = pn.widgets.Button(name="Run!", width=600)
|
50 |
+
|
51 |
+
|
52 |
+
# define global variables to keep track of things
|
53 |
+
convos = [] # store all panel objects in a list
|
54 |
+
image = None
|
55 |
+
filename = None
|
56 |
+
|
57 |
+
|
58 |
+
def normalize_image(value, width):
|
59 |
+
"""
|
60 |
+
normalize image to RBG channels and to the same size
|
61 |
+
"""
|
62 |
+
b = io.BytesIO(value)
|
63 |
+
image = PIL.Image.open(b).convert("RGB")
|
64 |
+
aspect = image.size[1] / image.size[0]
|
65 |
+
height = int(aspect * width)
|
66 |
+
return image.resize((width, height), PIL.Image.ANTIALIAS)
|
67 |
+
|
68 |
+
|
69 |
+
def get_conversations(_, img, img_guidance, guidance, steps, width=600):
|
70 |
+
"""
|
71 |
+
Get all the conversations in a Panel object
|
72 |
+
"""
|
73 |
+
global image, filename
|
74 |
+
prompt_text = prompt.value
|
75 |
+
prompt.value = ""
|
76 |
+
|
77 |
+
# if the filename changes, open the image again
|
78 |
+
if filename != file_input.filename:
|
79 |
+
filename = file_input.filename
|
80 |
+
image = normalize_image(file_input.value, width)
|
81 |
+
convos.clear()
|
82 |
+
|
83 |
+
if prompt_text:
|
84 |
+
# generate new image
|
85 |
+
image = new_image(prompt_text, image, img_guidance, guidance, steps)
|
86 |
+
convos.append(pn.Row("\U0001F60A", pn.pane.Markdown(prompt_text, width=600)))
|
87 |
+
convos.append(pn.Row("\U0001F916", image))
|
88 |
+
return pn.Column(*convos)
|
89 |
+
|
90 |
+
|
91 |
+
# bind widgets to functions
|
92 |
+
interactive_conversation = pn.bind(
|
93 |
+
get_conversations, run_button, file_input, img_guidance, guidance, steps
|
94 |
+
)
|
95 |
+
interactive_upload = pn.bind(pn.panel, file_input, width=600)
|
96 |
+
|
97 |
+
# layout
|
98 |
+
pn.Column(
|
99 |
+
pn.pane.Markdown("## \U0001F60A Upload an image file and start editing!"),
|
100 |
+
pn.Column(file_input, pn.panel(interactive_upload)),
|
101 |
+
pn.panel(interactive_conversation, loading_indicator=True),
|
102 |
+
prompt,
|
103 |
+
pn.Row(run_button),
|
104 |
+
pn.Card(img_guidance, guidance, steps, width=600, header="Advance settings"),
|
105 |
+
).servable(title="Stablel Diffusion InstructPix2pix Image Editing Chatbot")
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
panel
|
2 |
+
hvplot
|
3 |
+
diffusers
|
4 |
+
transformers
|
5 |
+
accelerate
|