CoolFace
Apppublic

antiperfect/refugee-movement-api

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
app.cpython-312.pyc78 linesDownload Raw Back to __pycache__
12�'�i�6��\�ddlmZmZmZmZddlmZddlZddlZ	ddl3Z4ddlZddlZ
ee�Zee�ej j#ej j%e��Zej*ej j-ed��Zej*ej j-ed��Zeej j-ed��5Ze5j*e�Zddd�ej j-edd�Ze	j:e�Zed	ed6zed<gd�ZiZ iZ!d%d�Z"d�Z#ejId�d��Z%ejIddg��d��Z&ejIddg��d��Z'ejIddg��d��Z(ejIddg��d��Z)ejIddg��d��Z*ejIddg��d ��Z+ed!k(re"d
�ejYd"d#�$�yy#1swY��+xYw)&�)�Flask�render_template�request�jsonify)�CORSNztime_model.pklzresource_model.pklzorigin_mapping.json�datazpersons_of_concern.csv�RefugeeszAsylum-seekers�Total_Refugees)�Afghanistan�7Bangladesh�China�Myanmar�Pakistanz	Sri Lanka�Nepal�Bhutan��c	��td|�d�d��tj�D]}t||�}|s�|t|<�ttdj��}td|��d��g}tjd�dj�j�}|jdd�	�|j�D]K\}}t|d�}d8|cxkr|ks� n�#|jt|�t|d�d���Mi}	tj�D�]J}9ttd|10k(jd�}|jr�3t|j d
d�}t|j d
d�}
tt#|�dkDr|j ddn|
�}t%j&|
�rd}
t%j&|�rd}t|11}|dz}|dks��t%j(|g|g|
g|gd��}tdtt*j-|�d��}||kDr|	j/|d�|z|	|<|
||dz}}
}|dkr�u��Mt1|	j3��D]+\}}|jt|�t|�d���-|t4d<tdd��y)zOPrecompute all predictions for a given year to avoid slow runtime calculations.zPrecomputing predictions for z...T)�flush�YearzCPrecomputing All Origins Series (2000-2030). Last historical year: r12r��inplace����x�y�Country of Origin��������������r�Origin_Encoded�Lag_1�Lag_2�allzPrecomputation complete.N)�print�mapping�keys�predict_for_country�PREDICTION_CACHE�int�
historical_df�max�groupby�sum�reset_index�fillna�iterrows�append�sort_values�empty�iloc�len�pd�isna�	DataFrame�13time_model�predict�get�sorted�items�SERIES_CACHE)�year�country�result�
MAX_HIST_YEAR�series�hist_agg�_�rowr�pred_map�c�c_df�last_y�l1�l2�enc�cy�input_df�p�vals                    �_C:\Users\lenovo\Documents\Coding Projects\Refugee Movement Project\prototypes\v1\backend\app.py�precompute_predictionsrU%s���	�)�$��s�143�4�@��<�<�>��$�W�d�3���(.��W�%�"��
�f�-�1�1�3�4�M�	�O�P]��15_�gk�l�
�F��$�$�V�,�-=�>�B�B�D�P�P�R�H��O�O�A�t�O�$��#�#�%���3���F�����1�%�
�%��M�M��A��S��5E�1F�-G�H�I�&��H�
�\�\�^���]�+>�?�1�D�E�Q�Q�RX�Y���:�:�x��T�Y�Y�r�]�6�*�+��
����2��/�0�
1��
�C��I��M����2��/�0�r�
J��
�7�7�2�;�Q��
�7�7�2�;�Q���a�j��
�a�Z���D�j��|�|�b�T�c�U�VX�UY�eg�dh�$i�j�H��A�s�:�-�-�h�7��:�;�<�A��M�!�'�|�|�B��2�Q�6�����Q��Q��B��B��D�j��2����)�*���3��
�
�C��F��S��2�3�+�!�L���	�16$�D�1�c
�T�|tvryt|}ttd|k(jd�}|jry||jddkrO||d|k(}|jst|djd�}n�t|jdd�}n�|jdd}t|�dkDr|jddn|}tj|�rd}tj|�rd}|jdddz}|}||kr\tj|g|g|g|gd	��}	ttj|	�d�}17td|18�}19|}|20}|21}|dz
}||kr�\d22}tj|g|g|g|gd��}tj|�}23|||tdt|24dd��tdt|25dd��tdt|26dd��tdt|27dd
��|tvd�S)z:Run both models for a single country+year and return dict.Nrrrr28rrrr!g�������?)rr"r29�Refugee_Growth��)rBrA�refugees�food�shelter�medical�water�is_neighbor)r'r,r4r5r6r+�valuesr7r8r9r:r;r<r-�resource_model�	NEIGHBORS)
rBrA�origin_encoded�30country_dfrHr[�lag_1�lag_2�current_yearrQ�pred�growth�31input_datas
             rTr)r)[sR���g����W�%�N��}�-@�A�W�L�M�Y�Y�Z`�a�J������z���r�"�6�*�*���F�+�t�3�4���y�y��3�/�0�7�7��:�;�H��:�?�?�2�.�/?�@�A�H�����#�$4�5��9<�Z��1�9L�32����#�$4�5�RW��
�7�7�5�>�1�5�
�7�7�5�>�1�5�!���r�*�6�2�Q�6�����d�"��|�|�%��#1�"2�����	%��H��z�)�)�(�3�A�6�7�D��q�$�<�D��E��E��H��A��L��d�"�"�F������)�*�#�*�!�(�	��J��!�!�*�-�D�����A�s�4��7�1�:��'��q�#�d�1�g�a�j�/�*��q�#�d�1�g�a�j�/�*��Q��D��G�A�J��(��)�+�	�	rV�/c� �tdddd��S)N�onlinez*Refugee Movement Prediction API is runningz1.0.0)�status�message�version)r�rVrT�homers�s����?����rVz/predict�POST)�methodsc33���ttjd�}tjd}t||�}|�yt	d|d|d|d|d|d	||�34�S)NrArB)�Country not found�zresult.htmlr[r\r]r^r_)r[r\r]r^r_rBrA)r+r�formr)r)rArBrCs   rTr<r<�sr���w�|�|�F�#�$�D��l�l�9�%�G�
 ��$�
/�F�
�~�'��=�$*�:�$6� &�v��#)�)�#4�#)�)�#4�!'���#*� $�&�&rVz/api/predict�GETc���tjjd�}tjjddt��}|r|tvrtddi�dfSt
||�}t|�S)zRSingle country prediction.35    GET /api/predict?country=Afghanistan&year=202636    rBrAr��type�errorzInvalid countryi�)r�argsr=r+r'rr))rBrArCs   rT�api_predictr��si��37�l�l���y�)�G��<�<���F�D�s��3�D��g�W�,���!2�3�4�c�9�9�
 ��$�
/�F��6�?�rVz/api/predict-allc�T�tjjddt��}|dk(r$trttj
��}n;g}tj�D]"}t||�}|s�|j|��$|jd�d��t|�S)z�Predict for ALL countries in the model.38    GET /api/predict-all?year=202639    Returns array of predictions sorted by refugee count descending.40    rArr|c��|dS)Nr[rr�rs rT�<lambda>z!api_predict_all.<locals>.<lambda>�s��q��}rVT)�key�reverse)
rrr=r+r*�listrar'r(r)r3�sortr)rA�resultsrBrCs    rT�api_predict_allr��s����<�<���F�D�s��3�D��t�|�(��'�.�.�0�1�����|�|�~�G�(��$�7�F�����v�&�&��L�L�,�d�L�;��7��rVz/api/countriesc���ttj��}|j�t	|t41D�cgc]
}|tvs�|��c}t
|�d��Scc}w)z'Return list of all available countries.)�	countries�	neighbors�total)r�r'r(r�rrcr7)r�rJs  rT�
api_countriesr��sQ���W�\�\�^�$�I�
�N�N����!*�;�A�a�7�l�a�;��Y������;s�
A!4243A!44z/api/seriesc	�^	�tjjd�}g}|�r"|tvrt	ddi�dfSttdj��}ttd|k(j�}|jdd�	�|j�D]K\}}t|d�}d45|cxkr|ks� n�#|jt|�t|d�d���Mttd|k(jd�}|j�stt|jd
d�}t|jd
d�}	tt|�dkDr|jddn|	�}46t!j"|	�rd}	t!j"|47�rd}48t|}|dz}|dk�r�t!j$|g|g|	g|49gd��}
tt&j)|
�d�}td|�}||kDr&|jt|�t|�d��|	|}	}50|dz
}|dkr���nJdt*vrt	t*d�Sttdj��}tj-d�dj/�j1�}|jdd�	�|j�D]K\}}t|d�}d51|cxkr|ks� n�#|jt|�t|d�d���Mi}tj3�D�]}ttd|k(jd�}|jr�3t|jd
d�}t|jd
d�}tt|�dkDr|jddn|�}t|}|dz}|dks��t!j$|g|g|g|gd��}
tdtt&j)|
�d��}||kDr|j|d�|z||<|||dz}}}|dkr�u��t5|j7��D]\}}|j||d���|j9d���t	|�S)z�Return combined historical (2000-MAX_HIST_YEAR) and predicted (MAX_HIST_YEAR+1 - 2030) series.52    GET /api/series?country=Afghanistan53    If no country provided, returns sum of all countries.54    rBr~rwrxrrrTrrr55rrrrr r!r%c��|dS)Nrrrr�s rTr�zapi_series.<locals>.<lambda>Ks��a��frV)r�)rrr=r'rr+r,r-�copyr1r2r3r4r5r6r7r8r9r:r;r<r@r.r/r0r(r>r?r�)rBrErD�histrGrHrrerLrfrgrd�curr_yrQrirFrIrJrKrMrNrOrPrRrSs                         rT�56api_seriesr��sO���l�l���y�)�G�
�F���'�!��G�%8�9�:�C�?�?��M�&�1�5�5�7�8�
��]�+>�?�7�J�K�P�P�R�����A�t��$��m�m�o�F�A�s��C��K� �A��q�)�M�)��
�
�C��F��S�9I�5J�1K�L�M�&�#�=�1D�#E��#P�Q�]�]�^d�e�57���������,�V�4�5�F��58����+�,<�=�>�E���Z��ST�AT�59����+�,<�=�Z_�`�E��w�w�u�~�q�u��w�w�u�~�q�u�$�W�-�N��a�Z�F��D�.��<�<�#�H�'5�&6�#�W�#�W�	)����:�-�-�h�7��:�;���1�d�|���M�)��M�M��F��#�d�)�"D�E�$�d�u���!����D�.�"�L� ��<��.�/�/��M�&�1�5�5�7�8�
� �(�(��0�1A�B�F�F�H�T�T�V������4��(��'�'�)�F�A�s��C��K� �A��q�)�M�)��
�
�C��F��S�9I�5J�1K�L�M�*�������A� ��/B�!C�q�!H�I�U�U�V\�]�D��z�z�8�����2��v�.�/�F��T�Y�Y�r�]�#3�4�5�B���D�	�A�
�T�Y�Y�r�]�#3�4�2�N�B��!�*�C��!��B���*��<�<�"��#��Z\�Y]�ik�hl�(m�n����3�z�1�1�(�;�A�>�?�@���
�%�#+�<�<��A�#6��#:�H�R�L���B��F��B����*� �"�X�^�^�-�.�F�A�s��M�M���,�-�/��K�K�$�K�%��6�?�rVz	/api/newsc60���	d}dddddd�}tj||d��}|jd	k(r�|j�}d61|vr�t	|d62�dkDrsg}|d63ddD]X}|j|jd
d�|jdd�|jdd�|jdd�d���Zt
|dd��S	ddlm	cm64}d}tj|d��}|jd	k(r�|j|j�}g}|jd�ddD]u}	|	jd
�}65|	jd�}|	jd�}|j|66�|67jnd|�|jnd|�|jnddd���w|rt
|dd��St
gd d!d"��S#t$rY��wxYw#t$rY�*wxYw)#uIFetch humanitarian news — tries GDELT first, then UN News RSS fallback.z+https://api.gdeltproject.org/api/v2/doc/docz'refugee displacement India humanitarian�ArtList�json��DateDesc)�query�mode�format�68maxrecordsr�)�params�timeout���articlesrN�title�Untitled�url�#�seendate��domain�News)r�r�r�r��GDELT)r��sourcezShttps://news.un.org/feed/subscribe/en/news/topic/migrants-and-refugees/feed/rss.xml�69)r�z.//item�link�pubDatezUN NewszUN News RSS�nonezAll news sources unavailable)r�r�r~)�
http_requestsr=�status_coder�r7r3r�	Exception�xml.etree.ElementTree�etree�ElementTree�70fromstring�content�findall�find�text)
�	gdelt_urlr��resprr��art�ET�rss_url�root�itemr�r��pubdates
             rT�api_newsr�Os���
