File size: 985 Bytes
3c5bad6 ac7dcc6 3c5bad6 484fdda 3c5bad6 | 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 | import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
from peft import PeftModel
import os
from huggingface_hub import login
login(token=os.environ.get("HF_TOKEN"))
BASE = "meta-llama/Llama-2-7b-hf"
LORA = "automorphic/LORA_20231221_040125_high_school_mathematics"
# Load tokenizer + model
tokenizer = AutoTokenizer.from_pretrained(BASE)
model = AutoModelForCausalLM.from_pretrained(BASE, device_map="auto")
model = PeftModel.from_pretrained(model, LORA)
# Define function for inference
def solve_math(prompt):
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=100)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# Create simple Gradio UI
iface = gr.Interface(
fn=solve_math,
inputs="text",
outputs="text",
title="High School Math Solver (LoRA)",
description="Type a math problem and the model will solve it step-by-step!"
)
iface.launch()
|