File size: 1,218 Bytes
b36bc94 0f75b4b 48b73c9 0f75b4b b36bc94 0f75b4b b36bc94 0f75b4b b36bc94 | 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 34 35 36 37 38 39 40 41 42 43 44 | import gradio as gr
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
pipeline,
logging,
)
from peft import PeftModel
# Reload model in FP16 and merge it with LoRA weights
model = AutoModelForCausalLM.from_pretrained(
'TinyLlama/TinyLlama-1.1B-Chat-v1.0'
)
model = PeftModel.from_pretrained(model, 'jezv/TinyLlama-1.1b-cmd')
model = model.merge_and_unload()
# Reload tokenizer to save it
tokenizer = AutoTokenizer.from_pretrained('TinyLlama/TinyLlama-1.1B-Chat-v1.0', trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = "right"
def greet(prompt):
# Try question-answering
pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_new_tokens=20)
messages = [
{
"role": "system",
"content": "You are a chatbot assistant who helps with writing console commands.",
},
{"role": "user", "content": prompt},
]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
result = pipe(text)
return result[0]['generated_text']
demo = gr.Interface(
fn=greet,
inputs=["text"],
outputs=["text"],
)
demo.launch() |