�A�	�>�����71��� � ��6�2�F�����s�"��9�9�;�D��T�!�c�$�z�*:�&;�a�&?����72�+�C�R�0�C��O�O�!$����*�!=�"�w�w�u�c�2�$'�G�G�J��$;�"%�'�'�(�F�";�	%��1��H��H�I�I�73
�*�*�g��� � ��"�5�����s�"��=�=����.�D��H����Y�/���4���	�	�'�*���y�y��(���)�)�I�.�����+0�+<�U�Z�Z�*�(,�(8�4�9�9�c�07�0C�����'�	!��	5���H�
�N�O�O���f�?]�^�_�_��5�
��
��.�
��
�s%�CG�C)G�	G�G�	G!� G!�__main__Fi�)�debug�port)r)-�flaskrrrr�74flask_corsr�joblib�pandasr8r��os�requestsr��__name__�app�path�dirname�abspath�__file__�BASE_DIR�load�joinr;rb�open�fr'�	DATA_PATH�read_csvr,rcr*r@rUr)�routersr<r�r�r�r�r��runrrrVrT�<module>r�sQ��:�:��
���	� ��H�o���S�	��7�7�?�?�2�7�7�?�?�8�4�5��
�V�[�[������h�0@�A�
B�75�����R�W�W�\�\�(�4H�I�J��76�"�'�'�,�,�x�!6�777�8��A��d�i�i��l�G��
�G�G�L�L��6�+C�D�	�����I�&�
�"/�78�";�m�L\�>]�"]�
���
�	�����42�lE�T���3��������:��x��(�&�)�&�(���>�E�7��+��,�������w��/��0��,����e�W��-��.�����=�5�'��*�W�+�W�t���;����(�3`�)�3`�n�z���4� ��G�G�%�d�G�#��i��s�%H!�!H+