|
import gradio as gr |
|
from peft import PeftModel, PeftConfig |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
import torch |
|
|
|
|
|
config = PeftConfig.from_pretrained("% MODEL_NAME %") |
|
model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf") |
|
model = PeftModel.from_pretrained(model, "% MODEL_NAME %") |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf", add_eos_token=True) |
|
|
|
|
|
def greet(text): |
|
batch = tokenizer(f"'{text}' ->: ", return_tensors='pt') |
|
|
|
|
|
with torch.no_grad(): |
|
output_tokens = model.generate(**batch, do_sample=True, max_new_tokens=50) |
|
|
|
return tokenizer.decode(output_tokens[0], skip_special_tokens=True) |
|
|
|
|
|
iface = gr.Interface(fn=greet, inputs="text", outputs="text") |
|
iface.launch() |