JuliaUpton commited on
Commit
8d63e99
1 Parent(s): eec1d0a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from peft import PeftModel, PeftConfig
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ peft_model_id = "JuliaUpton/Math_AI"
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=False)
8
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
9
+ "
10
+ # Load the Lora model
11
+ merged_model = PeftModel.from_pretrained(model, peft_model_id)
12
+
13
+ def input_from_text(instruction):
14
+ return f"<s>[INST]Below is a math inquiry, please answer it as a math expert showing your thought process.\n\n### Inquiry:\n{instruction}\n\n### Response:[/INST]"
15
+
16
+ def make_inference(instruction):
17
+ inputs = mixtral_tokenizer(input_from_text(instruction), 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
+ result = mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True).split("[/INST]")[1]
26
+ return result
27
+
28
+ if __name__ == "__main__":
29
+ # make a gradio interface
30
+ import gradio as gr
31
+
32
+ gr.Interface(
33
+ make_inference,
34
+ [
35
+ gr.Textbox(lines=5, label="Instruction"),
36
+ ],
37
+ gr.Textbox(label="Answer"),
38
+ title="Math-AI",
39
+ description="Math-AI is a generative model that answers math questions",
40
+ ).launch()