Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1165,40 +1165,37 @@ def generate_image(prompt: str):
|
|
1165 |
if len(clean_prompt) < 3:
|
1166 |
return None, None
|
1167 |
|
1168 |
-
|
1169 |
-
|
1170 |
-
|
1171 |
-
|
1172 |
-
|
1173 |
-
|
1174 |
-
|
1175 |
-
|
1176 |
-
|
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 |
-
|
1185 |
-
|
1186 |
-
|
1187 |
-
|
1188 |
-
|
1189 |
-
|
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 |
-
|
2351 |
-
|
|
|
2352 |
r"(?:#{0,3}\s*)?(?:Image|μ΄λ―Έμ§)\s*ν둬ννΈ\s*(?:[:οΌ\-]\s*)?\n+([^\n]+)",
|
2353 |
-
|
2354 |
-
|
2355 |
-
|
2356 |
-
|
2357 |
-
|
2358 |
-
|
2359 |
-
|
2360 |
-
|
2361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}
|