|
import gradio as gr |
|
import os, zipfile, tempfile, json |
|
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline |
|
|
|
|
|
|
|
|
|
model_name = "Salesforce/codegen-350M-multi" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
generator = pipeline("text-generation", model=model, tokenizer=tokenizer) |
|
|
|
|
|
|
|
|
|
def generate_website(prompt): |
|
|
|
instruction = f""" |
|
Generate a full website project based on the user prompt. |
|
Return the result as JSON where key = file path and value = file content. |
|
Include folders: css/, js/, assets/, db/ if needed. |
|
User prompt: {prompt} |
|
""" |
|
|
|
|
|
gen = generator(instruction, max_new_tokens=1024)[0]['generated_text'] |
|
|
|
|
|
try: |
|
files_dict = json.loads(gen) |
|
except: |
|
|
|
files_dict = {"index.html": "<!-- Failed to generate valid code -->"} |
|
|
|
|
|
temp_dir = tempfile.mkdtemp() |
|
for fname, content in files_dict.items(): |
|
file_path = os.path.join(temp_dir, fname) |
|
os.makedirs(os.path.dirname(file_path), exist_ok=True) |
|
with open(file_path, "w", encoding="utf-8") as f: |
|
f.write(content) |
|
|
|
|
|
zip_path = os.path.join(temp_dir, "website.zip") |
|
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf: |
|
for root, _, files in os.walk(temp_dir): |
|
for file in files: |
|
if file != os.path.basename(zip_path): |
|
file_path = os.path.join(root, file) |
|
arcname = os.path.relpath(file_path, temp_dir) |
|
zipf.write(file_path, arcname=arcname) |
|
|
|
return zip_path |
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
fn=generate_website, |
|
inputs=gr.Textbox(lines=4, placeholder="Describe your website: PHP/HTML/JS, admin panel, MySQL..."), |
|
outputs=gr.File(label="Download Generated Website ZIP"), |
|
title="Local AI Multi-file Website Generator", |
|
description="Single Python file app. Generates multi-file website locally using public HF model. Download as ZIP." |
|
) |
|
|
|
iface.launch() |