parrotmaker commited on
Commit
299f2c4
Β·
verified Β·
1 Parent(s): f80e4d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -30
app.py CHANGED
@@ -1,41 +1,84 @@
1
  import gradio as gr
 
 
 
2
 
3
- # Placeholder assistant function
4
- def assistant_response(message):
5
- return f"🧠 Assistant: You said '{message}'"
6
 
7
- # (Optional) Run code
8
- def run_code(code):
9
- try:
10
- exec_globals = {}
11
- exec(code, exec_globals)
12
- return "βœ… Code executed successfully (no visible output)"
13
- except Exception as e:
14
- return f"❌ Error:\n{e}"
 
 
 
 
 
 
 
15
 
16
- with gr.Blocks() as demo:
17
- with gr.Row():
18
- # LEFT COLUMN - AI CHAT
19
- with gr.Column(scale=1):
20
- gr.Markdown("### πŸ€– AI Assistant")
21
- chat = gr.Chatbot()
22
- msg = gr.Textbox(placeholder="Ask for help...", label="Your Message")
23
- send = gr.Button("Send")
 
 
 
 
 
 
 
24
 
25
- def handle_chat(user_message, history):
26
- reply = assistant_response(user_message)
27
- history.append((user_message, reply))
28
- return "", history
 
 
 
 
29
 
30
- send.click(handle_chat, [msg, chat], [msg, chat])
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # RIGHT COLUMN - CODE EDITOR
33
  with gr.Column(scale=2):
34
- gr.Markdown("### πŸ’» Code Canvas")
35
- editor = gr.Code(label="Your Code Here", language="python")
36
- run_btn = gr.Button("Run Code")
37
- output = gr.Textbox(label="Output")
 
 
 
 
 
 
 
 
 
38
 
39
- run_btn.click(run_code, inputs=editor, outputs=output)
 
 
 
40
 
41
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ import zipfile
4
+ import io
5
 
6
+ # Load AI assistant (can change model to faster one if needed)
7
+ assistant = pipeline("text-generation", model="mistralai/Mixtral-8x7B-Instruct-v0.1", max_new_tokens=1024)
 
8
 
9
+ def generate_code(prompt):
10
+ formatted = f"Create a full website. Include:\n[HTML]\n...HTML here...\n[CSS]\n...CSS here...\n[JS]\n...JS here...\nUser Prompt: {prompt}"
11
+ response = assistant(formatted)[0]["generated_text"]
12
+
13
+ # Try to split out HTML, CSS, JS sections
14
+ html = css = js = ""
15
+ if "[HTML]" in response and "[CSS]" in response and "[JS]" in response:
16
+ try:
17
+ html = response.split("[HTML]")[1].split("[CSS]")[0].strip()
18
+ css = response.split("[CSS]")[1].split("[JS]")[0].strip()
19
+ js = response.split("[JS]")[1].strip()
20
+ except:
21
+ html = response
22
+ else:
23
+ html = response
24
 
25
+ return html, css, js, f"βœ… AI generated your website!"
26
+
27
+ def combine_code(html, css, js):
28
+ return f"""
29
+ <!DOCTYPE html>
30
+ <html>
31
+ <head>
32
+ <style>{css}</style>
33
+ </head>
34
+ <body>
35
+ {html}
36
+ <script>{js}</script>
37
+ </body>
38
+ </html>
39
+ """
40
 
41
+ def export_zip(html, css, js):
42
+ in_memory = io.BytesIO()
43
+ with zipfile.ZipFile(in_memory, 'w') as zf:
44
+ zf.writestr("index.html", f"<!DOCTYPE html><html><head><link rel='stylesheet' href='style.css'></head><body>\n{html}\n<script src='script.js'></script></body></html>")
45
+ zf.writestr("style.css", css)
46
+ zf.writestr("script.js", js)
47
+ in_memory.seek(0)
48
+ return (in_memory, "website.zip")
49
 
50
+ with gr.Blocks(css="""
51
+ body { font-family: 'Courier New', monospace; background-color: #1e1e1e; color: white; }
52
+ .gr-button { background-color: #007acc; color: white; border: none; }
53
+ .gr-code textarea { background-color: #252526; color: white; font-size: 14px; }
54
+ .gr-textbox textarea { background-color: #1e1e1e; color: white; }
55
+ .gr-markdown h2 { color: #61dafb; }
56
+ """) as demo:
57
+ with gr.Row():
58
+ with gr.Column(scale=1):
59
+ gr.Markdown("## πŸ€– AI Assistant")
60
+ prompt = gr.Textbox(label="Describe the website you want", placeholder="e.g., A landing page for a pet adoption service")
61
+ generate_btn = gr.Button("✨ Generate Website")
62
+ status = gr.Textbox(label="Status")
63
 
 
64
  with gr.Column(scale=2):
65
+ gr.Markdown("## πŸ’» Code Editor (HTML / CSS / JS)")
66
+ with gr.Tabs():
67
+ with gr.Tab("HTML"):
68
+ html_editor = gr.Code(language="html", label="HTML")
69
+ with gr.Tab("CSS"):
70
+ css_editor = gr.Code(language="css", label="CSS")
71
+ with gr.Tab("JavaScript"):
72
+ js_editor = gr.Code(language="javascript", label="JavaScript")
73
+
74
+ run_btn = gr.Button("▢️ Preview")
75
+ preview = gr.HTML()
76
+ zip_btn = gr.Button("πŸ“¦ Export as ZIP")
77
+ zip_file = gr.File()
78
 
79
+ # Button Actions
80
+ generate_btn.click(generate_code, inputs=prompt, outputs=[html_editor, css_editor, js_editor, status])
81
+ run_btn.click(combine_code, inputs=[html_editor, css_editor, js_editor], outputs=preview)
82
+ zip_btn.click(export_zip, inputs=[html_editor, css_editor, js_editor], outputs=zip_file)
83
 
84
  demo.launch()