Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os, zipfile, tempfile
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
4 |
-
import json
|
5 |
|
6 |
# ---------------------------
|
7 |
-
# 1️⃣ Load
|
8 |
# ---------------------------
|
9 |
-
model_name = "
|
10 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
11 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
12 |
-
generator = pipeline(
|
13 |
|
14 |
# ---------------------------
|
15 |
# 2️⃣ Website Generation Function
|
16 |
# ---------------------------
|
17 |
def generate_website(prompt):
|
18 |
-
#
|
19 |
instruction = f"""
|
20 |
-
Generate a full website project
|
21 |
-
|
22 |
-
Include folders: css/, js/, assets/, db/ if needed.
|
|
|
23 |
"""
|
24 |
-
gen = generator(instruction, max_new_tokens=2048)[0]['generated_text']
|
25 |
|
26 |
-
#
|
|
|
|
|
|
|
27 |
try:
|
28 |
files_dict = json.loads(gen)
|
29 |
except:
|
30 |
-
|
|
|
31 |
|
32 |
# Create temp folder and save files
|
33 |
temp_dir = tempfile.mkdtemp()
|
@@ -57,7 +60,7 @@ iface = gr.Interface(
|
|
57 |
inputs=gr.Textbox(lines=4, placeholder="Describe your website: PHP/HTML/JS, admin panel, MySQL..."),
|
58 |
outputs=gr.File(label="Download Generated Website ZIP"),
|
59 |
title="Local AI Multi-file Website Generator",
|
60 |
-
description="Single Python file app. Generates multi-file website locally
|
61 |
)
|
62 |
|
63 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os, zipfile, tempfile, json
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
4 |
|
5 |
# ---------------------------
|
6 |
+
# 1️⃣ Load Public Code Model
|
7 |
# ---------------------------
|
8 |
+
model_name = "Salesforce/codegen-350M-multi" # public model, no token required
|
9 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
11 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
12 |
|
13 |
# ---------------------------
|
14 |
# 2️⃣ Website Generation Function
|
15 |
# ---------------------------
|
16 |
def generate_website(prompt):
|
17 |
+
# Instruction for multi-file code in JSON format
|
18 |
instruction = f"""
|
19 |
+
Generate a full website project based on the user prompt.
|
20 |
+
Return the result as JSON where key = file path and value = file content.
|
21 |
+
Include folders: css/, js/, assets/, db/ if needed.
|
22 |
+
User prompt: {prompt}
|
23 |
"""
|
|
|
24 |
|
25 |
+
# Generate code text
|
26 |
+
gen = generator(instruction, max_new_tokens=1024)[0]['generated_text']
|
27 |
+
|
28 |
+
# Try parsing JSON from generated text
|
29 |
try:
|
30 |
files_dict = json.loads(gen)
|
31 |
except:
|
32 |
+
# fallback: single index.html if parsing fails
|
33 |
+
files_dict = {"index.html": "<!-- Failed to generate valid code -->"}
|
34 |
|
35 |
# Create temp folder and save files
|
36 |
temp_dir = tempfile.mkdtemp()
|
|
|
60 |
inputs=gr.Textbox(lines=4, placeholder="Describe your website: PHP/HTML/JS, admin panel, MySQL..."),
|
61 |
outputs=gr.File(label="Download Generated Website ZIP"),
|
62 |
title="Local AI Multi-file Website Generator",
|
63 |
+
description="Single Python file app. Generates multi-file website locally using public HF model. Download as ZIP."
|
64 |
)
|
65 |
|
66 |
iface.launch()
|