CoolFace
Apppublic

Agents-MCP-Hackathon/Misty_Climate_Agent

sourceHugging Faceupdated 1y agoView on Hugging Face
3likes
agent.cpython-312.pyc149 linesDownload Raw Back to __pycache__
1�

2lpIh�a���ddlZddlZddlZddlZddlmZmZmZmZddl	m3Z4ddlZddlm
Z
mZddlmZddlmZmZmZddlmZddlmZmZddlZddlZddlZdd	lmZGd5�de�Z dZ!d1d
e"de"fd�Z#defd�Z$defd�Z%defd�Z&defd�Z'd2dee"d
ee"de"dee"effd�Z(d3de"de"de"dee"effd�Z)d4dee"efde"dee"effd�Z*d5d e"d!ee"efde"fd"�Z+d6dede"de"fd#�Z,d3ded$e"de"fd%�Z-d7ded&ee"efd'e.d(e/fd)�Z0d8ded e"fd*�Z1defd+�Z2defd,�Z3defd-�Z4e5d.k(r"ed/�0�Z6ejne4e6��yy)9�N)�Dict�Any�Optional�Union)�Path)�MCPClientSSE�SSEServerParams)�Mistral)�UserMessage�AssistantMessage�ToolMessage)�	BaseModel)�Audio�display)�gTTSc�P�eZdZUeed<eeed<eed<eeefed<eed<y)�AnalysisDescription�
document_type�key_findings�summary�metadata�confidence_scoreN)	�__name__�6__module__�__qualname__�str�__annotations__�listrr�float���Vd:\Office\Project\AIML PRO\MY PRO\HFMCPHACK\Mistral\agent\Misty_Climate_Agent\agent.pyrrs+�����s�)��
�L��3��8�n���r!rzmistral-large-latest�url�	save_pathc��	|jd�rYtjj|�j}tjj|j
d��}|Stj|d��}|j�t|d�5}|j|j�ddd�|}|S#1swY�
xYw#t$r/}tdt|����dt|���cYd}~Sd}~wwxYw)N�file://�/�7)�timeout�wbzError handling audio: �Error: )�8startswith�urllib�parse�urlparse�path�unquote�lstrip�requests�get�raise_for_status�open�write�content�	Exception�printr)r#r$�	file_path�response�f�es      r"�play_wavr?s���
"��>�>�)�$����-�-�c�2�7�7�I����,�,�Y�-=�-=�c�-B�C�I��� �|�|�C��4�H��%�%�'��i��&�
*�!�����(�(�)�
*�!�I���
*�
*���"�
�&�s�1�v�h�/�0���Q���!�!��"�s<�A)C�,3C�C�;C�C�C�	D�$D�D�D�clientc��|jjjtddddddddd	d9�ddd10�ddd11�d
�d�d�d�g��S)N�DocAgentz@Converts OCR PDFs to JSON using document processing capabilitiesz�Process documents by extracting text and structure, then convert to JSON format. Focus on climate-related documents and extract key data points.�function�process_climate_document�KProcess climate documents from file path or URL and extract structured data�object�string�Path to the document file��type�description�URL to the documentz7Type of climate document (report, analysis, data, etc.)�r;r#r�rJ�12properties��namerK�13parameters�rJrC��modelrQrK�instructions�tools��beta�agents�create�MODEL�r@s r"�create_doc_agentr^-s����;�;���$�$��
�V�h�#�6�#p� (�)1�/J�*�14)1�/D�$�15)1�/h�.�
'�#��
�16�%��r!c��|jjjtddddddddd	d17�ddd18�ddd19�d
�ddgd�d�d�g��S)N�20ImageAgentz=Converts image PDFs to JSON using image analysis capabilitiesz�Analyze image-based documents, extract text and visual elements, then structure the data as JSON. Handle charts, graphs, and tabular data effectively.rC�
analyze_image�3Analyze image documents and extract structured datarFrG�Base64-encoded image datarI�"Image format (png, jpg, pdf, etc.)zOSpecific focus for analysis (text_extraction, chart_analysis, table_extraction)��21image_data�image_format�analysis_focusrfrg�rJrO�requiredrPrSrTrXr]s r"�create_image_agentrkPs����;�;���$�$��
�S�n�#�+�#X� (�)1�/J�+�22)1�/S�-�23)1�0A�/�
'�&2�>�$B�!#��
�24�%� � r!c��|jjjtdddddddddd	�d25dd	�d�d
gd�d�d�g��S)N�JsonAnalyzerAgentzRAnalyzes JSON outputs from DocAgent or ImageAgent, producing detailed descriptionsz�Analyze JSON data structures, identify patterns, extract insights, and provide comprehensive analysis. Output should be structured and detailed.rC�analyze_json_data�>Process and analyze JSON data to extract insights and patternsrF�JSON data to analyzerIrGz>Type of analysis to perform (statistical, content, structural)��	json_data�
analysis_typerrrirPrSrTrXr]s r"�create_json_analyzer_agentrtts{���;�;���$�$��
 �h�h�#�/�#c� (�)1�/E�*�26)1�/o�.�	'�&1�M�
#��
�27�%��r!c��|jjjtddddddddd	d28�dddd
�ddd
�ddd
�d�d�d�dgd�d�d�g��S)N�SpeechAgentz9Converts text analysis from JsonAnalyzerAgent into speechzConvert text analysis into natural speech format. Optimize text for spoken delivery and handle technical content appropriately.rC�text_to_speech�Convert text to speech audiorFrG�Text to convert to speechrI�number��?�rJ�default�neutral��speed�pitch�29voice_typerN��text�voice_settingsr�rirPrSrTrXr]s r"�create_speech_agentr��s����;�;���$�$��
�O�W�#�,�#A� (�)1�/J�%�30)1�6>�3�-O�6>�3�-O�;C�PY�2Z�/"�/�
'�&,�H�!#��
�31�%� � r!r;r�returnc	�0�d|xs||dddgd�d�ddd	d32�d�S)z%Simulate document processing function�doc_001z1Climate change impacts are increasing globally...u1.5°C�420ppm)�Arcticz
Coastal AreaszTropical Regions)�temperature_increase�33co2_levels�affected_regions�-gq=34ףp�?z2.3s)�pages�extraction_confidence�processing_time)�document_id�sourcerJ�extracted_text�key_datarr rMs   r"�!simulate_process_climate_documentr��s=��!��"�s��M�$,�"� O�35��%)�%�36��r!rfrgrhc�4�d||dddggd�dd�gd�d	d37dd�d
d�S)z Simulate image analysis function�img_001z&Global Temperature Anomalies 2020-2024�line_chart_temperatures�bar_chart_emissions)�Year�Temperature�Anomaly�)�headers�rows)r��charts�tables���)�charts_detected�tables_detected�text_regionsg)\���(�?)�image_id�formatrs�extracted_content�visual_elements�38confidencer res   r"�simulate_analyze_imager��sH����'�<�0�2G�H�#E�q�Q�R�39� !� ��40�41��r!rrrsc� �dgd�dddd�gd�d�S)	zSimulate JSON analysis functionz1Comprehensive climate document analysis completed)z1Temperature data shows accelerating warming trendz3Regional variations indicate uneven climate impactsz3Emission data correlates with temperature increasesg��Q��?gףp=42��?g{�G�z�?)�completeness�consistency�reliability)z-Focus on high-impact regions for interventionz$Monitor temperature trends quarterlyz'Implement emission reduction strategies)�analysis_summary�key_insights�data_quality�recommendationsr rqs  r"�simulate_analyze_json_datar��s,��P�43�!���44�4546��r!r�r�c�|�td|dd�d��d}	t|d��}|j|�tjj|�st
d|����dtjj|���S#t$r/}td	t|����d47t|���cYd}~Sd}~wwxYw)NzConverting to speech: �dz...z/tmp/generated_speech.wav�en�r��langzFailed to save audio to r&zError generating speech: r+)48r:r�save�osr0�exists�FileNotFoundError�abspathr9r)r�r�r$�ttsr>s     r"�simulate_text_to_speechr��s���	�"�4���:�,�c�492�3�+�I�"���4�(��������w�w�~�~�i�(�#�&>�y�k�$J�K�K��������3�4�5�5���"�
�)�#�a�&��2�3���Q���!�!��"�s�A,B�	B;�$B6�0B;�6B;c	���K�td�	ddddddd�dd	d�dd50d�d�d�d
�d�g}td|�d|����g}|jjt||���d{���}td�t|j51djj�|j52djjrz|j53djjD]T}|jjdk(s�t||��}td�ttj|d����V|S7��#t$r!}tdt|����Yd}~yd}~wwxYw�w)Nz(Starting document processing workflow...rCrDrErFrGrHrIrLzType of climate documentrMrNrPrSz Process the climate document at z	 of type �r8�rU�messagesrWzDocument processing response:r)r;rzDocument processing result:r���indentzError in document workflow: )r:r�chat�complete_asyncr\�choices�messager8�54tool_callsrCrQr��json�dumpsr9r)	r@r;r�doc_toolr�r<�	tool_call�55doc_resultr>s	         r"�process_document_workflowr��s~����	�564�5�,�#�6�#p� (�2:�Kf�)g�,4�EZ�#[�6>�Oi�-j�'�#��
�57��&
�"B�9�+�Y�Wd�Ve� f�g�58�� ���3�3����4�59�60��	�-�.�
�h���q�!�)�)�1�1�2����A��&�&�1�1�%�-�-�a�0�8�8�C�C�
<�	��%�%�*�*�.H�H�!B�Y�fs�!t�J��7�8��$�*�*�Z��:�;�	
<���!61��$��
�,�S��V�H�5�6����sH�E+�AD>�!D<�"BD>�;D>�;E+�<D>�>	E(�E#�E+�#E(�(E+�62image_pathc	���K�td�	tjj|�st	d|����t|d�5}t
j|j��jd�}ddd�ddddd	d63d�d	dd�d	d
d�d�ddgd�d�d�g}td|�d|����g}|jjt||���d{���}td�t|jdjj �|jdjj"r{|jdjj"D]U}|j$j&dk(s�t)d|��}	td�tt+j,|	d����W|S#1swY��;xYw7��#t.$r!}64tdt1|65����Yd}66~67yd}68~69wwxYw�w)Nz%Starting image processing workflow...zImage file not found: �rbzutf-8rCrarbrFrGrcrIrdzSpecific focus for analysisrerfrgrirPrSzAnalyze the image document at z with focus on r�r�zImage processing response:r�jpgzImage analysis result:r�r�zError in image workflow: )r:r�r0r�r�r6�base64�	b64encode�read�decoderr�r�r\r�r�r8r�rCrQr�r�r�r9r)r@r�rh�70image_filerf�71image_toolr�r<r��image_resultr>s           r"�process_image_workflowr�0s�����	�721�2�9��w�w�~�~�j�)�#�&<�Z�L�$I�J�J��*�d�
#�	M�z��)�)�*�/�/�*;�<�C�C�G�L�J�	M�#�+�#X� (�3;�Lg�*h�5=�Nr�,s�7?�Pm�.n�'�73&2�>�$B�#��
�74�75�(
�"@���O�\j�[k� l�m�76�� ���3�3����4�77�78��	�*�+�
�h���q�!�)�)�1�1�2����A��&�&�1�1�%�-�-�a�0�8�8�C�C�
>�	��%�%�*�*�o�=�#9�#-�%*�'5�$�L�79�2�3��$�*�*�\�!�<�=�
>���_	M�	M��680��,��
�)�#�a�&��2�3����sZ�G0�9G�3F4�;AG�G�BG�7<G�3G0�4F>�9	G�	G-�G(�#G0�(G-�-G0�81input_data�max_retries�
initial_delayc��(����K�td�d,����fd�	�	ddddddd	�d82dd	�d�d
gd�d�d�g}td��g}�||��d{���}td�t|jdjj�|jdjj83ry|jdjj84D]S}|jjdk(s�t|��}td�ttj|d����Utjd��d{���ddddd85dd	�dddd�ddd�d86dd�d �d!�d"�d#gd�d�d�g}	d$}87td%|88����g}�||	��d{���}td&�t|jdjj�|jdjj89ru|jdjj90D]O}|jjdk(s�t|91�'�}
td(|
���t|
�}td)|����Q||fS7��7��#7��#t$r!}td*t!|����Yd}~y+d}~wwxYw�w)-Nz&Starting complete analysis workflow...rc92��f�K�	�jjt||���d{���}|S7�#t$rr}dt	|�vr^|�	krY�d|zz}td|�d|dz�d�	�d��t
j|��d{���7�|||dz��d{���7cYd}~S|�d}~wwxYw�w)	Nr��429r�zRate limit hit, retrying in z seconds... (Attempt r�r'�))r�r�r\r9rr:�asyncio�sleep)93r�rW�retry_countr<r>�delayr@r��
make_api_callr�s94      ����r"r�z1complete_analysis_workflow.<locals>.make_api_callqs������
	�#�[�[�7�7��!��8���H�95�O����	���A���;��#<�%��k�)9�:���4�U�G�;P�Q\�_`�Q`�Pa�ab�cn�bo�op�q�r��m�m�E�*�*�*�*�8�U�K�!�O�L�L�L�L��G��
	�s[�B1�%3�1�3�B1�3�	B.�A96B)�B	�B)�B�B)�!B.�"B1�'B)�)B.�.B1rCrnrorFrprIrGzType of analysis to performrqrrrirPrSzBAnalyze the provided JSON data and create a comprehensive analysisr�zJSON Analysis response:)rrzAnalysis result:r�r�g@rwrxryrzr{r|r~rrNr�r�zkClimate analysis reveals significant warming trends with regional variations requiring immediate attention.z!Convert this analysis to speech: zSpeech conversion response:�r��Generated audio URL: �Audio play result: z%Error in complete analysis workflow: )NN)r)r:rr�r�r8r�rCrQr�r�r�r�r�r�r?r9r)r@r�r�r��json_analysis_toolr��
json_responser��analysis_result�speech_tool�
analysis_text�speech_messages�speech_response�	audio_url�play_resultr>r�s` ``            @r"�complete_analysis_workflowr�ns������	�972�3��� ^�#�/�#c� (�2:�Ka�)b�6>�Ol�-m�'�&1�M�
#��
�98��(
� d�e�99��,�H�6H�I�I�
�
�'�(�
�m�#�#�A�&�.�.�6�6�7�� � ��#�+�+�6�6�*�2�2�1�5�=�=�H�H�
A�	��%�%�*�*�.A�A�&@�:�&V�O��,�-��$�*�*�_�Q�?�@�	
A��m�m�C� � � �100#�,�#A� (�-5�Fa�$b�(0�6>�3�-O�6>�3�-O�;C�PY�2Z�/"�/�101'�&,�H�#��
�102��4F�
�
�"C�M�?� S�T�103��!.�o�{� K�K��
�+�,�
�o�%�%�a�(�0�0�8�8�9��"�"�1�%�-�-�8�8�,�4�4�Q�7�?�?�J�J�
?�	��%�%�*�*�.>�>� 7�]� K�I��1�)��=�>�#+�9�"5�K��/��}�=�>�
?��o�-�-�CJ��	!��FL��$��
�5�c�!�f�X�>�?����st�J�1I%�
I�BI%�,AI%�=I �>AI%�I#�BI%�$8I%�J�I%� I%�#I%�%	J�.J104�J�105J�Jc���K�	ddddddd�ddd	d106�dd	d107�ddd108�d�d
�d�dgd�d�d�g}td�td|�d���g}|jjt||���d{���}td�t|j109djj�|j110djjru|j111djjD]O}|jjdk(s�t|��}td|���t|�}td|����Q|S7��#t$r!}tdt|����Yd}~yd}~wwxYw�w)NrCrwrxrFrGryrIrzr{r|r~rrNr�r�rirPrS�Running TTS workflow...zSay 'z' out loud!r�r�zTTS Agent response:rr�r�r��Error in TTS workflow: )r:rr�r�r\r�r�r8r�rCrQr�r?r9r)	r@r��tts_toolr�r<r�r�r�r>s	         r"�tts_with_mcpr��s�����5�#�,�#A� (�-5�Fa�$b�(0�6>�3�-O�6>�3�-O�;C�PY�2Z�/"�/�112'�&,�H�#��
�113��2	�'�(��%��v�[� 9�:�114�� ���3�3����4�115�116��	�#�$�
�h���q�!�)�)�1�1�2����A��&�&�1�1�%�-�-�a�0�8�8�C�C�
?�	��%�%�*�*�.>�>� 7�T� B�I��1�)��=�>�"*�9�"5�K��/��}�=�>�
?���#117��&��
�'��A��x�0�1����sH�E/�A&E�*E�+BE�	6E�?E/�E�	E,�E'�"E/�'E,�,E/c��K�td�	d}d}t|d��}|j|�td|���tdtj118j
|����}td|���t||��d{���}|rLtd	�|jd119jjj�D]
}t|��|S7�T#t$r!}tdt|����Yd}~yd}~wwxYw�w)Nr��&hello, and good luck for the hackathonz/tmp/output.wavr�r�zAudio saved to r&r�zAll run entries:rr�)r:rr�r?r�r0r�r�r�r�r8�120splitlinesr9r)r@r�r$r�r��121run_result�entryr>s        r"�mainrs�����	�122#�$��7��%�	���4�(�������
��	�{�+�,���������)C�(D�E�F��
�#�K�=�1�2�(���5�5�123���$�%�#�+�+�A�.�6�6�>�>�I�I�K�
���e��
���6����
�'��A��x�0�1����sB�D�A8C�C�AC�D�C�	D�'D�>D�D�Dc��HK�td�t|�}t|�}t|�}t	|�}td�td|j124���td|j125���td|j126���td|j127���td�td�td	�td�td128�gd�gd�gd
�d�}t
||��d{���\}}td�|r8td�t|jdjj�ntd�|r8td�t|jdjj�ntd�td�y7���w)Nz:Mistral Multi-Agent Document Processing System InitializedzAvailable agents:z- DocAgent ID: z- ImageAgent ID: z- JsonAnalyzerAgent ID: z- SpeechAgent ID: z2--------------------------------------------------zLSkipping hardcoded document and image processing workflows in main_workflow.z5Use the Gradio interface to upload and process files.z(3. Running complete analysis workflow...)g�����4@g�4@g3333335@g������5@)i�i�i�i�)�Globalr��Tropical)�temperature_data�	emissions�regionszAnalysis Response:rzNo analysis response receivedzSpeech Response:zNo speech response receivedzAll workflows completed!)129r:r^rkrtr��idr�r�r�r8)r@�	doc_agent�image_agent�json_analyzer_agent�speech_agent�sample_data�analysis_responser�s        r"�
main_workflowr6sk����	�130F�G� ��(�I�$�V�,�K�4�V�<��&�v�.�L�	�131��	�O�I�L�L�>�132*�+�	��k�n�n�-�133.�/�	�$�%8�%;�%;�$<�134=�>�	��|���/�1350�1�	�(�O�136�137X�Y�	�138A�B�	�(�O�139�1404�5�4�)�3��K�1410J�&�R]�/^�)^�&���	�(�O��
�"�#�
��'�'��*�2�2�:�:�;�
�-�.��
� �!�
�o�%�%�a�(�0�0�8�8�9�
�+�,�	�142$�%�*_�s�C5F"�7F �8B)F"c��K�t|��d{���td�td�t|��d{���y7�.7��w)Nz3143==================================================r�)rr:rr]s r"�full_runras=����144��145���	�-��	�146#�$�147�v�,��� ���s�A�A�(A�A�A�A�__main__�YOUR_API_KEY)�api_key)z/tmp/audio.wav)NN�report)�text_extractionr�)N)�climate_report)�g@)r)8r�r�r�r3�typingrrrr�pathlibrr��mistralai.extra.mcp.sserr	�	mistralair148�mistralai.modelsrrr
�pydanticr�IPython.displayrr�platform�149subprocess�urllib.parser-�gttsrrr\rr?r^rkrtr�r�r�r�r�r�r��intrr�r�rrrrr@�runr r!r"�<module>r*s.��	�
���-�-���A��G�G��*������)��	��"�#�"�#�"�" �W� �F!�w�!�H�w��>!��!�H��#���H�UX�M��qt��EI�JM�OR�JR�ES��&�s��#��s��ko�ps�ux�px�ky��&�$�s�C�x�.����]a�be�gj�bj�]k��*"�#�"�t�C��H�~�"�QT�"�/�G�/��/�TW�/�b<��<�c�<�SV�<�|q�W�q�$�s�C�x�.�q�_b�q�w|�q�f6�w�6�c�6�p�w��:)&��)&�V�7���z��
�^�
,�F��G�K�K��� �!�r!