|
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
from peft import PeftModel |
|
|
import torch |
|
|
import gradio as gr |
|
|
|
|
|
BASE_MODEL = "meta-llama/Llama-2-7b-hf" |
|
|
ADAPTER = "thangduong0509/vinallama-peft-7b-math-solver" |
|
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True) |
|
|
base_model = AutoModelForCausalLM.from_pretrained( |
|
|
BASE_MODEL, |
|
|
device_map="auto", |
|
|
torch_dtype=torch.float16, |
|
|
trust_remote_code=True |
|
|
) |
|
|
|
|
|
|
|
|
model = PeftModel.from_pretrained(base_model, ADAPTER) |
|
|
model.eval() |
|
|
|
|
|
def solve_math(prompt): |
|
|
inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
|
|
with torch.no_grad(): |
|
|
outputs = model.generate(**inputs, max_new_tokens=128, do_sample=True, temperature=0.7) |
|
|
return tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
gr.Interface( |
|
|
fn=solve_math, |
|
|
inputs=gr.Textbox(lines=3, placeholder="Nhập đề Toán tiểu học..."), |
|
|
outputs="text", |
|
|
title="🧮 ViLLama Math Solver", |
|
|
description="Giải toán tiểu học bằng mô hình LLaMA-2 7B fine-tune với PEFT" |
|
|
).launch() |
|
|
|