Spaces:
Running
Running
added for
Browse files- app.py +99 -69
- requirements.txt +2 -1
app.py
CHANGED
@@ -10,82 +10,112 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig, set_se
|
|
10 |
|
11 |
import torch
|
12 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
model.generation_config = GenerationConfig.from_pretrained(model_name)
|
18 |
-
model.generation_config.pad_token_id = model.generation_config.eos_token_id
|
19 |
-
|
20 |
-
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
problem=problem+'\nPlease reason step by step, and put your final answer within \\boxed{}.'
|
26 |
-
messages = [
|
27 |
-
{"role": "user", "content": problem}
|
28 |
-
]
|
29 |
-
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
|
30 |
-
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
|
31 |
|
32 |
-
|
33 |
-
#
|
34 |
-
return result
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
demo = gr.Interface(
|
88 |
-
fn=
|
89 |
inputs=[gr.Textbox(label="Question")],
|
90 |
outputs=gr.Textbox(label="Answer"),
|
91 |
title="Question and Answer Interface",
|
|
|
10 |
|
11 |
import torch
|
12 |
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
|
13 |
+
from transformers import BitsAndBytesConfig
|
14 |
+
from tqdm import tqdm
|
15 |
+
import os
|
16 |
+
quantization_config = BitsAndBytesConfig(
|
17 |
+
load_in_4bit = True,
|
18 |
+
bnb_4bit_quant_type="nf4",
|
19 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
20 |
+
bnb_4bit_use_double_quant=True,
|
21 |
+
)
|
22 |
|
23 |
+
USE_PAST_KEY = True
|
24 |
+
import gc
|
25 |
+
torch.backends.cuda.enable_mem_efficient_sdp(False)
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
from transformers import (
|
28 |
+
AutoModelForCausalLM,
|
29 |
+
AutoTokenizer,
|
30 |
+
AutoConfig,
|
31 |
+
StoppingCriteria,
|
32 |
+
set_seed
|
33 |
+
)
|
34 |
|
35 |
+
n_repetitions = 1
|
36 |
+
TOTAL_TOKENS = 2048
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
MODEL_PATH = "/kaggle/input/deepseek-math"
|
39 |
+
#"/kaggle/input/gemma/transformers/7b-it/1"
|
|
|
40 |
|
41 |
+
# DEEP = True
|
42 |
+
import torch
|
43 |
+
from transformers import BitsAndBytesConfig
|
44 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
|
45 |
+
|
46 |
+
config = AutoConfig.from_pretrained(MODEL_PATH)
|
47 |
+
config.gradient_checkpointing = True
|
48 |
+
|
49 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
50 |
+
quantization_config = BitsAndBytesConfig(
|
51 |
+
load_in_4bit = True,
|
52 |
+
bnb_4bit_quant_type="nf4",
|
53 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
54 |
+
bnb_4bit_use_double_quant=True,
|
55 |
+
)
|
56 |
+
model = AutoModelForCausalLM.from_pretrained(
|
57 |
+
MODEL_PATH,
|
58 |
+
device_map="sequential",
|
59 |
+
torch_dtype="auto",
|
60 |
+
trust_remote_code=True,
|
61 |
+
quantization_config=quantization_config,
|
62 |
+
config=config
|
63 |
+
)
|
64 |
+
pipeline = transformers.pipeline(
|
65 |
+
"text-generation",
|
66 |
+
model=model,
|
67 |
+
tokenizer=tokenizer,
|
68 |
+
torch_dtype='auto',
|
69 |
+
device_map=device_map,
|
70 |
+
)
|
71 |
+
from transformers import StoppingCriteriaList
|
72 |
+
|
73 |
+
class StoppingCriteriaSub(StoppingCriteria):
|
74 |
+
def __init__(self, stops = [], encounters=1):
|
75 |
+
super().__init__()
|
76 |
+
self.stops = [stop.to("cuda") for stop in stops]
|
77 |
+
|
78 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor):
|
79 |
+
for stop in self.stops:
|
80 |
+
last_token = input_ids[0][-len(stop):]
|
81 |
+
if torch.all(torch.eq(stop,last_token)):
|
82 |
+
return True
|
83 |
+
return False
|
84 |
+
|
85 |
+
|
86 |
+
stop_words = ["```output", "```python", "```\nOutput" , ")\n```" , "``````output"] #,
|
87 |
+
stop_words_ids = [tokenizer(stop_word, return_tensors='pt', add_special_tokens=False)['input_ids'].squeeze() for stop_word in stop_words]
|
88 |
+
stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=stop_words_ids)])
|
89 |
+
|
90 |
+
code = """Below is a math problem you are to solve (positive numerical answer):
|
91 |
+
\"{}\"
|
92 |
+
To accomplish this, first determine a sympy-based approach for solving the problem by listing each step to take and what functions need to be called in each step. Be clear so even an idiot can follow your instructions, and remember, your final answer should be positive integer, not an algebraic expression!
|
93 |
+
Write the entire script covering all the steps (use comments and document it well) and print the result. After solving the problem, output the final numerical answer within \\boxed{}.
|
94 |
+
|
95 |
+
Approach:"""
|
96 |
+
|
97 |
+
|
98 |
+
cot = """Below is a math problem you are to solve (positive numerical answer!):
|
99 |
+
\"{}\"
|
100 |
+
Analyze this problem and think step by step to come to a solution with programs. After solving the problem, output the final numerical answer within \\boxed{}.\n\n"""
|
101 |
+
|
102 |
+
promplt_options = [code,cot]
|
103 |
+
|
104 |
+
import re
|
105 |
+
from collections import defaultdict
|
106 |
+
from collections import Counter
|
107 |
+
|
108 |
+
from numpy.random import choice
|
109 |
+
import numpy as np
|
110 |
+
|
111 |
+
tool_instruction = '\n\nPlease integrate natural language reasoning with programs to solve the above problem, and put your final numerical answer within \\boxed{}.\nNote that the intermediary calculations may be real numbers, but the final numercal answer would always be an integer.'
|
112 |
+
|
113 |
+
|
114 |
+
#tool_instruction = " The answer should be given as a non-negative modulo 1000."
|
115 |
+
#tool_instruction += '\nPlease integrate natural language reasoning with programs to solve the problem above, and put your final answer within \\boxed{}.'
|
116 |
|
117 |
demo = gr.Interface(
|
118 |
+
fn=predict,
|
119 |
inputs=[gr.Textbox(label="Question")],
|
120 |
outputs=gr.Textbox(label="Answer"),
|
121 |
title="Question and Answer Interface",
|
requirements.txt
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
huggingface_hub==0.22.2
|
2 |
transformers==4.40.0
|
3 |
torch==2.3.1
|
4 |
-
accelerate==0.31.0
|
|
|
|
1 |
huggingface_hub==0.22.2
|
2 |
transformers==4.40.0
|
3 |
torch==2.3.1
|
4 |
+
accelerate==0.31.0
|
5 |
+
bitsandbytes==0.43.1
|