LLMPromptGenAI / app.py
LLMPrompGenAI's picture
Create app.py
00679e9 verified
import torch
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer
peft_model_id = "LLMPrompGenAI/LLMPromptGen-AI"
model = AutoModelForCausalLM.from_pretrained(peft_model_id, return_dict=True, device_map='auto')
# tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
mixtral_tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-v0.1")
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 = model.generate(
**inputs,
max_new_tokens=150,
generation_kwargs={"repetition_penalty" : 1.7}
)
print(mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True))
return 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()