Bsbell21's picture
Update app.py
072fc04 verified
import torch
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
peft_model_id = f"Bsbell21/llm_instruction_generator"
model = AutoModelForCausalLM.from_pretrained(peft_model_id, return_dict=True, load_in_8bit=True, load_in_8bit_fp32_cpu_offload=True, device_map='auto')
tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
# Load the Lora model
# model = PeftModel.from_pretrained(model, peft_model_id)
def input_from_text(text):
return "<s>[INST]Use the provided input to create an instruction that could have been used to generate the response with an LLM.\n" + text + "[/INST]"
def get_instruction(text):
inputs = mixtral_tokenizer(input_from_text(text), return_tensors="pt")
outputs = merged_model.generate(
**inputs,
max_new_tokens=150,
generation_kwargs={"repetition_penalty" : 1.7}
)
# print(mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True))
print(mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True).split("[/INST]")[1])
if __name__ == "__main__":
# make a gradio interface
import gradio as gr
gr.Interface(
get_instruction,
[
gr.Textbox(lines=10, label="LLM Response"),
],
gr.Textbox(label="LLM Predicted Prompt"),
title="LLM-Prompt-Predictor",
description="Prompt Predictor Based on LLM Response",
).launch()