JuliaUpton commited on
Commit
bbf6535
1 Parent(s): 2fb3086

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -1,21 +1,29 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
 
4
  # Load the tokenizer and model
5
  model_name = "JuliaUpton/Math_AI"
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
 
 
 
 
8
 
9
  # Define the inference function
10
- def answer_question(question):
11
- input_ids = tokenizer.encode(question, return_tensors="pt")
12
- output_ids = model.generate(input_ids, max_length=100)
13
- answer = tokenizer.decode(output_ids[0], skip_special_tokens=True)
14
- return answer
 
 
 
 
15
 
16
  # Create a Gradio interface
17
  inputs = gr.inputs.Textbox(label="Enter your math question")
18
- outputs = gr.outputs.Textbox(label="Answer")
19
 
20
  examples = [
21
  ["What is the square root of 64?"],
@@ -25,11 +33,11 @@ examples = [
25
 
26
  # Launch the interface
27
  iface = gr.Interface(
28
- fn=answer_question,
29
  inputs=inputs,
30
  outputs=outputs,
31
- title="Math Question Answering",
32
- description="Enter a math question and get the answer with an explanation.",
33
  examples=examples
34
  )
35
 
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer
3
 
4
  # Load the tokenizer and model
5
  model_name = "JuliaUpton/Math_AI"
6
+ mixtral_tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ merged_model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
+
9
+ # Define the input formatting function
10
+ def input_from_text(instruction):
11
+ 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]"
12
 
13
  # Define the inference function
14
+ def make_inference(instruction):
15
+ inputs = mixtral_tokenizer(input_from_text(instruction), return_tensors="pt")
16
+ outputs = merged_model.generate(
17
+ **inputs,
18
+ max_new_tokens=150,
19
+ generation_kwargs={"repetition_penalty": 1.7}
20
+ )
21
+ result = mixtral_tokenizer.decode(outputs[0], skip_special_tokens=True).split("[/INST]")[1]
22
+ return result
23
 
24
  # Create a Gradio interface
25
  inputs = gr.inputs.Textbox(label="Enter your math question")
26
+ outputs = gr.outputs.Textbox(label="Response")
27
 
28
  examples = [
29
  ["What is the square root of 64?"],
 
33
 
34
  # Launch the interface
35
  iface = gr.Interface(
36
+ fn=make_inference,
37
  inputs=inputs,
38
  outputs=outputs,
39
+ title="Math Inquiry Answering",
40
+ description="Enter a math question and get a response with an aexplanation.",
41
  examples=examples
42
  )
43