victor HF Staff commited on
Commit
24c2853
·
1 Parent(s): 95af8cc

Enforce mandatory text-balance class for all titles and headings; enhance redesign function to accept custom user instructions

Browse files
Files changed (1) hide show
  1. app.py +31 -10
app.py CHANGED
@@ -24,7 +24,7 @@ Color & Typography Preservation:
24
  - REUSE those exact color classes in the redesigned version
25
  - If input uses blue-500, continue using blue-* variants throughout
26
  - If input has specific font classes (font-medium, text-lg), maintain that typography scale
27
- - Use text-balance class on titles and headings for better text wrapping and visual balance
28
  - Only introduce new colors if the original has none or uses basic colors
29
 
30
  Dark Mode Support:
@@ -79,6 +79,7 @@ Critical Preservation Rules:
79
  - If input has buttons with onclick or type attributes, preserve them completely
80
  - If input has any data attributes (data-*), maintain them unchanged
81
  - All original text content must remain identical
 
82
 
83
  Framework-Specific Preservation (React/Vue/Svelte/Angular):
84
  - REACT: Preserve all JSX syntax: {variable}, {function()}, className, onClick, onChange, etc.
@@ -111,7 +112,7 @@ def extract_html_from_response(response_text):
111
  raise ValueError("No HTML content found in the response")
112
 
113
 
114
- def redesign_html(html_input: str) -> str:
115
  """
116
  Redesign HTML+TailwindCSS code with improved spacing, responsive design, and modern aesthetics.
117
 
@@ -124,6 +125,10 @@ def redesign_html(html_input: str) -> str:
124
  Args:
125
  html_input (str): The HTML code with TailwindCSS classes to be redesigned.
126
  Can include React/Vue/Svelte/Angular syntax which will be preserved.
 
 
 
 
127
 
128
  Returns:
129
  str: The redesigned HTML code with improved TailwindCSS styling, preserving all
@@ -137,6 +142,7 @@ def redesign_html(html_input: str) -> str:
137
  - Adds proper flex-none for non-stretching elements
138
  - Ensures mobile-first responsive design
139
  - Supports dark mode when present in input
 
140
  """
141
 
142
  # Check if HF_TOKEN is set
@@ -151,10 +157,14 @@ def redesign_html(html_input: str) -> str:
151
  "Content-Type": "application/json",
152
  }
153
 
154
- # Build messages with system prompt
 
 
 
 
155
  messages = [
156
  {"role": "system", "content": SYSTEM_PROMPT},
157
- {"role": "user", "content": html_input},
158
  ]
159
 
