File size: 1,471 Bytes
8d63e99
 
 
 
 
 
 
 
a67e26b
8d63e99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
import torch
from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer

peft_model_id = "JuliaUpton/Math_AI"
config = PeftConfig.from_pretrained(peft_model_id)
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=False)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)

# Load the Lora model
merged_model = PeftModel.from_pretrained(model, peft_model_id)

def input_from_text(instruction):
  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]"

def make_inference(instruction):
  inputs = mixtral_tokenizer(input_from_text(instruction), 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))
  result = mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True).split("[/INST]")[1]
  return result

if __name__ == "__main__":
    # make a gradio interface
    import gradio as gr

    gr.Interface(
        make_inference,
        [
            gr.Textbox(lines=5, label="Instruction"),
        ],
        gr.Textbox(label="Answer"),
        title="Math-AI",
        description="Math-AI is a generative model that answers math questions",
    ).launch()