Spaces:
Build error
Build error
Update app_dialogue.py
Browse files- app_dialogue.py +77 -26
app_dialogue.py
CHANGED
@@ -59,9 +59,16 @@ logger = logging.getLogger()
|
|
59 |
SEED = 38
|
60 |
set_seed(38)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
if image.mode == "RGB":
|
66 |
return image
|
67 |
|
@@ -69,27 +76,44 @@ def convert_to_rgb(image):
|
|
69 |
background = Image.new("RGBA", image_rgba.size, (255, 255, 255))
|
70 |
alpha_composite = Image.alpha_composite(background, image_rgba)
|
71 |
alpha_composite = alpha_composite.convert("RGB")
|
72 |
-
return alpha_composite
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
|
94 |
|
95 |
def base64_to_pil(encoded_image):
|
@@ -159,17 +183,33 @@ def isolate_images_urls(prompt_list):
|
|
159 |
]
|
160 |
```
|
161 |
"""
|
|
|
|
|
|
|
162 |
linearized_list = []
|
163 |
for prompt in prompt_list:
|
|
|
164 |
# Prompt can be either a string, or a PIL image
|
165 |
if isinstance(prompt, PIL.Image.Image):
|
|
|
|
|
|
|
|
|
|
|
166 |
linearized_list.append(prompt)
|
167 |
-
|
|
|
|
|
168 |
if "<fake_token_around_image>" not in prompt:
|
|
|
169 |
linearized_list.append(prompt)
|
|
|
170 |
else:
|
|
|
171 |
prompt_splitted = prompt.split("<fake_token_around_image>")
|
|
|
172 |
for ps in prompt_splitted:
|
|
|
173 |
if ps == "":
|
174 |
continue
|
175 |
if ps.startswith("<image:"):
|
@@ -181,6 +221,7 @@ def isolate_images_urls(prompt_list):
|
|
181 |
f"Unrecognized type for `prompt`. Got {type(type(prompt))}. Was expecting something in [`str`,"
|
182 |
" `PIL.Image.Image`]"
|
183 |
)
|
|
|
184 |
return linearized_list
|
185 |
|
186 |
|
@@ -209,17 +250,27 @@ def user_prompt_list_to_markdown(user_prompt_list: List[Union[str, PIL.Image.Ima
|
|
209 |
Convert a user prompt in the list format (i.e. elements are either a PIL image or a string) into
|
210 |
the markdown format that is used for the chatbot history and rendering.
|
211 |
"""
|
|
|
|
|
212 |
resulting_string = ""
|
213 |
for elem in user_prompt_list:
|
214 |
-
|
|
|
|
|
215 |
resulting_string += elem
|
216 |
-
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
218 |
else:
|
219 |
raise ValueError(
|
220 |
"Unknown type for `user_prompt_list`. Expected an element of type `str` or `PIL.Image.Image` and got"
|
221 |
f" `{type(elem)}`"
|
222 |
)
|
|
|
223 |
return resulting_string
|
224 |
|
225 |
|
|
|
59 |
SEED = 38
|
60 |
set_seed(38)
|
61 |
|
62 |
+
import requests
|
63 |
+
from io import BytesIO
|
64 |
+
import uuid
|
65 |
+
|
66 |
+
from PIL import Image
|
67 |
+
|
68 |
+
from PIL import Image
|
69 |
+
import tempfile
|
70 |
+
|
71 |
+
def convert_to_rgb_pil(image):
|
72 |
if image.mode == "RGB":
|
73 |
return image
|
74 |
|
|
|
76 |
background = Image.new("RGBA", image_rgba.size, (255, 255, 255))
|
77 |
alpha_composite = Image.alpha_composite(background, image_rgba)
|
78 |
alpha_composite = alpha_composite.convert("RGB")
|
|
|
79 |
|
80 |
+
# Save the converted image to a temporary file
|
81 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
82 |
+
temp_file_path = temp_file.name
|
83 |
+
alpha_composite.save(temp_file_path)
|
84 |
+
temp_file.close()
|
85 |
+
return temp_file_path # Return the path to the saved image
|
86 |
+
|
87 |
|
88 |
+
def convert_to_rgb(filepath_or_pilimg):
|
89 |
+
# `image.convert("RGB")` would only work for .jpg images, as it creates a wrong background
|
90 |
+
# for transparent images. The call to `alpha_composite` handles this case
|
91 |
+
if isinstance(filepath_or_pilimg, PIL.Image.Image):
|
92 |
+
return convert_to_rgb_pil(filepath_or_pilimg)
|
93 |
+
with Image.open(filepath_or_pilimg) as image:
|
94 |
+
# Check if the image is already in the RGB format
|
95 |
+
if image.mode == "RGB":
|
96 |
+
return filepath_or_pilimg # If already in RGB, return the original path
|
97 |
+
|
98 |
+
# Convert image to RGBA
|
99 |
+
image_rgba = image.convert("RGBA")
|
100 |
+
|
101 |
+
# Create a white background image of the same size
|
102 |
+
background = Image.new("RGBA", image_rgba.size, (255, 255, 255))
|
103 |
+
|
104 |
+
# Composite the original image over the white background
|
105 |
+
alpha_composite = Image.alpha_composite(background, image_rgba)
|
106 |
+
|
107 |
+
# Convert the composited image to RGB format
|
108 |
+
alpha_composite = alpha_composite.convert("RGB")
|
109 |
+
|
110 |
+
# Save the converted image to a temporary file
|
111 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".jpg")
|
112 |
+
temp_file_path = temp_file.name
|
113 |
+
alpha_composite.save(temp_file_path)
|
114 |
+
temp_file.close()
|
115 |
+
|
116 |
+
return temp_file_path # Return the path to the saved image
|
117 |
|
118 |
|
119 |
def base64_to_pil(encoded_image):
|
|
|
183 |
]
|
184 |
```
|
185 |
"""
|
186 |
+
print(f"******* isolate_images_urls *******")
|
187 |
+
print(f"params: prompt_list is - {prompt_list}")
|
188 |
+
|
189 |
linearized_list = []
|
190 |
for prompt in prompt_list:
|
191 |
+
print(f"inside FOR loop: prompt in prompt_list is - {prompt}")
|
192 |
# Prompt can be either a string, or a PIL image
|
193 |
if isinstance(prompt, PIL.Image.Image):
|
194 |
+
print(f"inside first IF in FOR loop: prompt is of type PIL.Image.Image")
|
195 |
+
linearized_list.append(prompt)
|
196 |
+
print(f"linearized_list after append is - {linearized_list}")
|
197 |
+
elif isinstance(prompt, str) and "/tmp/gradio/" in prompt: #isinstance(prompt, PIL.Image.Image):
|
198 |
+
print(f"inside IF in FOR loop: prompt is a string and is a path for temporary file")
|
199 |
linearized_list.append(prompt)
|
200 |
+
print(f"linearized_list after append is - {linearized_list}")
|
201 |
+
elif isinstance(prompt, str) and "/tmp/gradio/" not in prompt:
|
202 |
+
print(f"inside ELIF in FOR loop: prompt is a string and is NOT a path for temporary file")
|
203 |
if "<fake_token_around_image>" not in prompt:
|
204 |
+
print(f"inside IF inside ELIF in FOR loop: '<fake_token_around_image>' is NOT in prompt")
|
205 |
linearized_list.append(prompt)
|
206 |
+
print(f"linearized_list after append is - {linearized_list}")
|
207 |
else:
|
208 |
+
print(f"inside ELSE inside ELIF in FOR loop: '<fake_token_around_image>' IS IN prompt")
|
209 |
prompt_splitted = prompt.split("<fake_token_around_image>")
|
210 |
+
print(f"prompt_splitted is - {prompt_splitted}")
|
211 |
for ps in prompt_splitted:
|
212 |
+
print(f"Inside FOR loop inside FOR loop: ps in prompt_split is {ps}")
|
213 |
if ps == "":
|
214 |
continue
|
215 |
if ps.startswith("<image:"):
|
|
|
221 |
f"Unrecognized type for `prompt`. Got {type(type(prompt))}. Was expecting something in [`str`,"
|
222 |
" `PIL.Image.Image`]"
|
223 |
)
|
224 |
+
print(f"linearized_list to be returned is - {linearized_list}")
|
225 |
return linearized_list
|
226 |
|
227 |
|
|
|
250 |
Convert a user prompt in the list format (i.e. elements are either a PIL image or a string) into
|
251 |
the markdown format that is used for the chatbot history and rendering.
|
252 |
"""
|
253 |
+
print("********** user_prompt_list_to_markdown *********")
|
254 |
+
print(f" param : user_prompt_list is - {user_prompt_list}")
|
255 |
resulting_string = ""
|
256 |
for elem in user_prompt_list:
|
257 |
+
print(f"inside user_prompt_list_to_markdown, for loop on user_prompt_list")
|
258 |
+
print(f"elem is - {elem} ")
|
259 |
+
if isinstance(elem, str) and "/tmp/gradio/" not in elem:
|
260 |
resulting_string += elem
|
261 |
+
print(f"inside IF - when elem is string and is not temp image filepath. resulting_string is - {resulting_string}")
|
262 |
+
#elif isinstance(elem, str) and "/tmp/gradio/" in elem:
|
263 |
+
# resulting_string += f"![](/file={convert_to_rgb(elem)})" #f"![](/file={image})"
|
264 |
+
# print(f"inside first ELIF - when elem is string and is the temp image filepath. resulting_string is - {resulting_string}")
|
265 |
+
elif isinstance(elem, PIL.Image.Image) or "/tmp/gradio/" in elem: #and "/tmp/gradio/" in elem:
|
266 |
+
resulting_string += f"![](/file={convert_to_rgb(elem)})" #pil_to_markdown_im(convert_to_rgb(elem))
|
267 |
+
print(f"inside the ELIF - when elem is an instance of PIL.Image.Image or has /temp/file/path. resulting_string after pil_to_markdown_im() operation is - {resulting_string}")
|
268 |
else:
|
269 |
raise ValueError(
|
270 |
"Unknown type for `user_prompt_list`. Expected an element of type `str` or `PIL.Image.Image` and got"
|
271 |
f" `{type(elem)}`"
|
272 |
)
|
273 |
+
print(f" final resulting_string that will be returned is - {resulting_string}")
|
274 |
return resulting_string
|
275 |
|
276 |
|