ginipick commited on
Commit
23f0ae4
Β·
verified Β·
1 Parent(s): cb523d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -22
app.py CHANGED
@@ -1164,31 +1164,43 @@ def generate_image(prompt: str):
1164
  clean_prompt = prompt.strip("\"'").strip()
1165
  if len(clean_prompt) < 3:
1166
  return None, None
1167
- logging.info(f"Sending image generation request with prompt: {clean_prompt}")
1168
 
1169
  res = Client(IMAGE_API_URL).predict(
1170
- prompt=clean_prompt,
1171
- width=768,
1172
- height=768,
1173
- guidance=3.5,
1174
- inference_steps=30,
1175
- seed=3,
1176
- do_img2img=False,
1177
- init_image=None,
1178
- image2image_strength=0.8,
1179
- resize_img=True,
1180
- api_name="/generate_image"
1181
  )
1182
- if res and len(res) >= 2 and res[0]:
1183
- logging.info("Successfully received image data")
1184
- return res[0], clean_prompt
1185
- else:
1186
- logging.warning(f"Invalid response format from image API: {res}")
1187
  return None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1188
  except Exception as e:
1189
- logging.error(f"Image generation error: {str(e)}", exc_info=True)
1190
  return None, None
1191
 
 
1192
  # ───────────────────────────── Kaggle API ─────────────────────────────
1193
  @st.cache_resource
1194
  def check_kaggle_availability():
@@ -2331,12 +2343,16 @@ def process_input(prompt: str, uploaded_files):
2331
 
2332
  status.update(label="Invention ideas created!", state="complete")
2333
 
2334
- # Auto image generation
 
2335
  img_data = img_caption = None
2336
  if st.session_state.generate_image and full_response:
2337
- match = re.search(r"###\s*이미지\s*ν”„λ‘¬ν”„νŠΈ\s*\n+([^\n]+)", full_response, re.I)
2338
- if not match:
2339
- match = re.search(r"Image\s+Prompt\s*[:\-]\s*([^\n]+)", full_response, re.I)
 
 
 
2340
  if match:
2341
  raw_prompt = re.sub(r'[\r\n"\'\\]', " ", match.group(1)).strip()
2342
  with st.spinner("Generating illustrative image…"):
@@ -2344,6 +2360,7 @@ def process_input(prompt: str, uploaded_files):
2344
  if img_data:
2345
  st.image(img_data, caption=f"Visualized Concept – {img_caption}")
2346
 
 
2347
  answer_msg = {"role": "assistant", "content": full_response}
2348
  if img_data:
2349
  answer_msg["image"] = img_data
 
1164
  clean_prompt = prompt.strip("\"'").strip()
1165
  if len(clean_prompt) < 3:
1166
  return None, None
 
1167
 
1168
  res = Client(IMAGE_API_URL).predict(
1169
+ prompt = clean_prompt,
1170
+ width = 768,
1171
+ height = 768,
1172
+ guidance = 3.5,
1173
+ inference_steps = 30,
1174
+ seed = 3,
1175
+ do_img2img = False,
1176
+ api_name = "/generate_image",
 
 
 
1177
  )
1178
+
1179
+ # β‘‘ κ·Έλ ˆμ΄λ””μ˜€/파슀트API κ΅¬ν˜„μ— 따라 res κ°€ 1-element일 수 μžˆμœΌλ―€λ‘œ len 체크 제거
1180
+ if not res:
1181
+ logging.warning("Empty response from image API")
 
1182
  return None, None
1183
+
1184
+ raw = res[0] # URL, 파일경둜, λ˜λŠ” base64
1185
+ if raw.startswith("http"):
1186
+ return raw, clean_prompt
1187
+
1188
+ # base64일 경우 λ””μ½”λ”©
1189
+ if raw.startswith("data:image"):
1190
+ raw = raw.split(",", 1)[1]
1191
+ try:
1192
+ import base64
1193
+ img_bytes = base64.b64decode(raw)
1194
+ return img_bytes, clean_prompt
1195
+ except Exception as e:
1196
+ logging.error(f"base64 decode failed: {e}")
1197
+ return None, None
1198
+
1199
  except Exception as e:
1200
+ logging.error(f"Image generation error: {e}", exc_info=True)
1201
  return None, None
1202
 
1203
+
1204
  # ───────────────────────────── Kaggle API ─────────────────────────────
1205
  @st.cache_resource
1206
  def check_kaggle_availability():
 
2343
 
2344
  status.update(label="Invention ideas created!", state="complete")
2345
 
2346
+
2347
+ # ── Auto image generation ─────────────────────────────
2348
  img_data = img_caption = None
2349
  if st.session_state.generate_image and full_response:
2350
+ # β‘  header(###)κ°€ μžˆλ“  μ—†λ“ , ν•œ/영 ν‘œκΈ°λ₯Ό λͺ¨λ‘ μž‘μ•„λ‚Έλ‹€
2351
+ match = re.search(
2352
+ r"(?:#{0,3}\s*)?(?:Image|이미지)\s*ν”„λ‘¬ν”„νŠΈ\s*(?:[::\-]\s*)?\n+([^\n]+)",
2353
+ full_response,
2354
+ re.I,
2355
+ )
2356
  if match:
2357
  raw_prompt = re.sub(r'[\r\n"\'\\]', " ", match.group(1)).strip()
2358
  with st.spinner("Generating illustrative image…"):
 
2360
  if img_data:
2361
  st.image(img_data, caption=f"Visualized Concept – {img_caption}")
2362
 
2363
+
2364
  answer_msg = {"role": "assistant", "content": full_response}
2365
  if img_data:
2366
  answer_msg["image"] = img_data