Tonic commited on
Commit
7f73a1c
·
verified ·
1 Parent(s): 01d13d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +130 -11
app.py CHANGED
@@ -1,38 +1,157 @@
1
  import spaces
 
2
  import gradio as gr
3
  from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
4
  import torch
5
 
6
  title = """# 🙋🏻‍♂️Welcome to🌟Tonic's🔮DeepSeekMath📉
7
- You can build with this endpoint using🔮DeepSeekMath📉. The demo is still a work in progress and we're looking forward to build downstream tasks that showcase outstanding mathematical reasoning. Have any ideas ? join us below !
8
  You can also use 🔮DeepSeekMath📉 by cloning this space. Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/Math?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
9
  Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) Math with [introspector](https://huggingface.co/introspector) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [SciTonic](https://github.com/Tonic-AI/scitonic)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
10
  """
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  model_name = "deepseek-ai/deepseek-math-7b-instruct"
13
  tokenizer = AutoTokenizer.from_pretrained(model_name)
14
  model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
15
  model.generation_config = GenerationConfig.from_pretrained(model_name)
16
  model.generation_config.pad_token_id = model.generation_config.eos_token_id
17
 
 
 
 
 
 
 
 
 
 
 
18
  @spaces.GPU
19
- def solve_math_problem(question, max_tokens):
20
- prompt = f"User: {question}\nPlease reason step by step, and put your final answer within \\boxed{{}}.\nAssistant:"
21
  input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
22
  outputs = model.generate(input_ids, max_length=max_tokens + input_ids.shape[1], pad_token_id=model.generation_config.pad_token_id)
23
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
- return result
 
 
25
 
26
  def main():
27
  with gr.Blocks() as demo:
28
  gr.Markdown(title)
29
- with gr.Row():
30
- question = gr.Code(language='python', value='what is the integral of x^2 from 0 to 2?', label="Enter your math problem")
31
- max_tokens = gr.Slider(minimum=150, maximum=1200, value=250, label="Max Tokens")
32
- submit_button = gr.Button("Solve")
33
- output = gr.Code(label="🔮DeepSeekMath📉", interactive=False)
34
-
35
- submit_button.click(fn=solve_math_problem, inputs=[question, max_tokens], outputs=output)
 
 
 
 
36
 
37
  demo.launch()
38
 
 
1
  import spaces
2
+ import re
3
  import gradio as gr
4
  from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
5
  import torch
6
 
7
  title = """# 🙋🏻‍♂️Welcome to🌟Tonic's🔮DeepSeekMath📉
8
+ You can build with this endpoint using🔮DeepSeekMath📉 available here : [deepseek-ai/deepseek-math-7b-instruct](https://huggingface.co/deepseek-ai/deepseek-math-7b-instruct). We're using 🤖[introspector/unimath](https://huggingface.co/datasets/introspector/unimath) for cool examples, check it out below ! The demo is still a work in progress and we're looking forward to build downstream tasks that showcase outstanding mathematical reasoning. Have any ideas ? join us below !
9
  You can also use 🔮DeepSeekMath📉 by cloning this space. Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/Math?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
10
  Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's 🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) Math with [introspector](https://huggingface.co/introspector) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to🌟 [SciTonic](https://github.com/Tonic-AI/scitonic)🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
11
  """
12
 
