fddlZddlZddlZddlZddlZddlmZddlmZddl m Z ddl Z ddl ZddlmZGddZdZdZd Zd Zd Zy) N)Counter)contextmanager) dataclassc*eZdZddZedZdZy) PythonREPLc||_y)N)timeout)selfr s BC:\Users\acm\Desktop\huggingfacespace\deepseekmath\codeexecutor.py__init__zPythonREPL.__init__s  ctjd|gddd}|jdk(rd|jj fS|j j }|j d}g}d}|D]}d|vr|j|||dk(r|j|3||vrfd |vr|jd d znd}||vr|j|d znd}|s|||n||d} |j| d }|j|d}|s|j|d}ddj|j fS) Npython3TF)capture_outputchecktextr Tracebackz"/) subprocessrun returncodestdoutstripstderrsplitappendindexreplacejoin) temp_file_pathresult error_msgmsgsnew_msgs want_nextmstedclrs r _run_codezPythonREPL._run_codesU  '      !,,.. . ++-I??4(DHI &!#OOA&$r(]OOA&#q(.2ai*QB8F!8K014QUB*,!Br(!BC&C #r*AOOA& $IOOA& %I &$))H-3355 5r cHd|z}|jjd}d|dvr/d|dvr|djdd|d<d|dzdz|d<dj|}tj5}t j j|d}t|d d 5}|j|dddtjd 5}|j|j|f} |j|j\}} ddddddfS#1swYpxYw#tj$r7|j!d d|jdfcYcdddcdddSwxYw#1swYnxYw#1swYfSxYw)Nz2import math import numpy as np import sympy as sp rzprint(r#r)ztmp.pywzutf-8)encodingrFzTimed out after z seconds.)rrr"tempfileTemporaryDirectoryospathopenwritemultiprocessingPool apply_asyncr-getr TimeoutError terminate) r querytemp_dirr#fpoolr$successoutputs r __call__zPythonREPL.__call__2sG%O ##D) 59 $eBi!"IOOC03b  59,s2E"I %   ( ( * MhWW\\(H=NncG<  !%%a( MD))$..>:KLM&,jj&>OGV M M  '33MNN$ $4T\\N)"LLL  M M M MM  M M Ms`/F/D0FF =D<F0D9 5F<5F1F 2 FFF  F FF!N))__name__ __module__ __qualname__r staticmethodr-rEr r rrs 66>r rctjd|tj}t|dk(r ||rdfS|fS|r|dg}g}g}|D]k}d}dD].} | |vs| d} |j | |j |0 ||\}} |s|sd} |j | |j |mt|dj} |d}|r| |fS| dfS#t $r} t d| } Yd} ~ pd} ~ wwxYw) Nz```python(.*?)```rFr)rvenvz is not allowedzCode timed outr) refindallDOTALLlenrr=printstrr) executor completion return_statuslast_code_block executionsoutputs successescoderClibrDes r execute_completionr^Js70*biiHJ :!M5AAzAA n% GI") Cd{50v&  )    &tnOGV}Fv!!""  # # %FmGw 5=  " #F s C$$ D- C??Dc:t}t||||}~|S)N)rVrW)rr^)rrVrWrTr$s r postprocess_completionr`js"|H $m]l mF Mr c`t|syt|}|jd\}}|S)Nr)rQr most_common)answerscvalue_s r get_majority_votergqs/ w<A}}q!HE1 Lr ctj|}|jry|jry|j ryy)NRealComplex PolynomialOther)spsympifyis_real is_complex is_polynomial)expr_strexprs r type_checkrtys@zz(# << ??    r cb tjd}tj|}tj||d}t j ddd}||}t jt j||t jdt jdt jdt jdd }t j|t j|S#t$r}t!d |Yd}~yd}~wwxYw) Nxnumpyi izPolynomial PlotyTzpolynomial_plot.pngzError in draw_polynomial_plot: )rmsymbolsrnlambdifynplinspacepltfigureplottitlexlabelylabelgridsavefigclose ExceptionrR) expressionrv poly_expr poly_lambdax_valsy_vals plot_filenamer]s r draw_polynomial_plotrs JJsOJJz* kk!Y8 S"c*V$    #$ 3 3 .  M"   /s34sD D D.D))D.)r5rNrr3r9 collectionsr contextlibr dataclassesrsympyrmrwr|matplotlib.pyplotpyplotr~rr^r`rgrtrrKr r rsK %!99x@*r