Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
| 6 |
+
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_id,
|
| 11 |
+
trust_remote_code=True,
|
| 12 |
+
device_map="auto",
|
| 13 |
+
torch_dtype=torch.float32 # CPU compatible
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# Prompt formatter
|
| 17 |
+
def generate_html_code(prompt):
|
| 18 |
+
instruction = f"Write only valid HTML code for this: {prompt}\n"
|
| 19 |
+
inputs = tokenizer(instruction, return_tensors="pt")
|
| 20 |
+
outputs = model.generate(
|
| 21 |
+
**inputs,
|
| 22 |
+
max_new_tokens=300,
|
| 23 |
+
temperature=0.7,
|
| 24 |
+
do_sample=True,
|
| 25 |
+
top_p=0.95
|
| 26 |
+
)
|
| 27 |
+
html_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 28 |
+
|
| 29 |
+
# Optional: clean up output to extract HTML only
|
| 30 |
+
start = html_code.find("<")
|
| 31 |
+
if start != -1:
|
| 32 |
+
html_code = html_code[start:]
|
| 33 |
+
return html_code, html_code # code + live preview
|
| 34 |
+
|
| 35 |
+
# Gradio UI
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=generate_html_code,
|
| 38 |
+
inputs=gr.Textbox(label="Describe your HTML component", placeholder="e.g. A red button that says Click Me", lines=3),
|
| 39 |
+
outputs=[
|
| 40 |
+
gr.Code(label="Generated HTML Code", language="html"),
|
| 41 |
+
gr.HTML(label="Live Preview")
|
| 42 |
+
],
|
| 43 |
+
title="HTML Generator with Qwen2.5",
|
| 44 |
+
description="Generate and preview HTML in real-time using the Qwen2.5 32B Coder model (CPU mode, may be slow!)"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
demo.launch()
|