Shalmoni commited on
Commit
fdaee5f
·
verified ·
1 Parent(s): 754a171

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -5
app.py CHANGED
@@ -2,6 +2,9 @@ import time, base64, io, os, requests, traceback
2
  from typing import Optional
3
  from PIL import Image, UnidentifiedImageError
4
  import gradio as gr
 
 
 
5
 
6
  # =========================
7
  # Stable Horde config
@@ -90,7 +93,7 @@ def horde_txt2img(prompt: str,
90
  dbg.append("SUBMIT exception:\n" + traceback.format_exc())
91
  return None, "\n".join(dbg)
92
 
93
- # -------- Poll --------
94
  start = time.time()
95
  while True:
96
  try:
@@ -116,17 +119,51 @@ def horde_txt2img(prompt: str,
116
  if not gens:
117
  dbg.append("DONE but no generations returned.")
118
  return None, "\n".join(dbg)
 
119
  b64 = gens[0].get("img")
120
  if not b64:
121
  dbg.append(f"GEN fields: {list(gens[0].keys())}")
122
  return None, "\n".join(dbg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
  try:
124
- img_bytes = base64.b64decode(b64)
125
  img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
126
  return img, "\n".join(dbg)
127
- except UnidentifiedImageError:
128
- dbg.append("PIL could not decode image bytes.")
129
- return None, "\n".join(dbg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
  if time.time() - start > POLL_TIMEOUT:
132
  dbg.append("TIMEOUT waiting for Horde.")
 
2
  from typing import Optional
3
  from PIL import Image, UnidentifiedImageError
4
  import gradio as gr
5
+ import binascii
6
+ import imageio.v2 as imageio
7
+ import numpy as np
8
 
9
  # =========================
10
  # Stable Horde config
 
93
  dbg.append("SUBMIT exception:\n" + traceback.format_exc())
94
  return None, "\n".join(dbg)
95
 
96
+ # -------- Poll --------
97
  start = time.time()
98
  while True:
99
  try:
 
119
  if not gens:
120
  dbg.append("DONE but no generations returned.")
121
  return None, "\n".join(dbg)
122
+
123
  b64 = gens[0].get("img")
124
  if not b64:
125
  dbg.append(f"GEN fields: {list(gens[0].keys())}")
126
  return None, "\n".join(dbg)
127
+
128
+ # ---- robust decode path ----
129
+ # 1) fix base64 padding if needed
130
+ pad = (-len(b64)) % 4
131
+ if pad:
132
+ b64 = b64 + ("=" * pad)
133
+
134
+ try:
135
+ img_bytes = base64.b64decode(b64, validate=False)
136
+ except binascii.Error as e:
137
+ dbg.append(f"Base64 decode error: {e}")
138
+ return None, "\n".join(dbg)
139
+
140
+ # 2) log magic header to debug
141
+ head = img_bytes[:12]
142
+ dbg.append(f"header bytes: {head.hex(' ')}")
143
+
144
+ # 3) try Pillow first
145
  try:
 
146
  img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
147
  return img, "\n".join(dbg)
148
+ except Exception as e:
149
+ dbg.append(f"PIL decode failed: {type(e).__name__}: {e}")
150
+
151
+ # 4) imageio fallback (handles webp/png edge cases)
152
+ try:
153
+ arr = imageio.imread(io.BytesIO(img_bytes))
154
+ if isinstance(arr, np.ndarray):
155
+ if arr.ndim == 2: # grayscale → RGB
156
+ arr = np.stack([arr, arr, arr], axis=-1)
157
+ elif arr.shape[-1] == 4: # RGBA → RGB
158
+ arr = arr[..., :3]
159
+ img = Image.fromarray(arr.astype(np.uint8), mode="RGB")
160
+ dbg.append("Decoded via imageio fallback.")
161
+ return img, "\n".join(dbg)
162
+ except Exception as e:
163
+ dbg.append(f"imageio decode failed: {type(e).__name__}: {e}")
164
+
165
+ dbg.append("PIL could not decode image bytes.")
166
+ return None, "\n".join(dbg)
167
 
168
  if time.time() - start > POLL_TIMEOUT:
169
  dbg.append("TIMEOUT waiting for Horde.")