Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,26 +3,16 @@ from transformers import pipeline
|
|
3 |
import zipfile
|
4 |
import io
|
5 |
|
6 |
-
#
|
7 |
assistant = pipeline("text-generation", model="Salesforce/codegen-350M-multi", max_new_tokens=512)
|
8 |
|
9 |
-
def generate_code(prompt):
|
10 |
-
|
11 |
-
|
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 |
-
|
|
|
|
|
26 |
|
27 |
def combine_code(html, css, js):
|
28 |
return f"""
|
@@ -39,46 +29,65 @@ def combine_code(html, css, js):
|
|
39 |
"""
|
40 |
|
41 |
def export_zip(html, css, js):
|
42 |
-
|
43 |
-
with zipfile.ZipFile(
|
44 |
-
zf.writestr("index.html", f"<!DOCTYPE html><html><head><link rel='stylesheet' href='style.css'></head><body
|
45 |
zf.writestr("style.css", css)
|
46 |
zf.writestr("script.js", js)
|
47 |
-
|
48 |
-
return (
|
49 |
|
50 |
with gr.Blocks(css="""
|
51 |
-
body { font-family: '
|
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 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
|
64 |
with gr.Column(scale=2):
|
65 |
-
gr.Markdown("## 💻 Code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
with gr.Tabs():
|
67 |
-
with gr.Tab("
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
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()
|
|
|
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 |
+
base_prompt = f"# HTML, CSS, and JavaScript website task:\n# {prompt}\n"
|
11 |
+
result = assistant(base_prompt)[0]["generated_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"""
|
|
|
29 |
"""
|
30 |
|
31 |
def export_zip(html, css, js):
|
32 |
+
mem = io.BytesIO()
|
33 |
+
with zipfile.ZipFile(mem, 'w') as zf:
|
34 |
+
zf.writestr("index.html", f"<!DOCTYPE html><html><head><link rel='stylesheet' href='style.css'></head><body>{html}<script src='script.js'></script></body></html>")
|
35 |
zf.writestr("style.css", css)
|
36 |
zf.writestr("script.js", js)
|
37 |
+
mem.seek(0)
|
38 |
+
return (mem, "website.zip")
|
39 |
|
40 |
with gr.Blocks(css="""
|
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 |
with gr.Column(scale=1):
|
50 |
gr.Markdown("## 🤖 AI Assistant")
|
51 |
+
user_prompt = gr.Textbox(label="Ask AI to add or edit something:", placeholder="e.g., Add a sticky navbar or change background color")
|
52 |
+
ai_status = gr.Textbox(label="Status")
|
53 |
+
gr.Markdown("### AI Response Log")
|
54 |
+
ai_log = gr.Textbox(lines=12, interactive=False, label="Raw AI Output")
|
55 |
|
56 |
with gr.Column(scale=2):
|
57 |
+
gr.Markdown("## 💻 Code Workspace")
|
58 |
+
|
59 |
+
# Menu bar
|
60 |
+
with gr.Row():
|
61 |
+
generate_btn = gr.Button("✨ Ask AI")
|
62 |
+
preview_btn = gr.Button("▶️ Preview Website")
|
63 |
+
export_btn = gr.Button("📦 Export as ZIP")
|
64 |
+
zip_out = gr.File(interactive=False)
|
65 |
+
|
66 |
with gr.Tabs():
|
67 |
+
with gr.Tab("Editor"):
|
68 |
+
with gr.Tabs():
|
69 |
+
with gr.Tab("HTML"):
|
70 |
+
html_editor = gr.Code(language="html", label="HTML")
|
71 |
+
with gr.Tab("CSS"):
|
72 |
+
css_editor = gr.Code(language="css", label="CSS")
|
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 |
+
# Hook up buttons
|
79 |
+
generate_btn.click(
|
80 |
+
generate_code,
|
81 |
+
[user_prompt, html_editor, css_editor, js_editor],
|
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()
|