160
  data = {
@@ -211,12 +221,20 @@ custom_css = """
211
  # Create Gradio interface
212
  demo = gr.Interface(
213
  fn=redesign_html,
214
- inputs=gr.Textbox(
215
- label="HTML Input",
216
- placeholder="Paste your HTML+TailwindCSS code here...",
217
- lines=10,
218
- value='<div class="p-4 border"><h1 class="text-xl">Welcome</h1><p>This is a basic card</p><button class="bg-blue-500 text-white px-4 py-2">Click me</button></div>',
219
- ),
 
 
 
 
 
 
 
 
220
  outputs=gr.Code(language="html", label="Redesigned HTML"),
221
  title="Redesigner (MCP Server)",
222
  description="""
@@ -238,12 +256,15 @@ demo = gr.Interface(
238
  """,
239
  examples=[
240
  [
 
241
  '<div class="p-4 border"><h1 class="text-xl">Welcome</h1><p>This is a basic card</p><button class="bg-blue-500 text-white px-4 py-2">Click me</button></div>'
242
  ],
243
  [
 
244
  '<form class="space-y-4"><input type="email" placeholder="Email" class="border p-2 w-full"><input type="password" placeholder="Password" class="border p-2 w-full"><button type="submit" class="bg-green-500 text-white p-2">Login</button></form>'
245
  ],
246
  [
 
247
  '<nav class="flex justify-between p-4 bg-gray-100"><div class="font-bold">Logo</div><ul class="flex space-x-4"><li><a href="#" class="text-blue-500">Home</a></li><li><a href="#" class="text-blue-500">About</a></li></ul></nav>'
248
  ],
249
  ],
 
24
  - REUSE those exact color classes in the redesigned version
25
  - If input uses blue-500, continue using blue-* variants throughout
26
  - If input has specific font classes (font-medium, text-lg), maintain that typography scale
27
+ - MANDATORY: ALWAYS add text-balance class to ALL titles and headings (h1, h2, h3, h4, h5, h6, and any element with large text like text-xl, text-2xl, etc.) for better text wrapping and visual balance
28
  - Only introduce new colors if the original has none or uses basic colors
29
 
30
  Dark Mode Support:
 
79
  - If input has buttons with onclick or type attributes, preserve them completely
80
  - If input has any data attributes (data-*), maintain them unchanged
81
  - All original text content must remain identical
82
+ - MANDATORY TEXT-BALANCE RULE: Every heading (h1-h6) and large text element (text-xl, text-2xl, text-3xl, etc.) MUST include the text-balance class
83
 
84
  Framework-Specific Preservation (React/Vue/Svelte/Angular):
85
  - REACT: Preserve all JSX syntax: {variable}, {function()}, className, onClick, onChange, etc.
 
112
  raise ValueError("No HTML content found in the response")
113
 
114
 
115
+ def redesign_html(html_input: str, user_prompt: str = "") -> str:
116
  """
117
  Redesign HTML+TailwindCSS code with improved spacing, responsive design, and modern aesthetics.
118
 
 
125
  Args:
126
  html_input (str): The HTML code with TailwindCSS classes to be redesigned.
127
  Can include React/Vue/Svelte/Angular syntax which will be preserved.
128
+ user_prompt (str, optional): Custom instructions for the redesign process.
129
+ Examples: "make it more colorful", "add animations",
130
+ "make it minimal", "improve mobile layout".
131
+ If empty, uses default redesign approach.
132
 
133
  Returns:
134
  str: The redesigned HTML code with improved TailwindCSS styling, preserving all
 
142
  - Adds proper flex-none for non-stretching elements
143
  - Ensures mobile-first responsive design
144
  - Supports dark mode when present in input
145
+ - Accepts custom user instructions for targeted redesign approaches
146
  """
147
 
148
  # Check if HF_TOKEN is set
 
157
  "Content-Type": "application/json",
158
  }
159
 
160
+ # Build messages with system prompt and user prompt
161
+ user_content = html_input
162
+ if user_prompt.strip():
163
+ user_content = f"User instructions: {user_prompt.strip()}\n\nHTML to redesign:\n{html_input}"
164
+
165
  messages = [
166
  {"role": "system", "content": SYSTEM_PROMPT},
167
+ {"role": "user", "content": user_content},
168
  ]
169
 
170
  data = {
 
221
  # Create Gradio interface
222
  demo = gr.Interface(
223
  fn=redesign_html,
224
+ inputs=[
225
+ gr.Textbox(
226
+ label="Custom Instructions (Optional)",
227
+ placeholder="Enter specific instructions for the redesign (e.g., 'make it more colorful', 'add animations', 'make it minimal')...",
228
+ lines=2,
229
+ value="",
230
+ ),
231
+ gr.Textbox(
232
+ label="HTML Input",
233
+ placeholder="Paste your HTML+TailwindCSS code here...",
234
+ lines=10,
235
+ value='<div class="p-4 border"><h1 class="text-xl">Welcome</h1><p>This is a basic card</p><button class="bg-blue-500 text-white px-4 py-2">Click me</button></div>',
236
+ ),
237
+ ],
238
  outputs=gr.Code(language="html", label="Redesigned HTML"),
239
  title="Redesigner (MCP Server)",
240
  description="""
 
256
  """,
257
  examples=[
258
  [
259
+ "Make it more modern and colorful",
260
  '<div class="p-4 border"><h1 class="text-xl">Welcome</h1><p>This is a basic card</p><button class="bg-blue-500 text-white px-4 py-2">Click me</button></div>'
261
  ],
262
  [
263
+ "Add subtle animations and improve spacing",
264
  '<form class="space-y-4"><input type="email" placeholder="Email" class="border p-2 w-full"><input type="password" placeholder="Password" class="border p-2 w-full"><button type="submit" class="bg-green-500 text-white p-2">Login</button></form>'
265
  ],
266
  [
267
+ "",
268
  '<nav class="flex justify-between p-4 bg-gray-100"><div class="font-bold">Logo</div><ul class="flex space-x-4"><li><a href="#" class="text-blue-500">Home</a></li><li><a href="#" class="text-blue-500">About</a></li></ul></nav>'
269
  ],
270
  ],