Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
|
| 4 |
+
# Load a small code model
|
| 5 |
+
model_name = "Salesforce/codegen-350M-multi"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 10 |
+
|
| 11 |
+
def generate_website(prompt):
|
| 12 |
+
instruction = f"""
|
| 13 |
+
You are an expert web developer.
|
| 14 |
+
Generate ONLY three files for the website:
|
| 15 |
+
===index.html===
|
| 16 |
+
(use semantic HTML, basic structure)
|
| 17 |
+
===style.css===
|
| 18 |
+
(responsive, clean CSS)
|
| 19 |
+
===script.js===
|
| 20 |
+
(simple JS interactions)
|
| 21 |
+
Do NOT include explanations.
|
| 22 |
+
User request: {prompt}
|
| 23 |
+
"""
|
| 24 |
+
result = generator(instruction, max_length=500)[0]['generated_text']
|
| 25 |
+
|
| 26 |
+
# Split into files
|
| 27 |
+
files = {}
|
| 28 |
+
if "===index.html===" in result:
|
| 29 |
+
parts = result.split("===")
|
| 30 |
+
for i in range(1, len(parts), 2):
|
| 31 |
+
name = parts[i].strip()
|
| 32 |
+
code = parts[i+1].strip()
|
| 33 |
+
files[name] = code
|
| 34 |
+
# Fallback if model output is unexpected
|
| 35 |
+
return (
|
| 36 |
+
files.get("index.html", ""),
|
| 37 |
+
files.get("style.css", ""),
|
| 38 |
+
files.get("script.js", "")
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Gradio UI
|
| 42 |
+
iface = gr.Interface(
|
| 43 |
+
fn=generate_website,
|
| 44 |
+
inputs=gr.Textbox(lines=3, placeholder="Describe your website..."),
|
| 45 |
+
outputs=[
|
| 46 |
+
gr.Textbox(label="index.html"),
|
| 47 |
+
gr.Textbox(label="style.css"),
|
| 48 |
+
gr.Textbox(label="script.js")
|
| 49 |
+
],
|
| 50 |
+
title="AI Website Generator",
|
| 51 |
+
description="Generate HTML/CSS/JS websites directly online. No files are saved locally."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
iface.launch()
|