13
+ unimath1 = """Goal:
14
+
15
+ X : UU
16
+ Y : UU
17
+ P : UU
18
+ xp : (X → P) → P
19
+ yp : (Y → P) → P
20
+ X0 : X × Y → P
21
+ x : X
22
+ ============================
23
+ (Y → P)
24
+
25
+
26
+ DEBUG:Going to execute:
27
+ PTRDEBUGTAC<coq-core.plugins.ltac::intro@1> $1
28
+ DEBUG LTAC Evaluated term: yp
29
+
30
+ TcDebug (0) > NONE??? LtacNotationCall (no location),/mnt/data1/2024/01/05/UniMath/UniMath/Foundations/PartA.v:234
31
+ """
32
+ # source : unimath/unimath/batch2/data08
33
+
34
+ unimath2 = """Goal:
35
+ R : ring M : module R
36
+ ============================
37
+ (islinear (idfun M))
38
+
39
+
40
+ DEBUG:Going to execute:
41
+ PTRDEBUGTACapply pathsinv0; trivial
42
+ Level 0: Backtrace:
43
+
44
+ Proof is not complete.
45
+ Level 0: Backtrace:
46
+
47
+ Proof is not complete.
48
+
49
+ TcDebug (0) > NONE??? LtacNotationCall (no location),/mnt/data1/2024/01/05/UniMath/UniMath/MoreFoundations/Tactics.veasy:19, Tactics (UniMath.MoreFoundations),/mnt/data1/2024/01/05/UniMath/UniMath/Algebra/Modules/Examples.v:27
50
+
51
+ """
52
+ # source : unimath/unimath/batch2/data_22/BATCH122007
53
+
54
+ unimath3 = """Goal:
55
+ X : UU i : nat b : hProptoType (i < S i) x : Vector X (S i) r : i = i
56
+ ============================
57
+ (pr1 lastelement = pr1 (i,, b))
58
+
59
+
60
+ DEBUG:Going to execute:
61
+ PTRDEBUGTACsimpl
62
+ DEBUG LTAC Evaluated term: isinjstntonat
63
+
64
+ TcDebug (0) > /mnt/data1/2024/01/05/UniMath/UniMath/Combinatorics/FiniteSequences.v:114
65
+ """
66
+ # source : unimath/unimath/batch2/data_12/BATCH112026
67
+
68
+ unimath4 = """Goal:
69
+ X : dcpo CX : continuous_dcpo_struct X x : pr1hSet X y : pr1hSet X
70
+ ============================
71
+ (x ⊑ y
72
+ ≃ (∀ i : approximating_family CX x, approximating_family CX x i ⊑ y))
73
+
74
+
75
+ DEBUG:Going to execute:
76
+ PTRDEBUGTACsimple refine (p _ _ _) ||
77
+ simple refine (p _ _ _ _) ||
78
+ simple refine (p _ _ _ _ _) ||
79
+ simple refine (p _ _ _ _ _ _) ||
80
+ simple refine (p _ _ _ _ _ _ _) ||
81
+ simple refine (p _ _ _ _ _ _ _ _) ||
82
+ simple refine (p _ _ _ _ _ _ _ _ _) ||
83
+ simple refine (p _ _ _ _ _ _ _ _ _ _) ||
84
+ simple refine (p _ _ _ _ _ _ _ _ _ _ _) ||
85
+ simple refine (p _ _ _ _ _ _ _ _ _ _ _ _) ||
86
+ simple refine (p _ _ _ _ _ _ _ _ _ _ _ _ _) ||
87
+ simple refine
88
+ (p _ _ _ _ _ _ _ _ _ _ _ _ _ _) || simple
89
+ refine (p _ _ _ _ _ _ _ _ _ _ _ _ _ _ _)
90
+ Level 0: Backtrace:
91
+
92
+ In environment
93
+ X : dcpo
94
+ CX : continuous_dcpo_struct X
95
+ x, y : X
96
+ The term "weqimplimpl ?f ?g" has type "isaprop ?X → isaprop ?Y → ?X ≃ ?Y"
97
+ while it is expected to have type
98
+ "x ⊑ y ≃ (∀ i : approximating_family CX x, approximating_family CX x i ⊑ y)".
99
+ Level 0: Backtrace:
100
+
101
+ In environment
102
+ X : dcpo
103
+ CX : continuous_dcpo_struct X
104
+ x, y : X
105
+ The term "weqimplimpl ?f ?g" has type "isaprop ?X → isaprop ?Y → ?X ≃ ?Y"
106
+ while it is expected to have type
107
+ "x ⊑ y ≃ (∀ i : approximating_family CX x, approximating_family CX x i ⊑ y)".
108
+
109
+ TcDebug (0) > NONE??? LtacNotationCall (no location),/mnt/data1/2024/01/05/UniMath/UniMath/Foundations/Init.vsimple_rapply:174, Init (UniMath.Foundations),/mnt/data1/2024/01/05/UniMath/UniMath/Foundations/Init.v??? LtacNotationCall:189, Init (UniMath.Foundations),/mnt/data1/2024/01/05/UniMath/UniMath/OrderTheory/DCPOs/Basis/Continuous.v:166
110
+ """
111
+ # source : unimath/unimath/batch2/data_42/BATCH142042
112
+
113
+ unimath_examples = [unimath1, unimath2, unimath3, unimath4]
114
+
115
  model_name = "deepseek-ai/deepseek-math-7b-instruct"
116
  tokenizer = AutoTokenizer.from_pretrained(model_name)
117
  model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")
118
  model.generation_config = GenerationConfig.from_pretrained(model_name)
119
  model.generation_config.pad_token_id = model.generation_config.eos_token_id
120
 
121
+ def parse_full_answer(answer):
122
+ """Parses the assistant's answer, excluding any text before 'Assistant :'."""
123
+ match = re.search(r"Assistant\s*:\s*(.*)", answer, re.DOTALL)
124
+ return match.group(1).strip() if match else "No assistant answer found."
125
+
126
+ def parse_final_answer(answer):
127
+ """Extracts the final answer enclosed within \boxed{}."""
128
+ match = re.search(r"\\boxed\{([^}]+)\}", answer)
129
+ return match.group(1) if match else "No final answer found."
130
+
131
  @spaces.GPU
132
+ def solve_math_problem(question, additional_info, max_tokens):
133
+ prompt = f"User: {question}\n{additional_info}.\nAssistant:"
134
  input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
135
  outputs = model.generate(input_ids, max_length=max_tokens + input_ids.shape[1], pad_token_id=model.generation_config.pad_token_id)
136
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
137
+ full_answer = parse_full_answer(result)
138
+ final_answer = parse_final_answer(result)
139
+ return full_answer, final_answer
140
 
141
  def main():
142
  with gr.Blocks() as demo:
143
  gr.Markdown(title)
144
+ final_answer_output = gr.Textbox(label="🔮DeepSeekMath📉")
145
+ full_answer_output = gr.Code(label="🔮TonicsMathAssistant📉", interactive=False)
146
+ max_tokens = gr.Slider(minimum=150, maximum=1200, value=250, label="Max Tokens")
147
+ submit_button = gr.Button("📉Solve")
148
+ question = gr.Code(language='python', value='what is the integral of x^2 from 0 to 2?', label="🤔Enter your math problem")
149
+ additional_info = gr.Text(value='Please reason step by step, and put your final answer within \\boxed{{}}', label="🪜Optional train-of-thought")
150
+ with gr.Accordion("🤖metaintrospector/UniMath Examples"):
151
+ for example in unimath_examples:
152
+ gr.Textbox(value=example, label="Example", readonly=True, lines=10)
153
+
154
+ submit_button.click(fn=solve_math_problem, inputs=[question, additional_info, max_tokens], outputs=[full_answer_output, final_answer_output])
155
 
156
  demo.launch()
157