faizee07 commited on
Commit
8d73e72
·
verified ·
1 Parent(s): e09b313

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -51
app.py CHANGED
@@ -1,13 +1,13 @@
1
  import os
2
  import gradio as gr
3
  from crewai import Agent, Task, Crew, Process
4
- from crewai_tools import DuckDuckGoSearchTool, FileWriteTool # CORRECTED IMPORT
 
 
 
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,
@@ -16,53 +16,43 @@ try:
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 = DuckDuckGoSearchTool() # CORRECTED TOOL NAME
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,
@@ -70,52 +60,43 @@ developer = Agent(
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],
@@ -124,20 +105,14 @@ vibe_crew = Crew(
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()
@@ -145,7 +120,6 @@ def run_crew(prompt):
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(
@@ -161,12 +135,11 @@ iface = gr.Interface(
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()
172
-
 
1
  import os
2
  import gradio as gr
3
  from crewai import Agent, Task, Crew, Process
4
+ # CORRECTED IMPORTS FOR THE STABLE VERSIONS
5
+ from crewai_tools.tools.search_tools import DuckDuckGoSearchTool
6
+ from crewai_tools.tools.file_tools import FileWriteTool
7
+ # ---
8
  from langchain_google_genai import ChatGoogleGenerativeAI
9
 
10
  # --- 1. SET UP THE GEMINI LLM ---
 
 
 
11
  try:
12
  llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash",
13
  verbose=True,
 
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(f"Error: {e}")
20
  print("---\n\n")
21
+ llm = None
22
 
23
  # --- 2. DEFINE TOOLS AND OUTPUT DIRECTORY ---
24
+ search_tool = DuckDuckGoSearchTool()
25
  os.makedirs("outputs", exist_ok=True)
26
  file_write_tool = FileWriteTool(file_path="outputs/index.html")
27
 
 
28
  # --- 3. DEFINE THE AGENT TEAM ---
 
 
29
  designer = Agent(
30
  role='Principal Brand Designer',
31
  goal='Translate a user\'s "vibe" into a concrete, professional design brief for a website.',
32
  backstory=(
33
  "You are a world-class brand designer with a keen eye for aesthetics. "
34
+ "You excel at understanding abstract concepts and turning them into actionable design elements."
35
  ),
36
  verbose=True,
37
  llm=llm,
38
  tools=[search_tool],
39
  allow_delegation=False
40
  )
 
 
41
  copywriter = Agent(
42
  role='Senior UX Copywriter',
43
  goal='Write compelling, vibe-appropriate copy for all sections of a website based on a design brief.',
44
  backstory=(
45
+ "You are an expert copywriter who specializes in creating website content that resonates with the target audience."
 
46
  ),
47
  verbose=True,
48
  llm=llm,
49
  allow_delegation=False
50
  )
 
 
51
  developer = Agent(
52
  role='Senior Frontend Developer',
53
+ goal='Take a design brief and content map to build a complete, self-contained HTML file.',
54
  backstory=(
55
+ "You are a skilled frontend developer who can build beautiful and functional websites from scratch."
 
56
  ),
57
  verbose=True,
58
  llm=llm,
 
60
  allow_delegation=False
61
  )
62
 
 
63
  # --- 4. DEFINE THE WORKFLOW (TASKS) ---
 
 
64
  design_task = Task(
65
  description=(
66
  "Analyze the user's prompt: '{prompt}' to understand the desired vibe. "
67
+ "Conduct a web search for inspiration on color palettes, font pairings, and overall aesthetics. "
68
+ "Create a comprehensive 'Design Brief' document with a color palette (hex codes), font pairing (Google Fonts), and a detailed page structure."
69
  ),
70
  expected_output=(
71
+ "A structured markdown document with the complete Design Brief (Color Palette, Font Pairing, Page Structure)."
72
  ),
73
  agent=designer
74
  )
 
 
75
  copywriting_task = Task(
76
  description=(
77
+ "Using the Design Brief, write all text content for the website. "
78
+ "Ensure the tone matches the vibe. Create placeholder content for all sections."
79
  ),
80
  expected_output=(
81
+ "A structured markdown 'Content Map' with text for each section of the website."
82
  ),
83
  agent=copywriter,
84
+ context=[design_task]
85
  )
 
 
86
  development_task = Task(
87
  description=(
88
+ "Based on the Design Brief and Content Map, create a single-file HTML website. "
89
+ "The file must be self-contained with embedded CSS and any simple JavaScript. "
90
+ "Use the specified colors and Google Fonts. Your final output MUST be only the code for the HTML file."
 
91
  ),
92
  expected_output=(
93
+ "The complete HTML code for the website, written to 'outputs/index.html'."
94
  ),
95
  agent=developer,
96
+ context=[design_task, copywriting_task],
97
  tools=[file_write_tool]
98
  )
99
 
 
100
  # --- 5. ASSEMBLE THE CREW ---
101
  vibe_crew = Crew(
102
  agents=[designer, copywriter, developer],
 
105
  verbose=2
106
  )
107
 
 
108
  # --- 6. CREATE THE GRADIO WEB INTERFACE ---
109
  def run_crew(prompt):
110
  if llm is None:
111
  return "Error: Gemini LLM not initialized. Please check your API key.", None, None
 
 
112
  if os.path.exists("outputs/index.html"):
113
  os.remove("outputs/index.html")
 
114
  inputs = {'prompt': prompt}
115
  result = vibe_crew.kickoff(inputs=inputs)
 
 
116
  try:
117
  with open("outputs/index.html", "r") as file:
118
  html_content = file.read()
 
120
  except FileNotFoundError:
121
  return result, "Error: Output file not found. The developer agent might have failed.", None
122
 
 
123
  iface = gr.Interface(
124
  fn=run_crew,
125
  inputs=gr.Textbox(
 
135
  description="Enter the 'vibe' for your website, and this multi-agent team will design, write, and code a complete webpage for you using Gemini.",
136
  examples=[
137
  ["A personal portfolio for a photographer. The vibe should be minimalist, modern, and clean."],
138
+ ["A landing page for a new tech startup focused on AI. Vibe: futuristic, sleek, professional."],
139
+ ["A website for a local bakery. Vibe: warm, friendly, and homemade."]
140
  ],
141
  allow_flagging="never"
142
  )
143
 
144
  if __name__ == "__main__":
145
  iface.launch()