Spaces:
Sleeping
Sleeping
code generation
#1
by Naveen671 - opened
app.py
CHANGED
|
@@ -1,26 +1,532 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
|
|
|
|
|
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
#
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 24 |
Args:
|
| 25 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
|
@@ -33,37 +539,65 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
-
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 46 |
-
custom_role_conversions=None,
|
| 47 |
)
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 52 |
-
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
| 62 |
planning_interval=None,
|
| 63 |
-
name=
|
| 64 |
-
description=
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
import base64
|
| 7 |
+
import io
|
| 8 |
+
from PIL import Image
|
| 9 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 10 |
from Gradio_UI import GradioUI
|
| 11 |
|
| 12 |
+
# Enhanced design-to-code tool
|
| 13 |
@tool
|
| 14 |
+
def design_to_code_generator(design_prompt: str, image_path: str = None) -> str:
|
| 15 |
+
"""A tool that generates HTML, CSS, and JavaScript code based on a design description or image.
|
| 16 |
+
|
| 17 |
Args:
|
| 18 |
+
design_prompt: A detailed description of the desired web design/interface
|
| 19 |
+
image_path: Optional path to an image file for visual reference
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Base HTML template with modern styling capabilities
|
| 23 |
+
html_template = """<!DOCTYPE html>
|
| 24 |
+
<html lang="en">
|
| 25 |
+
<head>
|
| 26 |
+
<meta charset="UTF-8">
|
| 27 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 28 |
+
<title>{title}</title>
|
| 29 |
+
<style>
|
| 30 |
+
{css_content}
|
| 31 |
+
</style>
|
| 32 |
+
</head>
|
| 33 |
+
<body>
|
| 34 |
+
{html_content}
|
| 35 |
+
<script>
|
| 36 |
+
{js_content}
|
| 37 |
+
</script>
|
| 38 |
+
</body>
|
| 39 |
+
</html>"""
|
| 40 |
+
|
| 41 |
+
# Analyze the design prompt to determine components and styling
|
| 42 |
+
components = analyze_design_prompt(design_prompt)
|
| 43 |
+
|
| 44 |
+
# Generate CSS based on design requirements
|
| 45 |
+
css_content = generate_css_from_prompt(design_prompt, components)
|
| 46 |
+
|
| 47 |
+
# Generate HTML structure
|
| 48 |
+
html_content = generate_html_structure(design_prompt, components)
|
| 49 |
+
|
| 50 |
+
# Generate JavaScript for interactivity
|
| 51 |
+
js_content = generate_javascript_features(design_prompt, components)
|
| 52 |
+
|
| 53 |
+
# Determine page title from prompt
|
| 54 |
+
title = extract_title_from_prompt(design_prompt)
|
| 55 |
+
|
| 56 |
+
# Combine everything
|
| 57 |
+
final_code = html_template.format(
|
| 58 |
+
title=title,
|
| 59 |
+
css_content=css_content,
|
| 60 |
+
html_content=html_content,
|
| 61 |
+
js_content=js_content
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
return f"Generated HTML/CSS/JS code:\n\n```html\n{final_code}\n```"
|
| 65 |
+
|
| 66 |
+
def analyze_design_prompt(prompt: str) -> dict:
|
| 67 |
+
"""Analyze the design prompt to identify components and features needed."""
|
| 68 |
+
prompt_lower = prompt.lower()
|
| 69 |
+
|
| 70 |
+
components = {
|
| 71 |
+
'has_header': any(word in prompt_lower for word in ['header', 'navigation', 'nav', 'menu']),
|
| 72 |
+
'has_hero': any(word in prompt_lower for word in ['hero', 'banner', 'main section', 'landing']),
|
| 73 |
+
'has_cards': any(word in prompt_lower for word in ['card', 'cards', 'grid', 'boxes']),
|
| 74 |
+
'has_form': any(word in prompt_lower for word in ['form', 'input', 'contact', 'signup', 'login']),
|
| 75 |
+
'has_footer': any(word in prompt_lower for word in ['footer', 'bottom']),
|
| 76 |
+
'has_sidebar': any(word in prompt_lower for word in ['sidebar', 'aside', 'side panel']),
|
| 77 |
+
'has_gallery': any(word in prompt_lower for word in ['gallery', 'images', 'photos', 'portfolio']),
|
| 78 |
+
'has_animation': any(word in prompt_lower for word in ['animate', 'animation', 'hover', 'interactive']),
|
| 79 |
+
'color_scheme': extract_colors_from_prompt(prompt),
|
| 80 |
+
'layout_type': determine_layout_type(prompt),
|
| 81 |
+
'style_theme': determine_style_theme(prompt)
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
return components
|
| 85 |
+
|
| 86 |
+
def extract_colors_from_prompt(prompt: str) -> list:
|
| 87 |
+
"""Extract color preferences from the prompt."""
|
| 88 |
+
colors = []
|
| 89 |
+
color_words = ['blue', 'red', 'green', 'purple', 'orange', 'yellow', 'pink', 'black', 'white', 'gray', 'dark', 'light']
|
| 90 |
+
|
| 91 |
+
for color in color_words:
|
| 92 |
+
if color in prompt.lower():
|
| 93 |
+
colors.append(color)
|
| 94 |
+
|
| 95 |
+
return colors if colors else ['blue', 'white'] # Default colors
|
| 96 |
+
|
| 97 |
+
def determine_layout_type(prompt: str) -> str:
|
| 98 |
+
"""Determine the layout type based on the prompt."""
|
| 99 |
+
prompt_lower = prompt.lower()
|
| 100 |
+
|
| 101 |
+
if any(word in prompt_lower for word in ['dashboard', 'admin', 'panel']):
|
| 102 |
+
return 'dashboard'
|
| 103 |
+
elif any(word in prompt_lower for word in ['landing', 'homepage', 'marketing']):
|
| 104 |
+
return 'landing'
|
| 105 |
+
elif any(word in prompt_lower for word in ['blog', 'article', 'post']):
|
| 106 |
+
return 'blog'
|
| 107 |
+
elif any(word in prompt_lower for word in ['portfolio', 'gallery', 'showcase']):
|
| 108 |
+
return 'portfolio'
|
| 109 |
+
else:
|
| 110 |
+
return 'general'
|
| 111 |
+
|
| 112 |
+
def determine_style_theme(prompt: str) -> str:
|
| 113 |
+
"""Determine the visual style theme."""
|
| 114 |
+
prompt_lower = prompt.lower()
|
| 115 |
+
|
| 116 |
+
if any(word in prompt_lower for word in ['modern', 'clean', 'minimal']):
|
| 117 |
+
return 'modern'
|
| 118 |
+
elif any(word in prompt_lower for word in ['dark', 'night', 'black']):
|
| 119 |
+
return 'dark'
|
| 120 |
+
elif any(word in prompt_lower for word in ['colorful', 'vibrant', 'bright']):
|
| 121 |
+
return 'colorful'
|
| 122 |
+
elif any(word in prompt_lower for word in ['professional', 'corporate', 'business']):
|
| 123 |
+
return 'professional'
|
| 124 |
+
else:
|
| 125 |
+
return 'modern'
|
| 126 |
+
|
| 127 |
+
def generate_css_from_prompt(prompt: str, components: dict) -> str:
|
| 128 |
+
"""Generate CSS based on the design requirements."""
|
| 129 |
+
|
| 130 |
+
# Base styles
|
| 131 |
+
css = """
|
| 132 |
+
* {
|
| 133 |
+
margin: 0;
|
| 134 |
+
padding: 0;
|
| 135 |
+
box-sizing: border-box;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
body {
|
| 139 |
+
font-family: 'Arial', sans-serif;
|
| 140 |
+
line-height: 1.6;
|
| 141 |
+
color: #333;
|
| 142 |
+
}
|
| 143 |
"""
|
| 144 |
+
|
| 145 |
+
# Add theme-specific styles
|
| 146 |
+
if components['style_theme'] == 'dark':
|
| 147 |
+
css += """
|
| 148 |
+
body {
|
| 149 |
+
background-color: #1a1a1a;
|
| 150 |
+
color: #ffffff;
|
| 151 |
+
}
|
| 152 |
+
"""
|
| 153 |
+
elif components['style_theme'] == 'colorful':
|
| 154 |
+
css += """
|
| 155 |
+
body {
|
| 156 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 157 |
+
color: #ffffff;
|
| 158 |
+
}
|
| 159 |
+
"""
|
| 160 |
+
|
| 161 |
+
# Add component-specific styles
|
| 162 |
+
if components['has_header']:
|
| 163 |
+
css += """
|
| 164 |
+
.header {
|
| 165 |
+
background-color: #2c3e50;
|
| 166 |
+
color: white;
|
| 167 |
+
padding: 1rem 0;
|
| 168 |
+
position: sticky;
|
| 169 |
+
top: 0;
|
| 170 |
+
z-index: 100;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
.nav {
|
| 174 |
+
display: flex;
|
| 175 |
+
justify-content: space-between;
|
| 176 |
+
align-items: center;
|
| 177 |
+
max-width: 1200px;
|
| 178 |
+
margin: 0 auto;
|
| 179 |
+
padding: 0 2rem;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
.nav ul {
|
| 183 |
+
display: flex;
|
| 184 |
+
list-style: none;
|
| 185 |
+
gap: 2rem;
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
.nav a {
|
| 189 |
+
color: white;
|
| 190 |
+
text-decoration: none;
|
| 191 |
+
transition: color 0.3s;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
.nav a:hover {
|
| 195 |
+
color: #3498db;
|
| 196 |
+
}
|
| 197 |
+
"""
|
| 198 |
+
|
| 199 |
+
if components['has_hero']:
|
| 200 |
+
css += """
|
| 201 |
+
.hero {
|
| 202 |
+
background: linear-gradient(135deg, #3498db, #2c3e50);
|
| 203 |
+
color: white;
|
| 204 |
+
text-align: center;
|
| 205 |
+
padding: 8rem 2rem;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
.hero h1 {
|
| 209 |
+
font-size: 3rem;
|
| 210 |
+
margin-bottom: 1rem;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
.hero p {
|
| 214 |
+
font-size: 1.2rem;
|
| 215 |
+
margin-bottom: 2rem;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
.btn {
|
| 219 |
+
display: inline-block;
|
| 220 |
+
background-color: #e74c3c;
|
| 221 |
+
color: white;
|
| 222 |
+
padding: 1rem 2rem;
|
| 223 |
+
text-decoration: none;
|
| 224 |
+
border-radius: 5px;
|
| 225 |
+
transition: background-color 0.3s;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
.btn:hover {
|
| 229 |
+
background-color: #c0392b;
|
| 230 |
+
}
|
| 231 |
+
"""
|
| 232 |
+
|
| 233 |
+
if components['has_cards']:
|
| 234 |
+
css += """
|
| 235 |
+
.cards-section {
|
| 236 |
+
padding: 4rem 2rem;
|
| 237 |
+
max-width: 1200px;
|
| 238 |
+
margin: 0 auto;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
.cards-grid {
|
| 242 |
+
display: grid;
|
| 243 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 244 |
+
gap: 2rem;
|
| 245 |
+
margin-top: 2rem;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
.card {
|
| 249 |
+
background: white;
|
| 250 |
+
border-radius: 10px;
|
| 251 |
+
padding: 2rem;
|
| 252 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 253 |
+
transition: transform 0.3s, box-shadow 0.3s;
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
.card:hover {
|
| 257 |
+
transform: translateY(-5px);
|
| 258 |
+
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
|
| 259 |
+
}
|
| 260 |
+
"""
|
| 261 |
+
|
| 262 |
+
if components['has_form']:
|
| 263 |
+
css += """
|
| 264 |
+
.form-section {
|
| 265 |
+
padding: 4rem 2rem;
|
| 266 |
+
background-color: #f8f9fa;
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
.form-container {
|
| 270 |
+
max-width: 600px;
|
| 271 |
+
margin: 0 auto;
|
| 272 |
+
background: white;
|
| 273 |
+
padding: 2rem;
|
| 274 |
+
border-radius: 10px;
|
| 275 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
.form-group {
|
| 279 |
+
margin-bottom: 1.5rem;
|
| 280 |
+
}
|
| 281 |
+
|
| 282 |
+
.form-group label {
|
| 283 |
+
display: block;
|
| 284 |
+
margin-bottom: 0.5rem;
|
| 285 |
+
font-weight: bold;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
.form-group input,
|
| 289 |
+
.form-group textarea {
|
| 290 |
+
width: 100%;
|
| 291 |
+
padding: 0.75rem;
|
| 292 |
+
border: 1px solid #ddd;
|
| 293 |
+
border-radius: 5px;
|
| 294 |
+
font-size: 1rem;
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
.form-group input:focus,
|
| 298 |
+
.form-group textarea:focus {
|
| 299 |
+
outline: none;
|
| 300 |
+
border-color: #3498db;
|
| 301 |
+
box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
|
| 302 |
+
}
|
| 303 |
+
"""
|
| 304 |
+
|
| 305 |
+
if components['has_footer']:
|
| 306 |
+
css += """
|
| 307 |
+
.footer {
|
| 308 |
+
background-color: #2c3e50;
|
| 309 |
+
color: white;
|
| 310 |
+
text-align: center;
|
| 311 |
+
padding: 2rem 0;
|
| 312 |
+
margin-top: 4rem;
|
| 313 |
+
}
|
| 314 |
+
"""
|
| 315 |
+
|
| 316 |
+
# Add animations if requested
|
| 317 |
+
if components['has_animation']:
|
| 318 |
+
css += """
|
| 319 |
+
@keyframes fadeInUp {
|
| 320 |
+
from {
|
| 321 |
+
opacity: 0;
|
| 322 |
+
transform: translateY(30px);
|
| 323 |
+
}
|
| 324 |
+
to {
|
| 325 |
+
opacity: 1;
|
| 326 |
+
transform: translateY(0);
|
| 327 |
+
}
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
.animate-on-scroll {
|
| 331 |
+
animation: fadeInUp 0.6s ease-out;
|
| 332 |
+
}
|
| 333 |
+
"""
|
| 334 |
+
|
| 335 |
+
return css
|
| 336 |
+
|
| 337 |
+
def generate_html_structure(prompt: str, components: dict) -> str:
|
| 338 |
+
"""Generate HTML structure based on components."""
|
| 339 |
+
|
| 340 |
+
html = ""
|
| 341 |
+
|
| 342 |
+
# Header
|
| 343 |
+
if components['has_header']:
|
| 344 |
+
html += """
|
| 345 |
+
<header class="header">
|
| 346 |
+
<nav class="nav">
|
| 347 |
+
<div class="logo">
|
| 348 |
+
<h2>Your Brand</h2>
|
| 349 |
+
</div>
|
| 350 |
+
<ul>
|
| 351 |
+
<li><a href="#home">Home</a></li>
|
| 352 |
+
<li><a href="#about">About</a></li>
|
| 353 |
+
<li><a href="#services">Services</a></li>
|
| 354 |
+
<li><a href="#contact">Contact</a></li>
|
| 355 |
+
</ul>
|
| 356 |
+
</nav>
|
| 357 |
+
</header>
|
| 358 |
+
"""
|
| 359 |
+
|
| 360 |
+
# Hero section
|
| 361 |
+
if components['has_hero']:
|
| 362 |
+
html += """
|
| 363 |
+
<section class="hero" id="home">
|
| 364 |
+
<div class="hero-content">
|
| 365 |
+
<h1>Welcome to Our Amazing Service</h1>
|
| 366 |
+
<p>Transform your ideas into reality with our cutting-edge solutions</p>
|
| 367 |
+
<a href="#contact" class="btn">Get Started</a>
|
| 368 |
+
</div>
|
| 369 |
+
</section>
|
| 370 |
+
"""
|
| 371 |
+
|
| 372 |
+
# Cards section
|
| 373 |
+
if components['has_cards']:
|
| 374 |
+
html += """
|
| 375 |
+
<section class="cards-section" id="services">
|
| 376 |
+
<div class="container">
|
| 377 |
+
<h2 style="text-align: center; margin-bottom: 2rem;">Our Services</h2>
|
| 378 |
+
<div class="cards-grid">
|
| 379 |
+
<div class="card">
|
| 380 |
+
<h3>Service One</h3>
|
| 381 |
+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore.</p>
|
| 382 |
+
</div>
|
| 383 |
+
<div class="card">
|
| 384 |
+
<h3>Service Two</h3>
|
| 385 |
+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore.</p>
|
| 386 |
+
</div>
|
| 387 |
+
<div class="card">
|
| 388 |
+
<h3>Service Three</h3>
|
| 389 |
+
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore.</p>
|
| 390 |
+
</div>
|
| 391 |
+
</div>
|
| 392 |
+
</div>
|
| 393 |
+
</section>
|
| 394 |
+
"""
|
| 395 |
+
|
| 396 |
+
# Form section
|
| 397 |
+
if components['has_form']:
|
| 398 |
+
html += """
|
| 399 |
+
<section class="form-section" id="contact">
|
| 400 |
+
<div class="form-container">
|
| 401 |
+
<h2 style="text-align: center; margin-bottom: 2rem;">Contact Us</h2>
|
| 402 |
+
<form id="contactForm">
|
| 403 |
+
<div class="form-group">
|
| 404 |
+
<label for="name">Name:</label>
|
| 405 |
+
<input type="text" id="name" name="name" required>
|
| 406 |
+
</div>
|
| 407 |
+
<div class="form-group">
|
| 408 |
+
<label for="email">Email:</label>
|
| 409 |
+
<input type="email" id="email" name="email" required>
|
| 410 |
+
</div>
|
| 411 |
+
<div class="form-group">
|
| 412 |
+
<label for="message">Message:</label>
|
| 413 |
+
<textarea id="message" name="message" rows="5" required></textarea>
|
| 414 |
+
</div>
|
| 415 |
+
<button type="submit" class="btn" style="width: 100%;">Send Message</button>
|
| 416 |
+
</form>
|
| 417 |
+
</div>
|
| 418 |
+
</section>
|
| 419 |
+
"""
|
| 420 |
+
|
| 421 |
+
# Footer
|
| 422 |
+
if components['has_footer']:
|
| 423 |
+
html += """
|
| 424 |
+
<footer class="footer">
|
| 425 |
+
<div class="container">
|
| 426 |
+
<p>© 2024 Your Company. All rights reserved.</p>
|
| 427 |
+
</div>
|
| 428 |
+
</footer>
|
| 429 |
+
"""
|
| 430 |
+
|
| 431 |
+
return html
|
| 432 |
+
|
| 433 |
+
def generate_javascript_features(prompt: str, components: dict) -> str:
|
| 434 |
+
"""Generate JavaScript for interactivity."""
|
| 435 |
+
|
| 436 |
+
js = """
|
| 437 |
+
// Smooth scrolling for navigation links
|
| 438 |
+
document.addEventListener('DOMContentLoaded', function() {
|
| 439 |
+
const links = document.querySelectorAll('a[href^="#"]');
|
| 440 |
+
|
| 441 |
+
links.forEach(link => {
|
| 442 |
+
link.addEventListener('click', function(e) {
|
| 443 |
+
e.preventDefault();
|
| 444 |
+
const target = document.querySelector(this.getAttribute('href'));
|
| 445 |
+
if (target) {
|
| 446 |
+
target.scrollIntoView({
|
| 447 |
+
behavior: 'smooth',
|
| 448 |
+
block: 'start'
|
| 449 |
+
});
|
| 450 |
+
}
|
| 451 |
+
});
|
| 452 |
+
});
|
| 453 |
+
"""
|
| 454 |
+
|
| 455 |
+
# Add form handling if form exists
|
| 456 |
+
if components['has_form']:
|
| 457 |
+
js += """
|
| 458 |
+
// Form submission handling
|
| 459 |
+
const contactForm = document.getElementById('contactForm');
|
| 460 |
+
if (contactForm) {
|
| 461 |
+
contactForm.addEventListener('submit', function(e) {
|
| 462 |
+
e.preventDefault();
|
| 463 |
+
|
| 464 |
+
// Get form data
|
| 465 |
+
const formData = new FormData(this);
|
| 466 |
+
const name = formData.get('name');
|
| 467 |
+
const email = formData.get('email');
|
| 468 |
+
const message = formData.get('message');
|
| 469 |
+
|
| 470 |
+
// Simple validation
|
| 471 |
+
if (name && email && message) {
|
| 472 |
+
alert('Thank you for your message! We will get back to you soon.');
|
| 473 |
+
this.reset();
|
| 474 |
+
} else {
|
| 475 |
+
alert('Please fill in all fields.');
|
| 476 |
+
}
|
| 477 |
+
});
|
| 478 |
+
}
|
| 479 |
+
"""
|
| 480 |
+
|
| 481 |
+
# Add scroll animations if requested
|
| 482 |
+
if components['has_animation']:
|
| 483 |
+
js += """
|
| 484 |
+
// Scroll animations
|
| 485 |
+
const observerOptions = {
|
| 486 |
+
threshold: 0.1,
|
| 487 |
+
rootMargin: '0px 0px -50px 0px'
|
| 488 |
+
};
|
| 489 |
+
|
| 490 |
+
const observer = new IntersectionObserver((entries) => {
|
| 491 |
+
entries.forEach(entry => {
|
| 492 |
+
if (entry.isIntersecting) {
|
| 493 |
+
entry.target.classList.add('animate-on-scroll');
|
| 494 |
+
}
|
| 495 |
+
});
|
| 496 |
+
}, observerOptions);
|
| 497 |
+
|
| 498 |
+
// Observe cards and other elements
|
| 499 |
+
const animatedElements = document.querySelectorAll('.card, .hero-content');
|
| 500 |
+
animatedElements.forEach(el => observer.observe(el));
|
| 501 |
+
"""
|
| 502 |
+
|
| 503 |
+
js += """
|
| 504 |
+
});
|
| 505 |
+
"""
|
| 506 |
+
|
| 507 |
+
return js
|
| 508 |
+
|
| 509 |
+
def extract_title_from_prompt(prompt: str) -> str:
|
| 510 |
+
"""Extract a suitable title from the design prompt."""
|
| 511 |
+
prompt_lower = prompt.lower()
|
| 512 |
+
|
| 513 |
+
if 'dashboard' in prompt_lower:
|
| 514 |
+
return 'Dashboard'
|
| 515 |
+
elif 'portfolio' in prompt_lower:
|
| 516 |
+
return 'Portfolio'
|
| 517 |
+
elif 'landing' in prompt_lower or 'homepage' in prompt_lower:
|
| 518 |
+
return 'Welcome'
|
| 519 |
+
elif 'blog' in prompt_lower:
|
| 520 |
+
return 'Blog'
|
| 521 |
+
elif 'contact' in prompt_lower:
|
| 522 |
+
return 'Contact Us'
|
| 523 |
+
else:
|
| 524 |
+
return 'My Website'
|
| 525 |
|
| 526 |
@tool
|
| 527 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 528 |
"""A tool that fetches the current local time in a specified timezone.
|
| 529 |
+
|
| 530 |
Args:
|
| 531 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 532 |
"""
|
|
|
|
| 539 |
except Exception as e:
|
| 540 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 541 |
|
| 542 |
+
@tool
|
| 543 |
+
def image_analyzer_for_design(image_path: str, analysis_type: str = "design") -> str:
|
| 544 |
+
"""Analyze an uploaded image to extract design elements and generate code suggestions.
|
| 545 |
+
|
| 546 |
+
Args:
|
| 547 |
+
image_path: Path to the image file
|
| 548 |
+
analysis_type: Type of analysis to perform ('design', 'layout', 'colors')
|
| 549 |
+
"""
|
| 550 |
+
try:
|
| 551 |
+
# This is a placeholder for image analysis
|
| 552 |
+
# In a real implementation, you would use computer vision or AI models
|
| 553 |
+
return f"Image analysis for {image_path}: Detected modern layout with clean design elements. Suggested color scheme: blue and white. Layout appears to be a landing page with header, hero section, and cards."
|
| 554 |
+
except Exception as e:
|
| 555 |
+
return f"Error analyzing image: {str(e)}"
|
| 556 |
|
| 557 |
final_answer = FinalAnswerTool()
|
| 558 |
|
| 559 |
+
# Model configuration
|
|
|
|
|
|
|
| 560 |
model = HfApiModel(
|
| 561 |
+
max_tokens=2096,
|
| 562 |
+
temperature=0.5,
|
| 563 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 564 |
+
custom_role_conversions=None,
|
| 565 |
)
|
| 566 |
|
| 567 |
+
# Load additional tools
|
| 568 |
+
try:
|
| 569 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 570 |
+
except:
|
| 571 |
+
print("Image generation tool not available")
|
| 572 |
+
image_generation_tool = None
|
| 573 |
|
| 574 |
+
# Load prompts
|
|
|
|
|
|
|
| 575 |
with open("prompts.yaml", 'r') as stream:
|
| 576 |
prompt_templates = yaml.safe_load(stream)
|
| 577 |
+
|
| 578 |
+
# Create agent with enhanced tools
|
| 579 |
+
tools = [
|
| 580 |
+
final_answer,
|
| 581 |
+
design_to_code_generator,
|
| 582 |
+
get_current_time_in_timezone,
|
| 583 |
+
image_analyzer_for_design,
|
| 584 |
+
]
|
| 585 |
+
|
| 586 |
+
# Add image generation tool if available
|
| 587 |
+
if image_generation_tool:
|
| 588 |
+
tools.append(image_generation_tool)
|
| 589 |
+
|
| 590 |
agent = CodeAgent(
|
| 591 |
model=model,
|
| 592 |
+
tools=tools,
|
| 593 |
max_steps=6,
|
| 594 |
verbosity_level=1,
|
| 595 |
grammar=None,
|
| 596 |
planning_interval=None,
|
| 597 |
+
name="Design-to-Code Agent",
|
| 598 |
+
description="An AI agent that converts design descriptions and images into functional HTML, CSS, and JavaScript code.",
|
| 599 |
prompt_templates=prompt_templates
|
| 600 |
)
|
| 601 |
|
| 602 |
+
# Launch the Gradio interface
|
| 603 |
GradioUI(agent).launch()
|