File size: 1,340 Bytes
7efb4bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()