CoolFace
Apppublic

topabaem/memory-harness-gemma4

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
app.cpython-311.pyc91 linesDownload Raw Back to __pycache__
1�

2�%�i�$����dZddlmZddlZddlZddlmZddlmZddl	m3Z4ddlZddl
Z
ee�����jZee��ejvr$ej�dee����ddlmZmZmZmZ	ddlZn"#e$rGd�d	��Ze��ZYnwxYwd5Zeejdd����Z d
Z!da"da#da$d[d�Z%d\d�Z&d]d�Z'ej(d���e
j)��d^d%�����Z*d_d&�Z+d`d'�Z,dad+�Z-ejd,��d-kr6e%��ej.��5Z/ej0d.��ej1d/��5ej2��5ej3e!d0d1�2��Z4ddd��n#1swxYwYej2��5ej5d3d4d5d6d7�8��Z6ej5d9d:d;d<d=�8��Z7ddd��n#1swxYwYej2��5ej8d>d?�@��Z9ej5dAdBdAdAdC�8��Z:ddd��n#1swxYwYej;e,e4e6e7e9e:gdddDdDdEe!d5d;d>dAgdFe!d5d;d>dAgdGe!d5d;d>dAgg�H��ddd��n#1swxYwYej1dI��5ej0dJ��ej3dKdLdM�N��Z<ej5dAdBd1dAdO�8��Z=ej>dPdQ�R��Z?ej0dS�T��Z@ej3dUdV�W��ZAe?�Be-e<e=ge@eAgdX�Y��ddd��n#1swxYwYddd��n#1swxYwYeCdZkre/�D��dSdS)bz'Gradio Space app for Gemma 4 text chat.�)�annotationsN)�Iterator)�Path)�Thread)�format_search_grounding�format_search_markdown�search_project_notes�serialize_hitsc�(�eZdZeddd���ZdS)�
_SpacesCompatN�duration�7int | Nonec��d�}|S)Nc��|S�N�)�fns �:/Users/guribbong/code/memory_clear_haness/space_app/app.py�8_decoratorz%_SpacesCompat.GPU.<locals>._decorators���	�r)r
rs  r�GPUz_SpacesCompat.GPUs��
�
�
��rr)r
r)�__name__�9__module__�__qualname__�staticmethodrrrrrrs7������	�	�	�	�	�10��	�	�	rrzgoogle/gemma-4-e4b-it�MAX_INPUT_TOKENS�10000zhYou are a precise assistant helping evaluate a local memory harness project. Answer clearly and briefly.�return�Nonec�|�t�	t�dStjd��dkrdadS	ddlm}	ddlm}n#t$r	ddlm	}YnwxYw|�11td�	��a|�12td13tj
���adS#t$r}t|��aYd}~dSd}~wwxYw)N�SPACE_DISABLE_MODEL_LOAD�1z+Model loading disabled for local test mode.r)�
AutoProcessor)�AutoModelForMultimodalLM)�AutoModelForImageTextToTextF)�use_fast�auto)�14device_map�dtype)�	processor�model�os�getenv�15LOAD_ERROR�transformersr#r$�ImportErrorr%�from_pretrained�MODEL_ID�torch�bfloat16�	Exception�str)r#�ModelLoader�excs   r�_ensure_model_loadedr91s�����!2���	�y�+�,�,��3�3�B�16���
�.�.�.�.�.�.�	P�L�L�L�L�L�L�L���	P�	P�	P�O�O�O�O�O�O�O�O�	P����!�1�1�(�U�1�K�K�	��+�+����.�,�17�18�����19������X�X�20�21�22�23�24�25�26��������s8�B�<�B�A�B�A�AB�27B;�!B6�6B;�messager6�history�list�
system_prompt�28list[dict]c���g}|���r.|�dd|���d�gd���|D�]}t|t��rU|d}|d}t|t��r d�d�|D����}njt
|��}nZ|\}}	|�d	dt
|��d�gd���|	r)|�d29dt
|	��d�gd�����|d	kr|�d	d|d�gd�����|�d30d|d�gd�����|�d	d|d�gd���|S)N�system�text)�typerA)�role�contentrCrD� c3�lK�|]/}t|t���|�dd��V��0dS)rA�N)�31isinstance�dict�get)�.0�parts  r�	<genexpr>z"_build_messages.<locals>.<genexpr>QsA����c�c��J�W[�]a�Lb�Lb�c������ 4� 4�c�c�c�c�c�cr�user�	assistant)�strip�appendrHrIr<�joinr6)32r:r;r=�messages�itemrCrDrA�	user_text�assistant_texts33          r�_build_messagesrWHs����H������j������P]�Pc�Pc�Pe�Pe�7f�7f�6g�h�h�i�i�i��`�`���d�D�!�!�	���<�D��9�o�G��'�4�(�(�
$��x�x�c�c��c�c�c�c�c����7�|�|���(,�%�I�~��O�O�V�&�RU�V_�R`�R`�9a�9a�8b�c�c�d�d�d��
s�����6�[^�_m�[n�[n�Bo�Bo�Ap� q� q�r�r�r���6�>�>��O�O�V�&�RV�9W�9W�8X�Y�Y�Z�Z�Z�Z��O�O�[�v�W[�>\�>\�=]�^�^�_�_�_�_��O�O�V�&�'�1R�1R�0S�T�T�U�U�U��Or�
enable_search�bool�search_top_k�int�tuple[str, list]c���|s|gfSt||���}t|��}|s||fS|���r|����d|��|fS||fS)N��top_kz34 35)r	rrP)r:r=rXrZ�hits�	groundings      r�_inject_search_contextrbbs����!��b� � ���|�<�<�<�D�'��-�-�I��#��d�"�"������?��%�%�'�'�8�8�Y�8�8�$�>�>��d�?�r�x)r
rS�max_new_tokens�temperature�float�
Iterator[str]c#�F��
K�t��t�t�tjdt36pd�����ddlm}t�|dddd���}|dj	d	}|tkr tjd37|�dt�d����|�tjtj�
��}|tddd���}tt!|��d��}i|�||||dkdd���
g�d��
fd�}t#|���}	|	���g}38|D].}|39�|��d�|40��V��/|	����rtjd�d�����dS)NzGemma 4 model is not loaded. zUnknown load failure.r)�TextIteratorStreamerT�pt)�tokenize�return_dict�return_tensors�add_generation_prompt�	input_ids�zInput too long (z tokens). Maximum is �.)�devicer)g>@)�timeout�skip_prompt�skip_special_tokensg�h㈵��>)�streamerrdre�	do_sample�disable_compilerrc���	tjdi���dS#t$r }��|��Yd}~dSd}~wwxYw)Nr)r+�generater5rQ)r8�errors�kwargss ��r�_runz_stream_generate.<locals>._run�sg���	��N�$�$�V�$�$�$�$�$���	�	�	��M�M�#��������������	���s��41A�;�A)�targetrGzGeneration failed: �rr)r9r*r+�gr�Errorr.�!transformers.generation.streamersri�apply_chat_template�shaper�torrr3r4�maxrfr�startrQrR)rSrdreri�inputs�n_tokensrv�generation_temperaturer}�thread�chunks�chunkr{r|s            @@r�_stream_generater�ns�������������E�M��h�^�z�7\�E\�^�^�_�_�_�F�F�F�F�F�F�
�
*�
*�����"�+���F��k�"�(��+�H��"�"�"��h�\�(�\�\�IY�\�\�\�]�]�]�
�Y�Y�e�l�%�.�Y�
A�
A�F�#�#�I�t��cg�h�h�h�H� ��{�!3�!3�T�:�:���42���(�-� �1�_��
���F�!�F���������4�
 �
 �
 �F�43�L�L�N�N�N��F������
�
�e�����g�g�f�o�o�����44�K�K�M�M�M�
�:��h�8�V�A�Y�8�8�9�9�9�:�:rc#�K�|���stjd���t|||��}t	|||���Ed{V��dS�NzPlease enter a message.)rdre)rPr�r�rWr�)r:r;r=rdrerSs      r�chatr��sf�����=�=�?�?�2��h�0�1�1�1��w���?�?�H����U`�a�a�a�a�a�a�a�a�a�a�a�arc#��K�|���stjd���t||||��\}}t	|||��}	t|	||���Ed{V��dSr�)rPr�r�rbrWr�)45r:r;r=rdrerXrZ�grounded_prompt�_rSs46          r�
grounded_chatr��s������=�=�?�?�2��h�0�1�1�1�/���
�Wc�d�d��O�Q��w���A�A�H����U`�a�a�a�a�a�a�a�a�a�a�a�ar�queryr_�tuple[str, str]c�`�t||���}t|��t|��fS)Nr^)r	rr47)r�r_r`s   r�48run_searchr��s/����U�3�3�3�D�!�$�'�'���)=�)=�=�=rr!r"aP49        # Memory Harness Gemma 450        Text-only Hugging Face Space using `google/gemma-4-E4B-it`.51 52        This Space is meant to validate that the repository can be paired with a hosted Hugging Face Gemma 4 demo UI.53        It now includes lightweight project-note search so the model can ground answers before responding.54        �Chatz
System prompt�)�value�label�lines�@i�� zMax new tokens)�minimum�maximumr��stepr�gg333333�?g�������?g�������?�TemperatureTzEnable project search grounding)r�r�rp�zSearch top-kFzLSummarize why raw archive retrieval matters for exact-date memory questions.z>Explain the difference between summary memory and fact memory.z9Give me a concise evaluation rubric for a memory harness.)r�additional_inputs�title�description�cache_examples�run_examples_on_click�examples�Searchz[Search the bundled project notes directly and inspect what the model can use for grounding.zSearch query�zCAsk about raw archive, summary memory, evaluation, or architecture.)r�r��placeholderz
Top-k resultsz55Run search�primary)�variantzNo search results yet.)r�zRaw result JSON�)r�r��search)r��outputs�api_name�__main__r)r:r6r;r<r=r6rr>)56r:r6r=r6rXrYrZr[rr\)rSr>rdr[rerfrrg)57r:r6r;r>r=r6rdr[rerf)r:r6r;r>r=r6rdr[rerfrXrYrZr[)r�r6r_r[rr�)E�__doc__�58__future__rr,�sys�collections.abcr�pathlibr�	threadingr�gradior�r3�__file__�resolve�parent�
SPACE_APP_DIRr6�path�insert�search_backendrrr	r59�spacesr0rr2r[r-r�DEFAULT_SYSTEM_PROMPTr*r+r.r9rWrbr�inference_moder�r�r�r��Blocks�demo�Markdown�Tab�Row�Textboxr=�Sliderrdre�CheckboxrXrZ�
ChatInterface�search_query�search_results_top_k�Button�
search_button�search_markdown�60search_raw�clickr�launchrrr�<module>r�s���-�-�"�"�"�"�"�"�	�	�	�	�61�62�63�64�$�$�$�$�$�$�����������������������X���&�&�(�(�/�
��3�}���S�X�%�%��H�O�O�A�s�s�=�)�)�*�*�*���������������M�M�M�M���	�	�	����������]�_�_�F�F�F�	����#���3�y�r�y�!3�W�=�=�>�>��"��65
�	���
�66�����.����4	�	�	�	����S���������-:�-:�-:�����-:�`b�b�b�b�
b�
b�
b�
b� >�>�>�>�67
�2�9�
'�(�(�C�/�/�������R�Y�[�[�@O�D��B�K�	����68������.69�.70�
�R�V�X�X�	�	�&�B�J�+�%�����M�	�	�	�	�	�	�	�	�	�	�	����	�	�	�	��R�V�X�X�	h�	h�&�R�Y�r�4�s�QS�[k�l�l�l�N�#�"�)�C��C�c�Yf�g�g�g�K�	h�	h�	h�	h�	h�	h�	h�	h�	h�	h�	h����	h�	h�	h�	h��R�V�X�X�	b�	b�'�B�K�d�:[�\�\�\�M�$�2�9�Q����R`�a�a�a�L�	b�	b�	b�	b�	b�	b�	b�	b�	b�	b�	b����	b�	b�	b�	b�	����,�n�k�=�Zf�g��� �"'�c�)�����
�U�)�����
�P�)�����
�#�!	71�!	72�!	73�!	74�.75�.76�.77�.78�.79�.80�.81�.82�.83�.84�.85����.86�.87�.88�.89�^90����	�	�O�O����q�r�r�r�!�r�z��a�NS�T�T�T��(�r�y��A�Q�Q�Ve�f�f�f��!��	�,�	�B�B�B�
�%�"�+�,D�E�E�E���R�Z�&7�r�B�B�B�91����J��>R�/S�^m�oy�]z�FN��	O�	O�	O�O�O�O�O�O�O�O�O�O�O�O����O�O�O�O�s@O�@O�@O�@O�@O�@O�@O�@O�@O�@O�@O����@O�@O�@O�@O�F�z����K�K�M�M�M�M�M��s��B�B4�3B4�!M�1J�F!�J�!F%�%J�(F%�)J�;+G2�&J�2G6�6J�9G6�:J�(I�4J�I�J�I�4J�<M�J	�M�J	�M�#BL:�.M�:L>	�>M�L>	�M�M�M