Spaces:
Sleeping
Sleeping
jpatel commited on
Commit ·
ec883c7
1
Parent(s): 32ec3fe
adding html code gen
Browse files- app.py +51 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
# Load model
|
| 7 |
+
base_model = "unsloth/Llama-3.2-1B-Instruct"
|
| 8 |
+
adapter = "jinesh90/llama-3.2-ft-html-generator"
|
| 9 |
+
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
base_model, dtype=torch.float32, device_map="cpu"
|
| 13 |
+
)
|
| 14 |
+
model = PeftModel.from_pretrained(model, adapter)
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
def generate(instruction, input_text=""):
|
| 18 |
+
prompt = f"""Generate valid HTML code based on the following instructions. Return only the HTML code.
|
| 19 |
+
|
| 20 |
+
### Instruction
|
| 21 |
+
{instruction}
|
| 22 |
+
|
| 23 |
+
### Input
|
| 24 |
+
{input_text if input_text else "(none)"}"""
|
| 25 |
+
|
| 26 |
+
messages = [{"role": "user", "content": prompt}]
|
| 27 |
+
text = tokenizer.apply_chat_template(
|
| 28 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 29 |
+
)
|
| 30 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=2048)
|
| 31 |
+
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model.generate(
|
| 34 |
+
**inputs, max_new_tokens=512,
|
| 35 |
+
do_sample=False, temperature=0
|
| 36 |
+
)
|
| 37 |
+
input_len = inputs["input_ids"].shape[1]
|
| 38 |
+
return tokenizer.decode(outputs[0, input_len:], skip_special_tokens=True)
|
| 39 |
+
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=generate,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.Textbox(label="Instruction", placeholder="Create an HTML login form..."),
|
| 44 |
+
gr.Textbox(label="Input code (optional)", placeholder="Paste existing HTML..."),
|
| 45 |
+
],
|
| 46 |
+
outputs=gr.Code(label="Generated HTML", language="html"),
|
| 47 |
+
title="💻 HTML Code Generator",
|
| 48 |
+
description="Fine-tuned Llama 3.2 for HTML generation",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
peft
|
| 4 |
+
accelerate
|
| 5 |
+
gradio
|