Leyo commited on
Commit
5165bea
1 Parent(s): b627bb5

resize images sent to tgi

Browse files
Files changed (1) hide show
  1. app_dialogue.py +17 -3
app_dialogue.py CHANGED
@@ -9,7 +9,9 @@ from pathlib import Path
9
  from typing import List, Optional, Tuple
10
  from urllib.parse import urlparse
11
  from PIL import Image, ImageDraw, ImageFont
 
12
 
 
13
  import concurrent.futures
14
  import random
15
  import gradio as gr
@@ -86,9 +88,14 @@ def hash_bytes(bytes: bytes):
86
 
87
 
88
  def pil_to_temp_file(
89
- img: PIL.Image.Image, dir: str = DEFAULT_TEMP_DIR, format: str = "png"
 
 
 
90
  ) -> str:
91
  """Save a PIL image into a temp file"""
 
 
92
  bytes_data = processing_utils.encode_pil_to_bytes(img, format)
93
  temp_dir = Path(dir) / hash_bytes(bytes_data)
94
  temp_dir.mkdir(exist_ok=True, parents=True)
@@ -304,9 +311,16 @@ def prompt_list_to_tgi_input(prompt_list: List[str]) -> str:
304
  for elem in prompt_list:
305
  if is_image(elem):
306
  if is_url(elem):
307
- result_string_input += f"![]({elem})"
 
 
 
 
 
308
  else:
309
- result_string_input += f"![]({gradio_link(img_path=elem)})"
 
 
310
  else:
311
  result_string_input += elem
312
  return result_string_input
 
9
  from typing import List, Optional, Tuple
10
  from urllib.parse import urlparse
11
  from PIL import Image, ImageDraw, ImageFont
12
+ from io import BytesIO
13
 
14
+ import requests
15
  import concurrent.futures
16
  import random
17
  import gradio as gr
 
88
 
89
 
90
  def pil_to_temp_file(
91
+ img: PIL.Image.Image,
92
+ dir: str = DEFAULT_TEMP_DIR,
93
+ format: str = "png",
94
+ resize: bool = False,
95
  ) -> str:
96
  """Save a PIL image into a temp file"""
97
+ if resize:
98
+ img = img.resize((224, 224), Image.LANCZOS)
99
  bytes_data = processing_utils.encode_pil_to_bytes(img, format)
100
  temp_dir = Path(dir) / hash_bytes(bytes_data)
101
  temp_dir.mkdir(exist_ok=True, parents=True)
 
311
  for elem in prompt_list:
312
  if is_image(elem):
313
  if is_url(elem):
314
+ try:
315
+ response = requests.get(elem)
316
+ if response.status_code == 200:
317
+ elem_pil = Image.open(BytesIO(response.content))
318
+ except Exception:
319
+ print(f"Image is not loading")
320
  else:
321
+ elem_pil = Image.open(elem)
322
+ elem = pil_to_temp_file(elem_pil, resize=True)
323
+ result_string_input += f"![]({gradio_link(img_path=elem)})"
324
  else:
325
  result_string_input += elem
326
  return result_string_input