awacke1 commited on
Commit
ef1a324
·
verified ·
1 Parent(s): 4c0a62a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -16
app.py CHANGED
@@ -32,19 +32,24 @@ class ModelGenerator:
32
  api_name="/run"
33
  )
34
 
35
- if isinstance(result, list) and len(result) > 0:
36
- image_data = result[0]
 
 
 
 
 
 
37
  if isinstance(image_data, str):
38
  if image_data.startswith('http'):
39
  response = requests.get(image_data)
40
- image = Image.open(io.BytesIO(response.content))
41
- else:
42
- image = Image.open(image_data)
43
- else:
44
- image = Image.open(io.BytesIO(image_data))
45
- return ("Midjourney", image)
46
- else:
47
- return ("Midjourney", f"Error: Unexpected result format: {type(result)}")
48
  except Exception as e:
49
  return ("Midjourney", f"Error: {str(e)}")
50
 
@@ -65,7 +70,11 @@ class ModelGenerator:
65
  num_images_per_prompt=1,
66
  api_name="/run"
67
  )
68
- return ("Stable Cascade", result)
 
 
 
 
69
  except Exception as e:
70
  return ("Stable Cascade", f"Error: {str(e)}")
71
 
@@ -84,7 +93,23 @@ class ModelGenerator:
84
  num_inference_steps=28,
85
  api_name="/infer"
86
  )
87
- return ("SD 3 Medium", result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  except Exception as e:
89
  return ("SD 3 Medium", f"Error: {str(e)}")
90
 
@@ -103,7 +128,23 @@ class ModelGenerator:
103
  num_inference_steps=40,
104
  api_name="/infer"
105
  )
106
- return ("SD 3.5 Large", result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  except Exception as e:
108
  return ("SD 3.5 Large", f"Error: {str(e)}")
109
 
@@ -123,8 +164,15 @@ class ModelGenerator:
123
  True, # randomize seed
124
  api_name="/run"
125
  )
126
- if result and isinstance(result, tuple) and result[0]:
127
- return ("Playground v2.5", result[0][0]['image'])
 
 
 
 
 
 
 
128
  return ("Playground v2.5", "Error: No image generated")
129
  except Exception as e:
130
  return ("Playground v2.5", f"Error: {str(e)}")
@@ -150,7 +198,12 @@ def generate_images(prompt, selected_models):
150
  futures.append(executor.submit(model_map[model], prompt))
151
 
152
  for future in concurrent.futures.as_completed(futures):
153
- results.append(future.result())
 
 
 
 
 
154
 
155
  return results
156
 
 
32
  api_name="/run"
33
  )
34
 
35
+ if isinstance(result, tuple):
36
+ image_data = result[0] if len(result) > 0 else None
37
+ elif isinstance(result, list):
38
+ image_data = result[0] if len(result) > 0 else None
39
+ else:
40
+ image_data = result
41
+
42
+ if image_data:
43
  if isinstance(image_data, str):
44
  if image_data.startswith('http'):
45
  response = requests.get(image_data)
46
+ return ("Midjourney", Image.open(io.BytesIO(response.content)))
47
+ return ("Midjourney", Image.open(image_data))
48
+ elif isinstance(image_data, bytes):
49
+ return ("Midjourney", Image.open(io.BytesIO(image_data)))
50
+ elif hasattr(image_data, 'read'): # File-like object
51
+ return ("Midjourney", Image.open(image_data))
52
+ return ("Midjourney", "Error: No valid image data found")
 
53
  except Exception as e:
54
  return ("Midjourney", f"Error: {str(e)}")
55
 
 
70
  num_images_per_prompt=1,
71
  api_name="/run"
72
  )
73
+ if isinstance(result, (str, bytes)):
74
+ return ("Stable Cascade", Image.open(io.BytesIO(result) if isinstance(result, bytes) else result))
75
+ elif isinstance(result, list) and len(result) > 0:
76
+ return ("Stable Cascade", Image.open(io.BytesIO(result[0]) if isinstance(result[0], bytes) else result[0]))
77
+ return ("Stable Cascade", "Error: No valid image data found")
78
  except Exception as e:
79
  return ("Stable Cascade", f"Error: {str(e)}")
80
 
 
93
  num_inference_steps=28,
94
  api_name="/infer"
95
  )
96
+ if isinstance(result, bytes):
97
+ return ("SD 3 Medium", Image.open(io.BytesIO(result)))
98
+ elif isinstance(result, str):
99
+ if result.startswith('http'):
100
+ response = requests.get(result)
101
+ return ("SD 3 Medium", Image.open(io.BytesIO(response.content)))
102
+ return ("SD 3 Medium", Image.open(result))
103
+ elif isinstance(result, list) and len(result) > 0:
104
+ image_data = result[0]
105
+ if isinstance(image_data, bytes):
106
+ return ("SD 3 Medium", Image.open(io.BytesIO(image_data)))
107
+ elif isinstance(image_data, str):
108
+ if image_data.startswith('http'):
109
+ response = requests.get(image_data)
110
+ return ("SD 3 Medium", Image.open(io.BytesIO(response.content)))
111
+ return ("SD 3 Medium", Image.open(image_data))
112
+ return ("SD 3 Medium", "Error: No valid image data found")
113
  except Exception as e:
114
  return ("SD 3 Medium", f"Error: {str(e)}")
115
 
 
128
  num_inference_steps=40,
129
  api_name="/infer"
130
  )
131
+ if isinstance(result, bytes):
132
+ return ("SD 3.5 Large", Image.open(io.BytesIO(result)))
133
+ elif isinstance(result, str):
134
+ if result.startswith('http'):
135
+ response = requests.get(result)
136
+ return ("SD 3.5 Large", Image.open(io.BytesIO(response.content)))
137
+ return ("SD 3.5 Large", Image.open(result))
138
+ elif isinstance(result, list) and len(result) > 0:
139
+ image_data = result[0]
140
+ if isinstance(image_data, bytes):
141
+ return ("SD 3.5 Large", Image.open(io.BytesIO(image_data)))
142
+ elif isinstance(image_data, str):
143
+ if image_data.startswith('http'):
144
+ response = requests.get(image_data)
145
+ return ("SD 3.5 Large", Image.open(io.BytesIO(response.content)))
146
+ return ("SD 3.5 Large", Image.open(image_data))
147
+ return ("SD 3.5 Large", "Error: No valid image data found")
148
  except Exception as e:
149
  return ("SD 3.5 Large", f"Error: {str(e)}")
150
 
 
164
  True, # randomize seed
165
  api_name="/run"
166
  )
167
+ if isinstance(result, tuple) and result[0] and len(result[0]) > 0:
168
+ image_data = result[0][0].get('image')
169
+ if image_data:
170
+ if isinstance(image_data, str):
171
+ if image_data.startswith('http'):
172
+ response = requests.get(image_data)
173
+ return ("Playground v2.5", Image.open(io.BytesIO(response.content)))
174
+ return ("Playground v2.5", Image.open(image_data))
175
+ return ("Playground v2.5", Image.open(io.BytesIO(image_data)))
176
  return ("Playground v2.5", "Error: No image generated")
177
  except Exception as e:
178
  return ("Playground v2.5", f"Error: {str(e)}")
 
198
  futures.append(executor.submit(model_map[model], prompt))
199
 
200
  for future in concurrent.futures.as_completed(futures):
201
+ try:
202
+ result = future.result()
203
+ if result:
204
+ results.append(result)
205
+ except Exception as e:
206
+ st.error(f"Error during image generation: {str(e)}")
207
 
208
  return results
209