burhansyam commited on
Commit
e33dd82
·
verified ·
1 Parent(s): 807700f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -28,38 +28,52 @@ def load_fn(models):
28
 
29
  load_fn(models)
30
 
31
- def save_image_to_tmp(image_data):
 
32
  if image_data is None:
33
  return None
34
 
35
- # Handle case where image_data is already a file path
36
- if isinstance(image_data, str):
37
- if image_data.startswith(('http://', 'https://')):
 
 
 
 
38
  response = requests.get(image_data)
39
  img = Image.open(BytesIO(response.content))
40
- elif os.path.isfile(image_data):
41
- img = Image.open(image_data)
42
- else:
43
  return None
44
  else:
45
- # Assume it's a PIL Image or compatible object
46
  img = image_data
47
 
 
48
  filename = f"{uuid.uuid4()}.jpg"
49
  filepath = os.path.join(IMAGE_DIR, filename)
50
- img.save(filepath)
51
- return filepath # Return full path for Gradio to handle
 
 
 
 
 
 
 
 
 
52
 
53
  def gen_fn(model_str, prompt):
54
  if model_str == 'NA':
55
  return None
 
56
  noise = str(randint(0, 4294967296))
57
  klir = '| ultra detail, ultra elaboration, ultra quality, perfect'
58
  result = models_load[model_str](f'{prompt} {klir} {noise}')
59
 
60
  if isinstance(result, list):
61
- return [save_image_to_tmp(img) for img in result if img is not None]
62
- return save_image_to_tmp(result)
63
 
64
  def create_interface():
65
  with gr.Blocks(css=css_code) as demo:
 
28
 
29
  load_fn(models)
30
 
31
+ def process_image_output(image_data):
32
+ """Handle various image output formats and save to temp directory"""
33
  if image_data is None:
34
  return None
35
 
36
+ # If it's already a file path that exists, use it
37
+ if isinstance(image_data, str) and os.path.isfile(image_data):
38
+ return image_data
39
+
40
+ # If it's a URL, download it
41
+ if isinstance(image_data, str) and image_data.startswith(('http://', 'https://')):
42
+ try:
43
  response = requests.get(image_data)
44
  img = Image.open(BytesIO(response.content))
45
+ except:
 
 
46
  return None
47
  else:
48
+ # Assume it's a PIL Image or numpy array
49
  img = image_data
50
 
51
+ # Save to temp file
52
  filename = f"{uuid.uuid4()}.jpg"
53
  filepath = os.path.join(IMAGE_DIR, filename)
54
+
55
+ try:
56
+ if isinstance(img, Image.Image):
57
+ img.save(filepath)
58
+ else:
59
+ # Handle numpy arrays or other formats
60
+ Image.fromarray(img).save(filepath)
61
+ return filepath
62
+ except Exception as e:
63
+ print(f"Error saving image: {e}")
64
+ return None
65
 
66
  def gen_fn(model_str, prompt):
67
  if model_str == 'NA':
68
  return None
69
+
70
  noise = str(randint(0, 4294967296))
71
  klir = '| ultra detail, ultra elaboration, ultra quality, perfect'
72
  result = models_load[model_str](f'{prompt} {klir} {noise}')
73
 
74
  if isinstance(result, list):
75
+ return [process_image_output(img) for img in result if img is not None]
76
+ return process_image_output(result)
77
 
78
  def create_interface():
79
  with gr.Blocks(css=css_code) as demo: