Bsbell21 commited on
Commit
7f93fc1
Β·
verified Β·
1 Parent(s): af028a9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ peft_model_id = f"Bsbell21/llm_instruction_generator"
6
+ config = PeftConfig.from_pretrained(peft_model_id)
7
+ model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=True, device_map='auto')
8
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
9
+
10
+ # Load the Lora model
11
+ model = PeftModel.from_pretrained(model, peft_model_id)
12
+
13
+ def input_from_text(text):
14
+ 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]"
15
+
16
+ def get_instruction(text):
17
+ inputs = mixtral_tokenizer(input_from_text(text), return_tensors="pt")
18
+
19
+ outputs = merged_model.generate(
20
+ **inputs,
21
+ max_new_tokens=150,
22
+ generation_kwargs={"repetition_penalty" : 1.7}
23
+ )
24
+ # print(mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True))
25
+ print(mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True).split("[/INST]")[1])
26
+
27
+ if __name__ == "__main__":
28
+ # make a gradio interface
29
+ import gradio as gr
30
+
31
+ gr.Interface(
32
+ get_instruction,
33
+ [
34
+ gr.Textbox(lines=10, label="LLM Response"),
35
+ ],
36
+ gr.Textbox(label="LLM Predicted Prompt"),
37
+ title="LLM-Prompt-Predictor",
38
+ description="Prompt Predictor Based on LLM Response",
39
+ ).launch()