Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,30 +3,15 @@ from transformers import pipeline
|
|
3 |
import zipfile
|
4 |
import io
|
5 |
|
6 |
-
# Lightweight open model
|
7 |
assistant = pipeline("text-generation", model="Salesforce/codegen-350M-multi", max_new_tokens=512)
|
8 |
|
9 |
def generate_code(prompt, html, css, js):
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
# Append output to current HTML
|
14 |
-
new_html = html + "\n<!-- AI Suggestion -->\n" + result
|
15 |
-
return new_html, css, js, "✅ AI suggestion appended."
|
16 |
|
17 |
def combine_code(html, css, js):
|
18 |
-
return f"""
|
19 |
-
<!DOCTYPE html>
|
20 |
-
<html>
|
21 |
-
<head>
|
22 |
-
<style>{css}</style>
|
23 |
-
</head>
|
24 |
-
<body>
|
25 |
-
{html}
|
26 |
-
<script>{js}</script>
|
27 |
-
</body>
|
28 |
-
</html>
|
29 |
-
"""
|
30 |
|
31 |
def export_zip(html, css, js):
|
32 |
mem = io.BytesIO()
|
@@ -37,57 +22,41 @@ def export_zip(html, css, js):
|
|
37 |
mem.seek(0)
|
38 |
return (mem, "website.zip")
|
39 |
|
40 |
-
with gr.Blocks(
|
41 |
-
body { font-family: 'Inter', sans-serif; background-color: #1e1e1e; color: #ffffff; }
|
42 |
-
.gr-button { background-color: #007acc; color: white; border: none; border-radius: 8px; margin-right: 0.5rem; }
|
43 |
-
.gr-textbox textarea, .gr-code textarea { background-color: #252526; color: white; font-size: 14px; }
|
44 |
-
.gr-markdown h2 { color: #61dafb; }
|
45 |
-
.gr-tabitem { background-color: #1e1e1e; }
|
46 |
-
""") as demo:
|
47 |
-
|
48 |
with gr.Row():
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
gr.
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
65 |
|
66 |
with gr.Tabs():
|
67 |
-
with gr.Tab("
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
with gr.Tab("JS"):
|
74 |
-
js_editor = gr.Code(language="javascript", label="JavaScript")
|
75 |
with gr.Tab("Preview"):
|
76 |
live_preview = gr.HTML()
|
77 |
|
78 |
-
#
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
[html_editor, css_editor, js_editor, ai_status]
|
83 |
-
)
|
84 |
-
generate_btn.click(
|
85 |
-
generate_code,
|
86 |
-
[user_prompt, html_editor, css_editor, js_editor],
|
87 |
-
ai_log
|
88 |
-
)
|
89 |
-
|
90 |
-
preview_btn.click(combine_code, [html_editor, css_editor, js_editor], live_preview)
|
91 |
-
export_btn.click(export_zip, [html_editor, css_editor, js_editor], zip_out)
|
92 |
|
93 |
demo.launch()
|
|
|
3 |
import zipfile
|
4 |
import io
|
5 |
|
|
|
6 |
assistant = pipeline("text-generation", model="Salesforce/codegen-350M-multi", max_new_tokens=512)
|
7 |
|
8 |
def generate_code(prompt, html, css, js):
|
9 |
+
result = assistant(f"# Task: {prompt}\n")[0]["generated_text"]
|
10 |
+
new_html = html + "\n<!-- AI edit -->\n" + result
|
11 |
+
return new_html, css, js
|
|
|
|
|
|
|
12 |
|
13 |
def combine_code(html, css, js):
|
14 |
+
return f"""<!DOCTYPE html><html><head><style>{css}</style></head><body>{html}<script>{js}</script></body></html>"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def export_zip(html, css, js):
|
17 |
mem = io.BytesIO()
|
|
|
22 |
mem.seek(0)
|
23 |
return (mem, "website.zip")
|
24 |
|
25 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
with gr.Row():
|
27 |
+
# Left: AI Assistant
|
28 |
+
with gr.Column(scale=1, min_width=250):
|
29 |
+
gr.Markdown("### 🤖 Assistant")
|
30 |
+
ai_chat_log = gr.Textbox(label="AI Output", lines=18, interactive=False)
|
31 |
+
ai_input = gr.Textbox(label="Write what you want ✨")
|
32 |
+
ai_button = gr.Button("Generate")
|
33 |
+
|
34 |
+
# Right: Code Editor and Tools
|
35 |
+
with gr.Column(scale=3):
|
36 |
+
with gr.Row(equal_height=True):
|
37 |
+
with gr.Row():
|
38 |
+
html_tab = gr.Button("index.html")
|
39 |
+
css_tab = gr.Button("styles.css")
|
40 |
+
js_tab = gr.Button("script.js")
|
41 |
+
plus_tab = gr.Button("+")
|
42 |
+
with gr.Row():
|
43 |
+
preview_button = gr.Button("▶️ Run")
|
44 |
+
download_button = gr.Button("📦 Download")
|
45 |
+
zip_file = gr.File(interactive=False)
|
46 |
|
47 |
with gr.Tabs():
|
48 |
+
with gr.Tab("HTML"):
|
49 |
+
html_editor = gr.Code(language="html", label=None)
|
50 |
+
with gr.Tab("CSS"):
|
51 |
+
css_editor = gr.Code(language="css", label=None)
|
52 |
+
with gr.Tab("JS"):
|
53 |
+
js_editor = gr.Code(language="javascript", label=None)
|
|
|
|
|
54 |
with gr.Tab("Preview"):
|
55 |
live_preview = gr.HTML()
|
56 |
|
57 |
+
# Logic hooks
|
58 |
+
ai_button.click(generate_code, [ai_input, html_editor, css_editor, js_editor], [html_editor, css_editor, js_editor])
|
59 |
+
preview_button.click(combine_code, [html_editor, css_editor, js_editor], live_preview)
|
60 |
+
download_button.click(export_zip, [html_editor, css_editor, js_editor], zip_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
demo.launch()
|