Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,27 +1,51 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
3 |
|
4 |
-
|
|
|
|
|
5 |
model = AutoModelForCausalLM.from_pretrained(
|
6 |
-
|
7 |
-
trust_remote_code=True
|
8 |
)
|
9 |
|
|
|
10 |
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
result = generator(formatted_prompt, max_new_tokens=1024, do_sample=True, temperature=0.7)
|
16 |
-
code = result[0]['generated_text'].split("<|assistant|>")[-1].strip()
|
17 |
-
return code
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
|
5 |
+
MODEL_ID = "deepseek-ai/deepseek-coder-1.3b-instruct"
|
6 |
+
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
8 |
model = AutoModelForCausalLM.from_pretrained(
|
9 |
+
MODEL_ID,
|
10 |
+
trust_remote_code=True # keep it, but NO device_map to avoid needing accelerate
|
11 |
)
|
12 |
|
13 |
+
PROMPT_TEMPLATE = "<|user|>\n{prompt}\n<|assistant|>\n"
|
14 |
|
15 |
+
def generate_website_code(user_prompt, max_tokens=512, temperature=0.7, top_p=0.95):
|
16 |
+
prompt = PROMPT_TEMPLATE.format(prompt=user_prompt)
|
17 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
18 |
+
with torch.no_grad():
|
19 |
+
output_ids = model.generate(
|
20 |
+
**inputs,
|
21 |
+
max_new_tokens=max_tokens,
|
22 |
+
do_sample=True,
|
23 |
+
temperature=temperature,
|
24 |
+
top_p=top_p,
|
25 |
+
pad_token_id=tokenizer.eos_token_id
|
26 |
+
)
|
27 |
+
text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
28 |
+
code = text.split("<|assistant|>")[-1].strip()
|
29 |
+
return code, code # (code box, html preview)
|
30 |
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
gr.Markdown("# Web X — Turn your website ideas into code with AI.")
|
|
|
|
|
|
|
33 |
|
34 |
+
with gr.Row():
|
35 |
+
prompt = gr.Textbox(
|
36 |
+
label="Describe your website idea",
|
37 |
+
lines=3,
|
38 |
+
placeholder="e.g. Portfolio with navbar, about, projects grid and contact form"
|
39 |
+
)
|
40 |
+
generate_btn = gr.Button("Generate")
|
41 |
+
|
42 |
+
code_out = gr.Code(label="Generated HTML/CSS/JS")
|
43 |
+
preview = gr.HTML(label="Live Preview")
|
44 |
+
|
45 |
+
generate_btn.click(
|
46 |
+
fn=generate_website_code,
|
47 |
+
inputs=prompt,
|
48 |
+
outputs=[code_out, preview]
|
49 |
+
)
|
50 |
|
51 |
demo.launch()
|