akhaliq HF Staff commited on
Commit
f8f29e0
·
1 Parent(s): d0811b3

update text to image

Browse files
Files changed (1) hide show
  1. app.py +76 -1
app.py CHANGED
@@ -1131,6 +1131,80 @@ def create_image_replacement_blocks(html_content: str, user_prompt: str) -> str:
1131
 
1132
  return '\n\n'.join(replacement_blocks)
1133
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1134
  def create_image_replacement_blocks_from_input_image(html_content: str, user_prompt: str, input_image_data, max_images: int = 1) -> str:
1135
  """Create search/replace blocks using image-to-image generation with a provided input image.
1136
 
@@ -1239,7 +1313,8 @@ def apply_generated_images_to_html(html_content: str, user_prompt: str, enable_t
1239
 
1240
  if enable_text_to_image and (result.strip().startswith('<!DOCTYPE html>') or result.strip().startswith('<html')):
1241
  t2i_prompt = (text_to_image_prompt or user_prompt or "").strip()
1242
- blocks = create_image_replacement_blocks(result, t2i_prompt)
 
1243
  if blocks:
1244
  result = apply_search_replace_changes(result, blocks)
1245
  except Exception:
 
1131
 
1132
  return '\n\n'.join(replacement_blocks)
1133
 
1134
+ def create_image_replacement_blocks_text_to_image_single(html_content: str, prompt: str) -> str:
1135
+ """Create search/replace blocks that generate and insert ONLY ONE text-to-image result.
1136
+
1137
+ Replaces the first detected placeholder; if none found, inserts one image near the top of <body>.
1138
+ """
1139
+ if not prompt or not prompt.strip():
1140
+ return ""
1141
+
1142
+ import re
1143
+
1144
+ # Detect placeholders similarly to the multi-image version
1145
+ placeholder_patterns = [
1146
+ r'<img[^>]*src=["\'](?:placeholder|dummy|sample|example)[^"\']*["\'][^>]*>',
1147
+ r'<img[^>]*src=["\']https?://via\.placeholder\.com[^"\']*["\'][^>]*>',
1148
+ r'<img[^>]*src=["\']https?://picsum\.photos[^"\']*["\'][^>]*>',
1149
+ r'<img[^>]*src=["\']https?://dummyimage\.com[^"\']*["\'][^>]*>',
1150
+ r'<img[^>]*alt=["\'][^"\']*placeholder[^"\']*["\'][^>]*>',
1151
+ r'<img[^>]*class=["\'][^"\']*placeholder[^"\']*["\'][^>]*>',
1152
+ r'<img[^>]*id=["\'][^"\']*placeholder[^"\']*["\'][^>]*>',
1153
+ r'<img[^>]*src=["\']data:image[^"\']*["\'][^>]*>',
1154
+ r'<img[^>]*src=["\']#["\'][^>]*>',
1155
+ r'<img[^>]*src=["\']about:blank["\'][^>]*>',
1156
+ ]
1157
+
1158
+ placeholder_images = []
1159
+ for pattern in placeholder_patterns:
1160
+ matches = re.findall(pattern, html_content, re.IGNORECASE)
1161
+ if matches:
1162
+ placeholder_images.extend(matches)
1163
+
1164
+ # Fallback to any <img> if no placeholders
1165
+ if not placeholder_images:
1166
+ img_pattern = r'<img[^>]*>'
1167
+ placeholder_images = re.findall(img_pattern, html_content)
1168
+
1169
+ # Generate a single image
1170
+ image_html = generate_image_with_qwen(prompt, 0)
1171
+ if image_html.startswith("Error"):
1172
+ return ""
1173
+
1174
+ # Replace first placeholder if present
1175
+ if placeholder_images:
1176
+ placeholder = placeholder_images[0]
1177
+ placeholder_clean = re.sub(r'\s+', ' ', placeholder.strip())
1178
+ placeholder_variations = [
1179
+ placeholder_clean,
1180
+ placeholder_clean.replace('"', "'"),
1181
+ placeholder_clean.replace("'", '"'),
1182
+ re.sub(r'\s+', ' ', placeholder_clean),
1183
+ placeholder_clean.replace(' ', ' '),
1184
+ ]
1185
+ blocks = []
1186
+ for variation in placeholder_variations:
1187
+ blocks.append(f"""{SEARCH_START}
1188
+ {variation}
1189
+ {DIVIDER}
1190
+ {image_html}
1191
+ {REPLACE_END}""")
1192
+ return '\n\n'.join(blocks)
1193
+
1194
+ # Otherwise insert after <body>
1195
+ if '<body' in html_content:
1196
+ body_end = html_content.find('>', html_content.find('<body')) + 1
1197
+ insertion_point = html_content[:body_end] + '\n '
1198
+ return f"""{SEARCH_START}
1199
+ {insertion_point}
1200
+ {DIVIDER}
1201
+ {insertion_point}
1202
+ {image_html}
1203
+ {REPLACE_END}"""
1204
+
1205
+ # If no <body>, just append
1206
+ return f"{SEARCH_START}\n\n{DIVIDER}\n{image_html}\n{REPLACE_END}"
1207
+
1208
  def create_image_replacement_blocks_from_input_image(html_content: str, user_prompt: str, input_image_data, max_images: int = 1) -> str:
1209
  """Create search/replace blocks using image-to-image generation with a provided input image.
1210
 
 
1313
 
1314
  if enable_text_to_image and (result.strip().startswith('<!DOCTYPE html>') or result.strip().startswith('<html')):
1315
  t2i_prompt = (text_to_image_prompt or user_prompt or "").strip()
1316
+ # Single-image flow for text-to-image
1317
+ blocks = create_image_replacement_blocks_text_to_image_single(result, t2i_prompt)
1318
  if blocks:
1319
  result = apply_search_replace_changes(result, blocks)
1320
  except Exception: