CoolFace
Apppublic

wakeupmh/alem-do-espectro

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
model_handler.cpython-311.pyc49 linesDownload Raw Back to __pycache__
1�

2t�g���t�ddlZddlmZmZddlZddlmZddlm	Z	ddl3mZddlm
Z
dZGd�d	��ZdS)4�N)�
AutoTokenizer�AutoModelForCausalLM)�Agent)�Ollama)�5ArxivTools)�PubmedToolszmeta-llama/Llama-3.2-1Bc��eZdZd�Zd�Zeejejd�������Z	de6de7fd�Zede8fd���ZdS)	�ModelHandlerc��d|_d|_d|_d|_d|_d|_|���dS)zInitialize the model handlerN)�model�	tokenizer�9translator�10researcher�11summarizer�	presenter�_initialize_model��selfs �K/Users/devopmh/Documents/devopmh/ama-autism-pt-br/services/model_handler.py�__init__zModelHandler.__init__sD����12������������������� � � � � �c	��tddtd���ddg���|_tdd	td���t��t	��ggd13�d���|_td
dtd���gd�ddd���|_tddtd���gd�d���|_dS)zInitialize model and tokenizer�14Translatorz'You will translate the query to Englishzllama3.2:1b)�idzTranslate to EnglishzTranslate the query to English)�name�roler�goal�instructions�15Researcherz>You are a research scholar who specializes in autism research.)z�You need to understand the context of the question to provide the best answer based on your tools.Be precise and provide just enough information to be usefulzTYou must cite the sources used in your answer.You must create an accessible summary.�8The content must be for people without autism knowledge.�MFocus in the main findings of the paper taking in consideration the question.zThe answer must be brief.T)rrr�toolsr�show_tool_calls�16SummarizerzXYou are a specialist in summarizing research papers for people without autism knowledge.)17z5You must provide just enough information to be usefulz.You must cite the sources used in your answer.zYou must be clear and concise.z&You must create an accessible summary.r r!ztThe answer must be brief.Remove everything related to the run itself like: 'Running: transfer_', just use plain text�FYou must use the language provided by the user to present the results.�1Add references to the sources used in the answer.�YAdd emojis to make the presentation more interactive.Translaste the answer to Portuguese.)rrrrr#�markdown�add_references�	PresenterzKYou are a professional researcher who presents the results of the research.)zYou are multilingualz;You must present the results in a clear and concise manner.z2Clenaup the presentation to make it more readable.zRemove unnecessary information.z[Remove everything related to the run itself like: 'Running: transfer_', just use plain textr%r&r')rrrrr)N)rrrrrrrrrs rrzModelHandler._initialize_models�� ��:��M�*�*�*�'�0��18�19�20��� ��Q��M�*�*�*��<�<����/����!�21�22�23���  ��k��M�*�*�*�
�
�
�!���)24�25�26���.��^��M�*�*�*�27�28�29� �30�31�32����rc���	tjt��}tjt��}||fS#t$r/}tjdt|������Yd}~dSd}~wwxYw)NzError loading model: )NN)r�from_pretrained�33MODEL_PATHr�	Exception�logging�error�str)r
r�es   r�_load_modelzModelHandler._load_model]s}��	�%�5�j�A�A�I�(�8��D�D�E��)�#�#���	�	�	��M�:�#�a�&�&�:�:�;�;�;��:�:�:�:�:�����	���s�58�34A1�$A,�,A1�query�returnc��	|j�|d���}tjd��|j�|jd���}tjd��|j�|jd���}tjd��|j�|jd���}tjd��|js|���S|jS#t$rB}tj35dt|������|���cYd}~Sd}~wwxYw)NF)�streamzTranslated queryzGenerated researchzGenerated summaryzGenerated presentationzError generating answer: )r�runr/�infor�contentrr�_get_fallback_responser.r0r1)rr4r�research�summary�presentationr2s       r�generate_answerzModelHandler.generate_answeris@��	1���,�,�U�5�,�A�A�J��L�,�-�-�-���*�*�:�+=�e�*�L�L�H��L�.�/�/�/��o�)�)�(�*:�5�)�I�I�G��L�-�.�.�.��>�-�-�g�o�e�-�L�L�L��L�2�3�3�3��'�
5��2�2�4�4�4��'�'���	1�	1�	1��M�>�c�!�f�f�>�>�?�?�?��.�.�0�0�0�0�0�0�0�0�����	1���s$�C)C3�,C3�336D?�=7D:�4D?�:D?c��dS)z-Provide a friendly, helpful fallback responseuz37            Peço descula, mas encontrei um erro ao gerar a resposta. Tente novamente ou refaça a sua pergunta.38        �rArrr;z#ModelHandler._get_fallback_response{s���rN)
�__name__�39__module__�__qualname__rr�staticmethod�st�cache_resource�40cache_datar3r1r?r;rArrr41r42s�������!�!�!�D43�D44�D45�N����]����]����\��1�S�1�S�1�1�1�1�$��C�����\���rr46)r/�transformersrr�	streamlitrF�47agno.agentr�agno.models.ollamar�agno.tools.arxivr�agno.tools.pubmedrr-r48rArr�<module>rOs�������<�<�<�<�<�<�<�<�����������%�%�%�%�%�%�'�'�'�'�'�'�)�)�)�)�)�)�
&�49�u�u�u�u�u�u�u�u�u�ur