File size: 919 Bytes
e5e03cf c519d4e e5e03cf c519d4e 731e545 c519d4e 731e545 c519d4e a2bfd19 c519d4e |
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 |
import gradio as gr
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Load the model and config when the script starts
config = PeftConfig.from_pretrained("PhantHive/bigbrain")
model = AutoModelForCausalLM.from_pretrained("NousResearch/Llama-2-7b-chat-hf")
model = PeftModel.from_pretrained(model, "PhantHive/bigbrain")
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained("NousResearch/Llama-2-7b-chat-hf", add_eos_token=True)
def greet(text):
batch = tokenizer(f"'{text}' ->: ", return_tensors='pt')
# Use torch.no_grad to disable gradient calculation
with torch.no_grad():
output_tokens = model.generate(**batch, do_sample=True, max_new_tokens=50, temperature=0.9)
return tokenizer.decode(output_tokens[0], skip_special_tokens=True)
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch() |