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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from crewai import Agent, Task, Crew, Process
|
| 4 |
+
from crewai_tools import DuckDuckGoSearchRunTool, FileWriteTool
|
| 5 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 6 |
+
|
| 7 |
+
# --- 1. SET UP THE GEMINI LLM ---
|
| 8 |
+
# IMPORTANT: Set your Google API key in your environment variables.
|
| 9 |
+
# For local testing, create a .env file with: GOOGLE_API_KEY="your_key_here"
|
| 10 |
+
# On Hugging Face Spaces, set this in the "Secrets" section.
|
| 11 |
+
try:
|
| 12 |
+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash",
|
| 13 |
+
verbose=True,
|
| 14 |
+
temperature=0.6,
|
| 15 |
+
google_api_key=os.environ.get("GOOGLE_API_KEY"))
|
| 16 |
+
except Exception as e:
|
| 17 |
+
print("\n\n---")
|
| 18 |
+
print("Error initializing Gemini LLM. Make sure you have set your GOOGLE_API_KEY.")
|
| 19 |
+
print("See the README for instructions on how to set up your API key.")
|
| 20 |
+
print(f"Error: {e}")
|
| 21 |
+
print("---\n\n")
|
| 22 |
+
llm = None # Set llm to None to prevent further errors
|
| 23 |
+
|
| 24 |
+
# --- 2. DEFINE TOOLS AND OUTPUT DIRECTORY ---
|
| 25 |
+
search_tool = DuckDuckGoSearchRunTool()
|
| 26 |
+
os.makedirs("outputs", exist_ok=True)
|
| 27 |
+
file_write_tool = FileWriteTool(file_path="outputs/index.html")
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# --- 3. DEFINE THE AGENT TEAM ---
|
| 31 |
+
|
| 32 |
+
# Agent 1: The Brand Designer
|
| 33 |
+
designer = Agent(
|
| 34 |
+
role='Principal Brand Designer',
|
| 35 |
+
goal='Translate a user\'s "vibe" into a concrete, professional design brief for a website.',
|
| 36 |
+
backstory=(
|
| 37 |
+
"You are a world-class brand designer with a keen eye for aesthetics. "
|
| 38 |
+
"You excel at understanding abstract concepts and turning them into actionable design elements like color palettes, font pairings, and page structures."
|
| 39 |
+
),
|
| 40 |
+
verbose=True,
|
| 41 |
+
llm=llm,
|
| 42 |
+
tools=[search_tool],
|
| 43 |
+
allow_delegation=False
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Agent 2: The UI/UX Copywriter
|
| 47 |
+
copywriter = Agent(
|
| 48 |
+
role='Senior UX Copywriter',
|
| 49 |
+
goal='Write compelling, vibe-appropriate copy for all sections of a website based on a design brief.',
|
| 50 |
+
backstory=(
|
| 51 |
+
"You are an expert copywriter who specializes in creating website content that resonates with the target audience. "
|
| 52 |
+
"You know how to craft engaging headlines, informative text, and clear calls-to-action that perfectly match the brand's voice."
|
| 53 |
+
),
|
| 54 |
+
verbose=True,
|
| 55 |
+
llm=llm,
|
| 56 |
+
allow_delegation=False
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
# Agent 3: The Frontend Developer
|
| 60 |
+
developer = Agent(
|
| 61 |
+
role='Senior Frontend Developer',
|
| 62 |
+
goal='Take a design brief and content map to build a complete, self-contained HTML file with embedded CSS and JavaScript.',
|
| 63 |
+
backstory=(
|
| 64 |
+
"You are a skilled frontend developer who can build beautiful and functional websites from scratch. "
|
| 65 |
+
"You are a master of HTML, CSS, and JavaScript, and you specialize in creating single-file web pages that are easy to deploy and view."
|
| 66 |
+
),
|
| 67 |
+
verbose=True,
|
| 68 |
+
llm=llm,
|
| 69 |
+
tools=[file_write_tool],
|
| 70 |
+
allow_delegation=False
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# --- 4. DEFINE THE WORKFLOW (TASKS) ---
|
| 75 |
+
|
| 76 |
+
# Task 1: Create the Design Brief
|
| 77 |
+
design_task = Task(
|
| 78 |
+
description=(
|
| 79 |
+
"Analyze the user's prompt: '{prompt}' to understand the desired vibe. "
|
| 80 |
+
"Conduct a web search for inspiration on color palettes, font pairings, and overall aesthetics that match this vibe. "
|
| 81 |
+
"Create a comprehensive 'Design Brief' document that includes a color palette (with hex codes), a font pairing (with Google Fonts links), and a detailed page structure (e.g., Navbar, Hero, About, Menu, Contact)."
|
| 82 |
+
),
|
| 83 |
+
expected_output=(
|
| 84 |
+
"A structured markdown document containing the complete Design Brief, including sections for Color Palette, Font Pairing, and Page Structure."
|
| 85 |
+
),
|
| 86 |
+
agent=designer
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Task 2: Write the Website Content
|
| 90 |
+
copywriting_task = Task(
|
| 91 |
+
description=(
|
| 92 |
+
"Using the Design Brief provided, write all the necessary text content for the website. "
|
| 93 |
+
"Ensure the tone of the copy perfectly matches the specified vibe. Create placeholder content for all sections, including the menu and contact form."
|
| 94 |
+
),
|
| 95 |
+
expected_output=(
|
| 96 |
+
"A structured markdown document called a 'Content Map' that lists the text content for each section of the website (e.g., hero_title, about_text, menu_items)."
|
| 97 |
+
),
|
| 98 |
+
agent=copywriter,
|
| 99 |
+
context=[design_task] # This task depends on the design_task
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
# Task 3: Build the HTML Website
|
| 103 |
+
development_task = Task(
|
| 104 |
+
description=(
|
| 105 |
+
"Based on the final Design Brief and the Content Map, create a complete, single-file HTML website. "
|
| 106 |
+
"The HTML file must be self-contained, with all CSS and any simple JavaScript embedded within the file in <style> and <script> tags. "
|
| 107 |
+
"Use the specified color palette and Google Fonts. Populate the HTML with the exact content from the Content Map. "
|
| 108 |
+
"Your final output MUST be only the code for the HTML file."
|
| 109 |
+
),
|
| 110 |
+
expected_output=(
|
| 111 |
+
"The complete and final HTML code for the website, written to the file 'outputs/index.html'."
|
| 112 |
+
),
|
| 113 |
+
agent=developer,
|
| 114 |
+
context=[design_task, copywriting_task], # This task depends on the previous two
|
| 115 |
+
tools=[file_write_tool]
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# --- 5. ASSEMBLE THE CREW ---
|
| 120 |
+
vibe_crew = Crew(
|
| 121 |
+
agents=[designer, copywriter, developer],
|
| 122 |
+
tasks=[design_task, copywriting_task, development_task],
|
| 123 |
+
process=Process.sequential,
|
| 124 |
+
verbose=2
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# --- 6. CREATE THE GRADIO WEB INTERFACE ---
|
| 129 |
+
def run_crew(prompt):
|
| 130 |
+
if llm is None:
|
| 131 |
+
return "Error: Gemini LLM not initialized. Please check your API key.", None, None
|
| 132 |
+
|
| 133 |
+
# Clear previous output file
|
| 134 |
+
if os.path.exists("outputs/index.html"):
|
| 135 |
+
os.remove("outputs/index.html")
|
| 136 |
+
|
| 137 |
+
inputs = {'prompt': prompt}
|
| 138 |
+
result = vibe_crew.kickoff(inputs=inputs)
|
| 139 |
+
|
| 140 |
+
# Read the generated HTML file
|
| 141 |
+
try:
|
| 142 |
+
with open("outputs/index.html", "r") as file:
|
| 143 |
+
html_content = file.read()
|
| 144 |
+
return result, html_content, "outputs/index.html"
|
| 145 |
+
except FileNotFoundError:
|
| 146 |
+
return result, "Error: Output file not found. The developer agent might have failed.", None
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
iface = gr.Interface(
|
| 150 |
+
fn=run_crew,
|
| 151 |
+
inputs=gr.Textbox(
|
| 152 |
+
lines=3,
|
| 153 |
+
placeholder="e.g., A website for my new coffee shop in Brooklyn. The vibe should be cozy, rustic, and artisanal."
|
| 154 |
+
),
|
| 155 |
+
outputs=[
|
| 156 |
+
gr.Markdown(label="Crew Final Report"),
|
| 157 |
+
gr.Code(label="Generated HTML Code", language="html"),
|
| 158 |
+
gr.File(label="Download Website")
|
| 159 |
+
],
|
| 160 |
+
title="🎨 Vibe Coder AI",
|
| 161 |
+
description="Enter the 'vibe' for your website, and this multi-agent team will design, write, and code a complete webpage for you using Gemini.",
|
| 162 |
+
examples=[
|
| 163 |
+
["A personal portfolio for a photographer. The vibe should be minimalist, modern, and clean."],
|
| 164 |
+
["A landing page for a new tech startup focused on AI. The vibe is futuristic, sleek, and professional."],
|
| 165 |
+
["A website for a local bakery. The vibe is warm, friendly, and homemade."]
|
| 166 |
+
],
|
| 167 |
+
allow_flagging="never"
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
iface.launch()
|