File size: 1,670 Bytes
c4a5305
ad99d53
 
c4a5305
ad99d53
 
 
5e5f34d
ad99d53
 
5e5f34d
 
ad99d53
c4a5305
ad99d53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4a5305
ad99d53
 
c4a5305
ad99d53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4a5305
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

MODEL_ID = "deepseek-ai/deepseek-coder-1.3b-instruct"

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    trust_remote_code=True  # keep it, but NO device_map to avoid needing accelerate
)

PROMPT_TEMPLATE = "<|user|>\n{prompt}\n<|assistant|>\n"

def generate_website_code(user_prompt, max_tokens=512, temperature=0.7, top_p=0.95):
    prompt = PROMPT_TEMPLATE.format(prompt=user_prompt)
    inputs = tokenizer(prompt, return_tensors="pt")
    with torch.no_grad():
        output_ids = model.generate(
            **inputs,
            max_new_tokens=max_tokens,
            do_sample=True,
            temperature=temperature,
            top_p=top_p,
            pad_token_id=tokenizer.eos_token_id
        )
    text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
    code = text.split("<|assistant|>")[-1].strip()
    return code, code  # (code box, html preview)

with gr.Blocks() as demo:
    gr.Markdown("# Web X — Turn your website ideas into code with AI.")

    with gr.Row():
        prompt = gr.Textbox(
            label="Describe your website idea",
            lines=3,
            placeholder="e.g. Portfolio with navbar, about, projects grid and contact form"
        )
        generate_btn = gr.Button("Generate")

    code_out = gr.Code(label="Generated HTML/CSS/JS")
    preview = gr.HTML(label="Live Preview")

    generate_btn.click(
        fn=generate_website_code,
        inputs=prompt,
        outputs=[code_out, preview]
    )

demo.launch()