Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,84 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
#
|
4 |
-
|
5 |
-
return f"π§ Assistant: You said '{message}'"
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
# RIGHT COLUMN - CODE EDITOR
|
33 |
with gr.Column(scale=2):
|
34 |
-
gr.Markdown("
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
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()
|