faizee07 commited on
Commit
dddc9c1
Β·
verified Β·
1 Parent(s): 4845b21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -50
app.py CHANGED
@@ -132,10 +132,20 @@ vibe_crew = Crew(
132
  def run_crew(prompt):
133
  try:
134
  if llm is None:
135
- return "❌ **Error:** Gemini LLM not initialized. Please check your GOOGLE_API_KEY in environment variables.", None, None
 
 
 
 
 
136
 
137
  if not prompt or prompt.strip() == "":
138
- return "❌ **Error:** Please enter a prompt describing your desired website vibe.", None, None
 
 
 
 
 
139
 
140
  # Clean up previous output
141
  if os.path.exists("outputs/index.html"):
@@ -151,85 +161,139 @@ def run_crew(prompt):
151
  html_content = file.read()
152
 
153
  if html_content.strip():
 
 
 
 
 
154
  return (
155
- f"βœ… **Success!** Your website has been generated.\n\n{str(result)}",
156
- html_content,
157
- "outputs/index.html"
 
158
  )
159
  else:
160
  return (
161
- f"⚠️ **Warning:** File created but is empty.\n\n{str(result)}",
162
- "<!-- No content generated -->",
163
- None
 
164
  )
165
  else:
166
  return (
167
- f"❌ **Error:** Output file not found. The developer agent might have failed.\n\n{str(result)}",
168
- "<!-- File not created -->",
169
- None
 
170
  )
171
 
172
  except Exception as e:
173
  error_trace = traceback.format_exc()
174
- error_message = f"❌ **Error occurred during execution:**\n\n```\n{error_trace}\n```"
175
  print(error_trace) # Also print to console
176
- return error_message, f"<!-- Error: {str(e)} -->", None
 
 
 
 
 
177
 
178
  # --- 7. CREATE THE GRADIO INTERFACE WITH CUSTOM LAYOUT ---
179
- with gr.Blocks(theme=gr.themes.Soft()) as iface:
 
 
 
 
 
180
  gr.Markdown(
181
  """
182
  # 🎨 Vibe Coder AI
183
- Enter the 'vibe' for your website, and this multi-agent team will design, write, and code a complete webpage for you using Gemini.
 
184
  """
185
  )
186
 
187
  with gr.Row():
188
- with gr.Column():
189
- prompt_input = gr.Textbox(
190
- lines=5,
191
- placeholder="e.g., A website for my new coffee shop in Brooklyn. The vibe should be cozy, rustic, and artisanal.",
192
- label="Describe Your Website Vibe"
193
- )
194
-
195
- generate_btn = gr.Button("πŸš€ Generate Website", variant="primary", size="lg")
196
-
197
- gr.Examples(
198
- examples=[
199
- ["A personal portfolio for a photographer. The vibe should be minimalist, modern, and clean."],
200
- ["A landing page for a new tech startup focused on AI. Vibe: futuristic, sleek, professional."],
201
- ["A website for a local bakery. Vibe: warm, friendly, and homemade."]
202
- ],
203
- inputs=prompt_input
204
- )
205
 
206
  with gr.Row():
207
- report_output = gr.Markdown(label="Crew Report")
208
 
209
  with gr.Row():
210
- with gr.Column():
211
- code_output = gr.Code(label="Generated HTML Code", language="html", lines=20)
212
- with gr.Column():
213
- html_preview = gr.HTML(label="Live Preview")
 
 
 
 
 
 
214
 
215
  with gr.Row():
216
- download_file = gr.File(label="πŸ“₯ Download Your Website", visible=True)
 
 
 
 
217
 
218
- # Define the generation function with preview
219
- def generate_and_preview(prompt):
220
- report, code, file_path = run_crew(prompt)
221
-
222
- # Create a preview iframe if HTML was generated successfully
223
- preview_html = ""
224
- if code and code.strip() and not code.startswith("<!--"):
225
- preview_html = f'<iframe srcdoc="{code.replace(chr(34), "&quot;")}" width="100%" height="600px" style="border: 1px solid #ccc; border-radius: 8px;"></iframe>'
226
-
227
- return report, code, file_path, preview_html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
228
 
229
  generate_btn.click(
230
- fn=generate_and_preview,
231
  inputs=[prompt_input],
232
- outputs=[report_output, code_output, download_file, html_preview]
 
 
 
 
 
 
233
  )
234
 
235
  if __name__ == "__main__":
 
132
  def run_crew(prompt):
133
  try:
134
  if llm is None:
135
+ return (
136
+ "❌ **Error:** Gemini LLM not initialized. Please check your GOOGLE_API_KEY in environment variables.",
137
+ None,
138
+ gr.update(visible=False),
139
+ gr.update(visible=False)
140
+ )
141
 
142
  if not prompt or prompt.strip() == "":
143
+ return (
144
+ "❌ **Error:** Please enter a prompt describing your desired website vibe.",
145
+ None,
146
+ gr.update(visible=False),
147
+ gr.update(visible=False)
148
+ )
149
 
150
  # Clean up previous output
151
  if os.path.exists("outputs/index.html"):
 
161
  html_content = file.read()
162
 
163
  if html_content.strip():
164
+ # Create preview
165
+ preview_html = f'<iframe srcdoc="{html_content.replace(chr(34), "&quot;")}" width="100%" height="800px" style="border: 2px solid #e0e0e0; border-radius: 8px;"></iframe>'
166
+
167
+ success_message = "βœ… **Success!** Your website has been generated and is ready to download."
168
+
169
  return (
170
+ success_message,
171
+ preview_html,
172
+ gr.update(value="outputs/index.html", visible=True),
173
+ gr.update(visible=True)
174
  )
175
  else:
176
  return (
177
+ "⚠️ **Warning:** File created but is empty. Please try again.",
178
+ None,
179
+ gr.update(visible=False),
180
+ gr.update(visible=False)
181
  )
182
  else:
183
  return (
184
+ "❌ **Error:** Output file not found. The developer agent might have failed. Please try again.",
185
+ None,
186
+ gr.update(visible=False),
187
+ gr.update(visible=False)
188
  )
189
 
190
  except Exception as e:
191
  error_trace = traceback.format_exc()
192
+ error_message = f"❌ **Error occurred during execution:**\n\n```python\n{str(e)}\n```\n\nPlease try again or check your API keys."
193
  print(error_trace) # Also print to console
194
+ return (
195
+ error_message,
196
+ None,
197
+ gr.update(visible=False),
198
+ gr.update(visible=False)
199
+ )
200
 
201
  # --- 7. CREATE THE GRADIO INTERFACE WITH CUSTOM LAYOUT ---
202
+ with gr.Blocks(theme=gr.themes.Soft(), css="""
203
+ .container { max-width: 1400px; margin: auto; }
204
+ .status-box { padding: 20px; border-radius: 8px; margin: 10px 0; }
205
+ .download-section { text-align: center; padding: 20px; }
206
+ """) as iface:
207
+
208
  gr.Markdown(
209
  """
210
  # 🎨 Vibe Coder AI
211
+ ### Create beautiful websites with AI - Just describe your vision!
212
+ Enter the 'vibe' for your website, and our multi-agent team will design, write, and code a complete webpage for you.
213
  """
214
  )
215
 
216
  with gr.Row():
217
+ prompt_input = gr.Textbox(
218
+ lines=4,
219
+ placeholder="Example: A website for my new coffee shop in Brooklyn. The vibe should be cozy, rustic, and artisanal.",
220
+ label="✍️ Describe Your Website Vibe",
221
+ elem_classes="container"
222
+ )
 
 
 
 
 
 
 
 
 
 
 
223
 
224
  with gr.Row():
225
+ generate_btn = gr.Button("πŸš€ Generate Website", variant="primary", size="lg", scale=1)
226
 
227
  with gr.Row():
228
+ gr.Examples(
229
+ examples=[
230
+ ["A personal portfolio for a photographer. The vibe should be minimalist, modern, and clean."],
231
+ ["A landing page for a new tech startup focused on AI. Vibe: futuristic, sleek, professional."],
232
+ ["A website for a local bakery. Vibe: warm, friendly, and homemade."],
233
+ ["A fitness coach website. Vibe: energetic, motivating, and bold."]
234
+ ],
235
+ inputs=prompt_input,
236
+ label="πŸ’‘ Try these examples"
237
+ )
238
 
239
  with gr.Row():
240
+ status_output = gr.Markdown(
241
+ value="",
242
+ label="Status",
243
+ elem_classes="status-box"
244
+ )
245
 
246
+ with gr.Row():
247
+ preview_output = gr.HTML(
248
+ label="πŸ–₯️ Live Preview",
249
+ visible=True
250
+ )
251
+
252
+ with gr.Row():
253
+ with gr.Column(scale=1):
254
+ download_btn = gr.File(
255
+ label="πŸ“₯ Download Website (HTML)",
256
+ visible=False,
257
+ elem_classes="download-section"
258
+ )
259
+ with gr.Column(scale=1):
260
+ view_code_btn = gr.Button(
261
+ "πŸ‘οΈ View Source Code",
262
+ visible=False,
263
+ variant="secondary"
264
+ )
265
+
266
+ # Hidden code display (only shown when user clicks "View Source Code")
267
+ with gr.Row(visible=False) as code_row:
268
+ code_output = gr.Code(
269
+ label="πŸ“„ Generated HTML Source Code",
270
+ language="html",
271
+ lines=15,
272
+ elem_classes="container"
273
+ )
274
+
275
+ def toggle_code_view():
276
+ return gr.update(visible=True)
277
+
278
+ def generate_website(prompt):
279
+ status, preview, download, view_btn = run_crew(prompt)
280
+ return (
281
+ status,
282
+ preview,
283
+ download,
284
+ view_btn
285
+ )
286
 
287
  generate_btn.click(
288
+ fn=generate_website,
289
  inputs=[prompt_input],
290
+ outputs=[status_output, preview_output, download_btn, view_code_btn]
291
+ )
292
+
293
+ view_code_btn.click(
294
+ fn=lambda: (gr.update(visible=True), open("outputs/index.html", "r", encoding="utf-8").read() if os.path.exists("outputs/index.html") else ""),
295
+ inputs=None,
296
+ outputs=[code_row, code_output]
297
  )
298
 
299
  if __name__ == "__main__":