Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738import spacesimport gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfigimport torchtitle = """# ๐๐ปโโ๏ธWelcome to๐Tonic's๐ฎDeepSeekMath๐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 !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>Join us : ๐TeamTonic๐ is always making cool demos! Join our active builder's ๐ ๏ธcommunity ๐ป [](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 ๐ค"""model_name = "deepseek-ai/deepseek-math-7b-instruct"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")model.generation_config = GenerationConfig.from_pretrained(model_name)model.generation_config.pad_token_id = model.generation_config.eos_token_id@spaces.GPUdef solve_math_problem(question, max_tokens):prompt = f"User: {question}\nPlease reason step by step, and put your final answer within \\boxed{{}}.\nAssistant:"input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)outputs = model.generate(input_ids, max_length=max_tokens + input_ids.shape[1], pad_token_id=model.generation_config.pad_token_id)result = tokenizer.decode(outputs[0], skip_special_tokens=True)return resultdef main():with gr.Blocks() as demo:gr.Markdown(title)with gr.Row():question = gr.Code(language='python', value='what is the integral of x^2 from 0 to 2?', label="Enter your math problem")max_tokens = gr.Slider(minimum=150, maximum=1200, value=250, label="Max Tokens")submit_button = gr.Button("Solve")output = gr.Code(label="๐ฎDeepSeekMath๐", interactive=False)submit_button.click(fn=solve_math_problem, inputs=[question, max_tokens], outputs=output)demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157import spacesimport reimport gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfigimport torchtitle = """# ๐๐ปโโ๏ธWelcome to๐Tonic's๐ฎDeepSeekMath๐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 !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>Join us : ๐TeamTonic๐ is always making cool demos! Join our active builder's ๐ ๏ธcommunity ๐ป [](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 ๐ค"""unimath1 = """Goal:X : UUY : UUP : UUxp : (X โ P) โ Pyp : (Y โ P) โ PX0 : X ร Y โ Px : X============================(Y โ P)DEBUG:Going to execute:PTRDEBUGTAC<coq-core.plugins.ltac::intro@1> $1DEBUG LTAC Evaluated term: ypTcDebug (0) > NONE??? LtacNotationCall (no location),/mnt/data1/2024/01/05/UniMath/UniMath/Foundations/PartA.v:234"""# source : unimath/unimath/batch2/data08unimath2 = """Goal:R : ring M : module R============================(islinear (idfun M))DEBUG:Going to execute:PTRDEBUGTACapply pathsinv0; trivialLevel 0: Backtrace:Proof is not complete.Level 0: Backtrace:Proof is not complete.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"""# source : unimath/unimath/batch2/data_22/BATCH122007unimath3 = """Goal:X : UU i : nat b : hProptoType (i < S i) x : Vector X (S i) r : i = i============================(pr1 lastelement = pr1 (i,, b))DEBUG:Going to execute:PTRDEBUGTACsimplDEBUG LTAC Evaluated term: isinjstntonatTcDebug (0) > /mnt/data1/2024/01/05/UniMath/UniMath/Combinatorics/FiniteSequences.v:114"""# source : unimath/unimath/batch2/data_12/BATCH112026unimath4 = """Goal:X : dcpo CX : continuous_dcpo_struct X x : pr1hSet X y : pr1hSet X============================(x โ yโ (โ i : approximating_family CX x, approximating_family CX x i โ y))DEBUG:Going to execute:PTRDEBUGTACsimple refine (p _ _ _) ||simple refine (p _ _ _ _) ||simple refine (p _ _ _ _ _) ||simple refine (p _ _ _ _ _ _) ||simple refine (p _ _ _ _ _ _ _) ||simple refine (p _ _ _ _ _ _ _ _) ||simple refine (p _ _ _ _ _ _ _ _ _) ||simple refine (p _ _ _ _ _ _ _ _ _ _) ||simple refine (p _ _ _ _ _ _ _ _ _ _ _) ||simple refine (p _ _ _ _ _ _ _ _ _ _ _ _) ||simple refine (p _ _ _ _ _ _ _ _ _ _ _ _ _) ||simple refine(p _ _ _ _ _ _ _ _ _ _ _ _ _ _) || simplerefine (p _ _ _ _ _ _ _ _ _ _ _ _ _ _ _)Level 0: Backtrace:In environmentX : dcpoCX : continuous_dcpo_struct Xx, y : XThe term "weqimplimpl ?f ?g" has type "isaprop ?X โ isaprop ?Y โ ?X โ ?Y"while it is expected to have type"x โ y โ (โ i : approximating_family CX x, approximating_family CX x i โ y)".Level 0: Backtrace:In environmentX : dcpoCX : continuous_dcpo_struct Xx, y : XThe term "weqimplimpl ?f ?g" has type "isaprop ?X โ isaprop ?Y โ ?X โ ?Y"while it is expected to have type"x โ y โ (โ i : approximating_family CX x, approximating_family CX x i โ y)".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"""# source : unimath/unimath/batch2/data_42/BATCH142042unimath_examples = [unimath1, unimath2, unimath3, unimath4]model_name = "deepseek-ai/deepseek-math-7b-instruct"tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, device_map="auto")model.generation_config = GenerationConfig.from_pretrained(model_name)model.generation_config.pad_token_id = model.generation_config.eos_token_iddef parse_full_answer(answer):"""Parses the assistant's answer, excluding any text before 'Assistant :'."""match = re.search(r"Assistant\s*:\s*(.*)", answer, re.DOTALL)return match.group(1).strip() if match else "No assistant answer found."def parse_final_answer(answer):"""Extracts the final answer enclosed within \boxed{}."""match = re.search(r"\\boxed\{([^}]+)\}", answer)return match.group(1) if match else "No final answer found."@spaces.GPUdef solve_math_problem(question, additional_info, max_tokens):prompt = f"User: {question}\n{additional_info}.\nAssistant:"input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)outputs = model.generate(input_ids, max_length=max_tokens + input_ids.shape[1], pad_token_id=model.generation_config.pad_token_id)result = tokenizer.decode(outputs[0], skip_special_tokens=True)full_answer = parse_full_answer(result)final_answer = parse_final_answer(result)return full_answer, final_answerdef main():with gr.Blocks() as demo:gr.Markdown(title)final_answer_output = gr.Textbox(label="๐ฎDeepSeekMath๐")full_answer_output = gr.Code(label="๐ฎTonicsMathAssistant๐", interactive=False)max_tokens = gr.Slider(minimum=150, maximum=1200, value=250, label="Max Tokens")submit_button = gr.Button("๐Solve")question = gr.Code(language='python', value='what is the integral of x^2 from 0 to 2?', label="๐คEnter your math problem")additional_info = gr.Text(value='Please reason step by step, and put your final answer within \\boxed{{}}', label="๐ชOptional train-of-thought")with gr.Accordion("๐คmetaintrospector/UniMath Examples"):for example in unimath_examples:gr.Textbox(value=example, label="Example", readonly=True, lines=10)submit_button.click(fn=solve_math_problem, inputs=[question, additional_info, max_tokens], outputs=[full_answer_output, final_answer_output])demo.launch()
app.py+130-11