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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -42
app.py CHANGED
@@ -1165,40 +1165,37 @@ def generate_image(prompt: str):
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 ─────────────────────────────
@@ -2343,22 +2340,42 @@ def process_input(prompt: str, uploaded_files):
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…"):
2359
- img_data, img_caption = generate_image(raw_prompt)
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}
 
1165
  if len(clean_prompt) < 3:
1166
  return None, None
1167
 
1168
+ # Add timeout and better logging
1169
+ st.write(f"Connecting to image API at {IMAGE_API_URL}...")
1170
+
1171
+ try:
1172
+ res = Client(IMAGE_API_URL).predict(
1173
+ prompt = clean_prompt,
1174
+ width = 768,
1175
+ height = 768,
1176
+ guidance = 3.5,
1177
+ inference_steps = 30,
1178
+ seed = 3,
1179
+ do_img2img = False,
1180
+ api_name = "/generate_image",
1181
+ timeout = 30 # Add timeout
1182
+ )
1183
+ except Exception as api_err:
1184
+ st.error(f"Image API connection failed: {api_err}")
1185
+ logging.error(f"Image API connection error: {api_err}")
1186
+ return None, None
1187
 
 
1188
  if not res:
1189
+ st.warning("Image API returned empty response")
1190
  logging.warning("Empty response from image API")
1191
  return None, None
1192
 
1193
+ # Debug the response
1194
+ st.write(f"API Response type: {type(res)}")
1195
+ logging.info(f"Image API response: {res}")
1196
+
1197
+ raw = res[0] if isinstance(res, list) else res
1198
+ # Rest of your function...
 
 
 
 
 
 
 
 
 
 
 
 
1199
 
1200
 
1201
  # ───────────────────────────── Kaggle API ─────────────────────────────
 
2340
 
2341
  status.update(label="Invention ideas created!", state="complete")
2342
 
2343
+ # ── Auto image generation (Improved) ─────────────────────────────
 
2344
  img_data = img_caption = None
2345
  if st.session_state.generate_image and full_response:
2346
+ # Use multiple regex patterns to catch different image prompt formats
2347
+ prompt_patterns = [
2348
+ # Original pattern
2349
  r"(?:#{0,3}\s*)?(?:Image|이미지)\s*ν”„λ‘¬ν”„νŠΈ\s*(?:[::\-]\s*)?\n+([^\n]+)",
2350
+ # Alternative formats
2351
+ r"(?:#{0,3}\s*)?(?:Image|이미지)(?:\s*prompt)?(?:[::\-]\s*)?\n*([^\n]+)",
2352
+ r"\*\*(?:Image|이미지)(?:\s*prompt)?\*\*(?:[::\-]\s*)?\n*([^\n]+)",
2353
+ r"[Ii]mage\s+[Pp]rompt:\s*([^\n]+)"
2354
+ ]
2355
+
2356
+ raw_prompt = None
2357
+ for pattern in prompt_patterns:
2358
+ match = re.search(pattern, full_response, re.I)
2359
+ if match:
2360
+ raw_prompt = re.sub(r'[\r\n"\'\\]', " ", match.group(1)).strip()
2361
+ break
2362
+
2363
+ if raw_prompt:
2364
+ with st.spinner(f"Generating image for concept: {raw_prompt[:50]}..."):
2365
+ try:
2366
+ img_data, img_caption = generate_image(raw_prompt)
2367
+ if img_data:
2368
+ st.image(img_data, caption=f"Visualized Concept – {img_caption}")
2369
+ else:
2370
+ st.warning("⚠️ Image generation produced no results. Using text-only output.")
2371
+ logging.warning(f"Image generation failed for prompt: {raw_prompt}")
2372
+ except Exception as img_err:
2373
+ st.error(f"⚠️ Image generation error: {str(img_err)[:100]}...")
2374
+ logging.error(f"Image generation error: {img_err}", exc_info=True)
2375
+ else:
2376
+ logging.warning("No image prompt found in the response")
2377
+ if "debug" in st.session_state and st.session_state.debug:
2378
+ st.info("DEBUG: No image prompt pattern matched in the response.")
2379
 
2380
 
2381
  answer_msg = {"role": "assistant", "content": full_response}