CoolFace
Apppublic

NifemiAluko/linkedin-post-analyzer

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
helpers.cpython-313.pyc247 linesDownload Raw Back to __pycache__
1�

2F�hvY��R�SSKrSSKrSSKrSSKJr SSKrSSKJr SSK	J3r4 SSKJr SSK
Jr SSKrSSKJr SSKJr SS	KJr SS5KJrJr SSK	Jr SSKJr SS
KJr \R8"S5 \"5 SrSrSrSr Sr!Sr"Sr#Sr$Sr%Sr&Sr'Sr(Sr)Sr*Sr+Sr,g)�N)�SentimentIntensityAnalyzer)�OpenAI)�PromptTemplate)�RunnableSequence)�load_dotenv)�datetime)�Tool)�6ChatOpenAI)�create_openai_tools_agent�
AgentExecutor)�ChatPromptTemplate)�create_pandas_dataframe_agent)�MessagesPlaceholder�
vader_lexiconc�P�[R"U5(ag[U5$)z1Helper function to ensure we're working with text�7No content)�pd�isna�str��texts �W/Users/nifemialuko/Documents/Applications/LI_Analyzer/linkedin_post_analyzer/helpers.py�safe_text_analysisrs��	�w�w�t�}�}���t�9��c�P�[R"[U55$! g=f�Nr)�textstat�flesch_reading_easerrs r�calculate_reading_easers&����+�+�C��I�6�6����s�!�%c�V�[[U5R55$! g=fr)�lenr�splitrs r�8word_countr#%s'����3�t�9�?�?�$�%�%����s�!$�(c���[U5nUS:Xag[[R"S5S9n[	S/SS9nX!-nURSU05R
5nU$![a9nSnAgSnAff=f)	NrzNo tone�OPENAI_API_KEY��api_key�postau10            Analyze this LinkedIn post and determine its primary tone.11            Choose exactly one tone from this list:12            - Formal (professional, structured, traditional)13            - Neutral (balanced, objective, straightforward)14            - Informal (casual, conversational, relaxed)15            - Optimistic (positive, hopeful, upbeat)16            - Worried (concerned, cautious, uncertain)17            - Friendly (warm, approachable, personable)18            - Curious (inquisitive, questioning, exploring)19            - Assertive (confident, direct, strong)20            - Encouraging (supportive, motivational, inspiring)21            - Surprised (amazed, unexpected, intrigued)22            - Cooperative (collaborative, inclusive, team-oriented)23 24            Post: {post}25            26            Return only the tone name (single word), nothing else.27            ��input_variables�templatezTone Analysis Failed�rr�os�getenvr�invoke�strip�	Exception)r�llm�prompt�chain�tone�es      r�writing_toner7+s����d�#�D��|���"&��R�Y�Y�'7�8�9�� �#�H��28��0����|�|�V�T�N�+�1�1�3������&�%��&���AA#�#29A7�2A7c��[U5nUS:Xag[R"SU5(agg![a30nSnAgSnAff=f)Nrz
No formattingz(\* |- |\d+\.)\s+zBullet points presentzNo bullet pointszFormatting Analysis Failed)r�re�searchr1)rr6s  r�formatting_analysisr<SsG���d�#�D��|���,�
�9�9�)�4�0�0�*�!���,�+��,�s�2�31A�Ac���[U5nUS:Xag[[R"S5S9n[	S/SS9nX!-nURSU05R
5nU$![a32nSnAgSnAff=f)	Nrz	No intentr%r&r(a!33            Analyze this LinkedIn post and determine its primary intent.34            Choose exactly one intent from this list:35            - Inform (share knowledge, updates, or news)36            - Convince (persuade, influence, or call to action)37            - Describe (explain details, features, or processes)38            - Tell a story (share personal experiences or narratives)39 40            Post: {post}41            42            Return only the intent category name (Inform, Convince, Describe, or Tell a story), nothing else.43            r)zIntent Analysis Failedr,)rr2r3r4�intentr6s      r�analyze_intentr?_s����d�#�D��|���(��R�Y�Y�'7�8�9�� �#�H��44��"������v�t�n�-�3�3�5���
���(�'��(�r8c��^�[R"U5(ag[U5R5mTS;aTR	5$ST;a)ST;ag[U4SjS55(agS	T;ag45g)z5Determine media type from either direct string or URL�None)�image�video�carousel�none�httprB�Imagec3�,># �UH	oT;v� M g7f�N�)�.0�vid_term�	media_strs  �r�	<genexpr>�'determine_media_type.<locals>.<genexpr>�s����O�7N�8�Y�&�7N�s�)�vid�mp4rC�VideorD�Carousel)rrr�lower�46capitalize�any)�
media_contentrMs @r�determine_media_typerX�sz���	�w�w�}�����M�"�(�(�*�I��:�:��#�#�%�%�����i���
�O�7N�O�
O�
O��
�9�
$��rc�j�USR[5US'USR[5US'USR[5US'USR[5US'USR[475US'SUR;aUSR[5US'U$)	N�	Full Post�Flesch Reading Ease�48Word Count�Writing Tone�49Formatting�Intent�Media�Media Creative)�applyrr#r7r<r?�columnsrX)�dfs r�process_dataframere�s��� "�;�� 5� 5�6L� M�B����+��,�,�Z�8�B�|���K��.�.�|�<�B�~���+��,�,�-@�A�B�|���k�?�(�(��8�B�x�L��"�*�*��!�'�{�0�0�1E�F����
�Irc���[S5 [SURR55 [SUR55 [	U5nUSRS5R
5nUSRS5R
5nUSRS5R
5nUSRS5R
5nUS	RS5R
5nSn0nS50UR;a�US51R5nURH�n	XS52U	:Hn53[	U545U55SRS5R
5U56SRS5R
5U57SRS5R
5SUR;a"U58SRS5R
5OSS.X�'M� SUR;a"USRS5R
5OSnXSUSRS
5:�R5nXSUSRS5:R5n
SUSUSSUSSUS3nUS:�a59USUSS3-
nUSUSSUS3-
nUb�US-
nUS-
nUR5Hup�USU	SUSX�-S-SS3-
nM US-
nUR5HQun	nUS U	S!US"S#3-
nUSUS$S3-
nUSUS%S3-
nUSUS&S3-
nUS'S:�dMDUSUS'SS3-
nMS US(USRS
5S)S*USRS+5S)S,3-
nSUR;a2US-USRS
5SS.USRS+5SS3-
nS/UR;a�US/RS05R5RS15nUS2-
nUS3-
nUR5HunnUSUSUS43-
nM [	U5S:�a\US/RS05R5RS15nUS5-
nUR5HunnUSUSUS43-
nM S6UR;a\US6RS05R5RS15nUS7-
nUR5HunnUSUSUS43-
nM S8UR;a\US8RS05R5RS15nUS9-
nUR5HunnUSUSUS43-
nM US:[	XS	S;:�5S<[	XS	S=:�US	S;:*-5S>[	XS	S=:5S?[	XSS:5S@[	XSS:�USSA:*-5SB[	XSSA:�5S43
-
nUSC-
nUSD[USR
55SE3-
nUSF[US	R
553-
nS/UR;aRUS/RS05R5RS15nUSGSHRUR53-
nS6UR;aRUS6RS05R5RSI5nUSJSHRUR53-
nSUR;aUSKUSR
5SSL3-
nU$![a/n[SM[U535 SN[U53sSnA$SnAff=f)ONz"Starting generate_summary functionzDataFrame columns:zDataFrame head:�	Reactionsr�Comment�Repostsr\r[ra�Engagement_rate)�count�
avg_reactions�avg_comments�avg_reposts�avg_engagement��?g�?zP# LinkedIn Post Analysis Summary60 61## Overall Statistics62- Total Posts Analyzed: z63- Average Reactions: z.1fz64- Average Comments: z65- Average Reposts: z66- Average Engagement Rate: �%z67- Average Word Count: z% words68- Average Reading Ease Score: z69 70## Media Creative Analysisz71### Distribution by Media Typez72- �: z posts (�dz%)z73 74### Performance by Media Typez75 76z Posts (rkz posts):rlrmrnrozK77 78## Engagement Analysis79### High Engagement Thresholds80- 75th Percentile: z.0fz reactions81- 90th Percentile: g�������?z82 reactionsz$83- 75th Percentile Engagement Rate: z%%84- 90th Percentile Engagement Rate: �Topic�Unknown�z85 86## Content Analysisz87### Most Common Topics� postsz)88 89### Top Topics in High-Performing Postsr]z90 91### Writing Tone Distributionr_z92 93### Content IntentzF94 95## Readability Analysis96- Posts with High Readability (Score > 60): �<z6 posts97- Posts with Medium Readability (Score 40-60): �(z2 posts98- Posts with Low Readability (Score < 40): z7 posts99 100## Length Analysis101- Short Posts (<100 words): z' posts102- Medium Posts (100-300 words): i,z" posts103- Long Posts (>300 words): z104 105## Recommendationsz1061. Content Length: Aim for z( words (average of top performing posts)z71072. Readability: Target a Flesch reading ease score of z$1083. Most Effective Topics: Focus on z, �z$1094. Recommended Tone: Primarily use z$1105. Engagement Rate Target: Aim for z% (average of top performers)zError in generate_summary: zError generating summary: )�printrc�tolist�headr!�fillna�mean�value_counts�index�quantile�copy�items�int�joinr1r)rd�total_postsrlrmrn�avg_word_count�avg_reading_ease�media_distribution�media_engagement�111media_type�media_posts�avg_engagement_rate�high_performing�low_performing�summaryrk�metrics�112top_topics�topic�high_perf_topics�	top_tonesr5�top_intentsr>�high_perf_tonesr6s                          r�generate_summaryr��s	��P5�
�2�3�
�"�B�J�J�$5�$5�$7�8�
������+��"�g���;��.�.�q�1�6�6�8�
��)�}�+�+�A�.�3�3�5����m�*�*�1�-�2�2�4���L�)�0�0��3�8�8�:���3�4�;�;�A�>�C�C�E��"�����r�z�z�)�!#�$4�!5�!B�!B�!D��0�6�6�113� �$4�!5��!C�D�� ��-�%0��%=�%D�%D�Q�%G�%L�%L�%N�$/�	�$:�$A�$A�!�$D�$I�$I�$K�#.�y�#9�#@�#@��#C�#H�#H�#J�Yj�np�nx�nx�Yx�k�2C�&D�&K�&K�A�&N�&S�&S�&U�~�0� �,�7�IZ�]_�]g�]g�Hg�b�!2�3�:�:�1�=�B�B�D�mn����_�r�+��/G�/G��/M�M�N�S�S�U���{�O�b��o�.F�.F�t�.L�L�M�R�R�T���%�
�&�#�C�(�)�!�#�&�'���$�
(����"���/��4�A�9�
9�G�	��%�c�*�+�/��4�8�	8��114�)��7�7�G��9�9�G�%7�%=�%=�%?�!�115��T�*��R��w�h��@Q�RU�@U�WZ�>[�[]�^�^��&@�
�:�:�G�'7�'=�'=�'?�#�116�G��T�*��X�g�g�6F�5G�x�P�P���4�W�_�5M�c�4R�S�S���3�G�N�4K�C�3P�Q�Q���2�7�=�3I�#�2N�O�O���+�,�q�0��!>�w�GW�?X�Y\�>]�]^�_�_�G�
(@�	���{�O�,�,�T�2�3�7�8��{�O�,�,�T�2�3�7�z�E�	E����117�118�*��$�$&�'8�$9�$B�$B�4�$H��#M�N$�$&�'8�$9�$B�$B�4�$H��#M�Q�R�
R�G�119�b�j�j� ��G��+�+�I�6�C�C�E�J�J�1�M�J��0�0�G��1�1�G� *� 0� 0� 2���u��T�%���5�'��8�8��!3��?�#�a�'�#2�7�#;�#B�#B�9�#M�#Z�#Z�#\�#a�#a�bc�#d� ��H�H��$4�$:�$:�$<�L�E�5���e�W�B�u�g�V�<�<�G�%=��R�Z�Z�'��>�*�1�1�)�<�I�I�K�P�P�QR�S�I��:�:�G�(���0���e��T�$��r�%���7�7�� 1��r�z�z�!��X�,�-�-�i�8�E�E�G�L�L�Q�O�K��/�/�G�!,�!2�!2�!4�
����T�&���E�7�&�9�9��"5�	�-�.1��7L�4M�PR�4R�1S�-T�,U�V0�03�B�;P�8Q�UW�8W�\^�_t�\u�y{�\{�7|�4}�0~�/�@,�,/��6K�3L�r�3Q�0R�,S�+T�U�!��|�$4�s�$:�!;�<�=�>!�!$�R�L�)9�S�)@�R��EU�Y\�E\�(]�%^�!_� `�a���l�#3�c�#9� :�;�<�F�120F�121	F��	�+�+���2�3��|�7T�7Y�7Y�7[�3\�2]�^F�G�	G���M�c�Ra�bw�Rx�R}�R}�R�OA�NB�C�	C���b�j�j� �.�w�7�>�>�y�I�V�V�X�]�]�^_�`���>�t�y�y�IY�I_�I_�?`�>a�b�b�G��R�Z�Z�'�-�n�=�D�D�Y�O�\�\�^�c�c�de�f�O��>�t�y�y��I^�I^�?_�>`�a�a�G���122�123�*��>��O`�?a�?f�?f�?h�il�>m�nK�L�
L�G�����5�
�+�C��F�8�4�5�+�C��F�8�4�4��5�s%�L-\7�3P\7�7124]0�$]+�%]0�+]0c��SRUR5VVs/sHupSUSUS3PM snn5$s snnf)z9Helper function to format value counts into markdown list�125z- rrrw)r�r�)�seriesr��values   r�format_listr�6s:���9�9�f�l�l�n�U�n�l�e��5�'��E�7�&�1�n�U�V�V��Us�<126c�`�[[R"S5S9n[SS/SS9nX-nU$)Nr%r&�question�dataz�127        Based on the following data: {data}128        129        Please answer this question: {question}130        131        Provide a clear and concise answer based only on the data provided.132        r))rr-r.r)r2r3r4s   r�setup_langchainr�:s;��133����#3�4�1345�C�
�#�V�,��	�F�
�L�E��Lrc�(�URXS.5$)N)r�r�)r/)r4r�r�s   r�	run_chainr�Ms���<�<�X�<�=�=rc��URRS5(a [R"URSS9nObURRS5(d URRS5(a![R"UR5nOgUSR5n[
U5nU(a�[R"U5n[R"X/SS	9nUSR[5US135'USR[5US'[R"5RS5nS
US3nSUS3nURUSS9 [!U5n[#US5n	U	R%U5 SSS5 SXg4$g!,(df   N=f![&an136S[)U13753SS4sSn138A139$Sn140A141ff=f)Nz.csvzlatin-1)�encodingz.xlsxz.xls)z;Unsupported file format. Please upload a CSV or Excel file.NNrZ�)�axisr[r\z
%Y%m%d_%H%M%S�analyzed_posts_�analysis_summary_z.mdF)r��wzAnalysis complete!zAn error occurred: )�name�endswithr�read_csv�142read_excelr|�batch_analyze_posts�	DataFrame�concatrbrr#r�now�strftime�to_excelr��open�writer1r)�filerd�posts�analysis_results�143results_df�	timestamp�output_excel_path�output_markdown_pathr��fr6s           r�analyze_filer�Ps���"<��9�9���f�%�%����T�Y�Y��;�B�
�Y�Y�
�
��
(�
(�D�I�I�,>�,>�v�,F�,F����t�y�y�)�B�^��;��&�&�(��.�u�5������&6�7�J����B�+�!�4�B�(*�;��(=�(=�>T�(U�B�$�%�!�+��4�4�Z�@�B�|�� ����/�/��@�I�"1�)��E� B��%6�y�k��#E� ��K�K�)��K�7�&�r�*�G��*�C�0�A����� �1�)�*;�R�R�)�"1�0��144�<�%�c�!�f�X�.��d�;�;��<�s=�B!F2�$CF2�F!�F2�!145F/�+F2�2146G�<G�G�Gc	��[[R"S5S9n[S/SS9n/nUGHnX!-nUR	SU05R5nUR
S5nUSRSS	5R5nUS147RSS	5R5n	USRS
S	5R5n148USRSS	5R5nUSRSS	5R5nURUU	U149USU3S.5 GM U$![a"n
URSSSSS.5 Sn
A
GM3Sn
A
ff=f![a150n
Sn
A
gSn
A
ff=f)Nr%r&r(a�151            Analyze this LinkedIn post and provide five pieces of information in exactly this format:152            TOPIC: [Choose from: 153            - Career Development154            - Career Development: Communications155            - Career Development: Networking156            - Career Development: Branding157            - Career Development: Writing158            - Career Development: Leadership159            - Industry Insights: AI160            - Industry Insights: Industrial Automation161            - Industry Insights: Marketing162            - Company News163            - Professional Tips: Communications164            - Professional Tips: Networking165            - Professional Tips: Branding166            - Professional Tips: Writing167            - Professional Tips: Marketing168            - Professional Tips: Leadership169            - Professional Tips: Personal growth170            - Personal Achievement171            - Event/Conference172            - Product/Service173            - Thought Leadership174            - Education/Learning: AI Fundamentals175            - Education/Learning: Machine Learning Basics176            - Education/Learning: Deep Learning177            - Education/Learning: Natural Language Processing178            - Education/Learning: Computer Vision179            - Education/Learning: AI Ethics and Safety180            - Education/Learning: AI Tools and Platforms181            - Job Opportunity182            - Market Trends183            - Team/Culture]184            TONE: [Choose from: Formal, Neutral, Informal, Optimistic, Worried, Friendly, Curious, Assertive, Encouraging, Surprised, Cooperative]185            INTENT: [Choose from: Inform, Convince, Describe, Tell a story]186            STRUCTURE: [Choose from: AIDA (Attention-Interest-Desire-Action), PAS (Problem-Agitation-Solution), Inverted Pyramid (most important first), No Clear Structure]187            BULLETS: [Choose from: Has Bullets, No Bullets]188 189            Post: {post}190            191            Return ONLY these five lines with your choices, nothing else.192            r)r�rzTOPIC:�r�zTONE:rzzINTENT:rvz193STRUCTURE:�zBULLETS:z | )rtr]r_r^zAnalysis Failed)194rr-r.rr/r0r"�replace�appendr1)r�r2r3�resultsr(r4�response�linesr�r5r>�	structure�bulletsr6s              rr�r�us���P��R�Y�Y�'7�8�9���#�H�*�-195��^���D�
���� �<�<����7�=�=�?�� ���t�,���a��(�(��2�6�<�<�>���Q�x�'�'���4�:�:�<���q��)�)�)�R�8�>�>�@��!�!�H�,�,�\�2�>�D�D�F�	���(�*�*�:�r�:�@�@�B�����"�$(�$�%.�K�s�7�)�"<�	 ���6����
����.�$5�/�"3�	 ����
�������s;�0E)�DD:�3E)�:196E&�E!�E)�!E&�&E)�)197E=�8E=c198��[SSS9n[UUSSSSSS9nU$![a!n[S[	U535 SnAgSnAff=f)	zTSet up a LangChain agent that can perform calculations and analysis on the DataFrame�gpt-4皙�����?��199model_name�temperatureTa�You are an expert data analyst specializing in LinkedIn post analytics.200            You have access to a DataFrame with LinkedIn post data and metrics.201            You can perform calculations, statistical analysis, and answer questions about the data.202            Analyze the data carefully and provide detailed insights backed by specific metrics.203            204            The DataFrame includes the following columns:205            - Full Post: The complete text of the LinkedIn post206            - Reactions: Number of reactions (likes etc.) received207            - Comment: Number of comments received208            - Reposts: Number of reposts/shares209            - Word Count: Number of words in the post210            - Flesch Reading Ease: Readability score211            - Topic: The main topic category of the post212            - Writing Tone: The tone used in the post213            - Intent: The purpose of the post (Inform, Convince, etc.)214            - Formatting: Structure and formatting characteristics215            )�verbose�allow_dangerous_code�handle_parsing_errors�include_df_in_prompt�prefixz"Error setting up LangChain agent: N)r216rr1r{r)rdr2�agentr6s    r�setup_langchain_agentr��sh��"����217��.����!%�"&�!%��218��2�����
�2�3�q�6�(�;�<����s��219A�A�Ac�@^�[SSS9n[SU4SjSS9[SU4S	jS220S9[SU4SjS
S9/n[R"S[	SS9SS/5n[XU5n[
XBSS9nU$![a!n[S[U535 SnAgSnAff=f)zYSet up a safe LangChain agent that can perform calculations and analysis on the DataFramer�r�r��analyze_column_statsc�n>�UTR;a!TUR5R5$SS0$)N�errorzColumn not found)rc�describe�to_dict)�colrds �r�<lambda>�+setup_safe_analysis_agent.<locals>.<lambda>�s5�����221�222�AR��C��!1�!1�!3�!;�!;�!=�!u�Y`�bt�Xu�!urzEGet statistical summary of a column. Input should be the column name.)r��func�description�get_high_performingc�\>�TTSTSRS5:�RS5$)Nrgrp�records)r�r�)�argsrds �rr�r�s/���2�b��o��;��8P�8P�QU�8V�&V�#W�#_�#_�`i�#jrz5Get high performing posts (top 25%). No input needed.�get_correlationc�>�UTR;a0UTR;a [TURTU55$S$rI)rc�float�corr)�col1�col2rds  �rr�r�sA���$�RT�R\�R\�J\�ae�ik�is�is�as��b��h�m�m�B�t�H�.E�(F�(}�y}�(}rzLCalculate correlation between two columns. Input should be two column names.)�systema�You are an expert data analyst specializing in LinkedIn post analytics.223            You have access to a DataFrame with LinkedIn post data and metrics.224            You can perform calculations, statistical analysis, and answer questions about the data.225            Analyze the data carefully and provide detailed insights backed by specific metrics.226            227            The DataFrame includes the following columns:228            - Full Post: The complete text of the LinkedIn post229            - Reactions: Number of reactions (likes etc.) received230            - Comment: Number of comments received231            - Reposts: Number of reposts/shares232            - Word Count: Number of words in the post233            - Flesch Reading Ease: Readability score234            - Topic: The main topic category of the post235            - Writing Tone: The tone used in the post236            - Intent: The purpose of the post (Inform, Convince, etc.)237            - Formatting: Structure and formatting characteristics�chat_history)�
variable_name)�humanz{input})�	assistantz{agent_scratchpad}T)r��toolsr�z&Error setting up safe analysis agent: N)238r239r	r
�
from_messagesrrrr1r{r)rdr2r�r3r��agent_executorr6s`      r�setup_safe_analysis_agentr��s����7����240��
�+�u�c�
�241
�*�j�S�
�242
�&�}�j�
�243��&$�1�1�
G� 
 �n�=� �/�'3244���,*�#�f�=��&�U��N�������
�6�s�1�v�h�?�@����s�A.A2�2245B�<B�B)-�pandasrr�nltk�nltk.sentimentrr:�langchain_openair�langchain_core.promptsr�langchain_core.runnablesr�dotenvrr-r�langchain_core.toolsr	r246�langchain.agentsrrr
�langchain_experimental.agentsr�langchain.promptsr�downloadrrr#r7r<r?rXrer�r�r�r�r�r�r�r�rJrr�<module>rs������5�	�#�1�5��	��%�'�E�5�G�1��
�
�o���
����&&�P247,�(�B�,�Q5�fW��&>�#<�JQ�f$�L9r