File size: 15,729 Bytes
3d40d3b
1b7e1bb
c3f257f
eefba2d
edc46fa
3eff0be
5431a98
 
ff2536f
c72ad56
6bfcb60
629889e
3d40d3b
edc46fa
 
c6720e8
edc46fa
5431a98
da24b26
90964f5
 
a94da6c
90964f5
43d0d69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b1928ac
 
 
 
 
 
 
 
 
 
 
 
2ea893d
ff2536f
b1928ac
 
 
 
 
32d38dd
b1928ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8720ce5
b1928ac
 
 
 
 
 
 
 
 
 
 
 
 
ac83ae2
b1928ac
 
 
 
 
 
 
 
 
 
 
 
 
3323043
40dcc90
8663b79
 
 
edc46fa
8663b79
 
3eff0be
 
 
 
 
 
 
 
 
 
 
 
 
821eeb7
0075cbc
3eff0be
53b5d98
1060556
e05976c
d724221
3eff0be
53b5d98
1060556
e05976c
d724221
3eff0be
53b5d98
1060556
d724221
cf4b647
53b5d98
cf4b647
d724221
cf4b647
53b5d98
cf4b647
d724221
cf4b647
53b5d98
cf4b647
d724221
53b5d98
 
 
d724221
99a9a56
 
 
 
 
 
d724221
99a9a56
 
 
d724221
99a9a56
 
 
d724221
bcda7b4
 
 
 
 
 
d724221
bcda7b4
 
 
d724221
bcda7b4
 
 
cf4b647
df7340c
 
a94da6c
6b8afe4
 
e697884
8663b79
 
 
edc46fa
4e761ea
4d71c10
 
6a98842
4d71c10
 
 
 
 
 
 
 
 
 
 
b1928ac
ff2536f
b1928ac
ff2536f
b1928ac
 
ff2536f
231d9e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import os
import json
import bcrypt
import pandas as pd
import numpy as np
import plotly.express as px
from typing import List
from pathlib import Path
from langchain_openai import ChatOpenAI
from langchain.schema.runnable.config import RunnableConfig
from langchain.schema import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate

from langchain.agents import AgentExecutor
from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_pandas_dataframe_agent, create_csv_agent

import chainlit as cl
from chainlit.input_widget import TextInput, Select, Switch, Slider

from deep_translator import GoogleTranslator
from IPython.display import display

@cl.password_auth_callback
def auth_callback(username: str, password: str):
    auth = json.loads(os.environ['CHAINLIT_AUTH_LOGIN'])
    ident = next(d['ident'] for d in auth if d['ident'] == username)
    pwd = next(d['pwd'] for d in auth if d['ident'] == username)
    resultLogAdmin = bcrypt.checkpw(username.encode('utf-8'), bcrypt.hashpw(ident.encode('utf-8'), bcrypt.gensalt())) 
    resultPwdAdmin = bcrypt.checkpw(password.encode('utf-8'), bcrypt.hashpw(pwd.encode('utf-8'), bcrypt.gensalt())) 
    resultRole = next(d['role'] for d in auth if d['ident'] == username)
    if resultLogAdmin and resultPwdAdmin and resultRole == "admindatapcc":
        return cl.User(
            identifier=ident + " : 🧑‍💼 Admin Datapcc", metadata={"role": "admin", "provider": "credentials"}
        )
    elif resultLogAdmin and resultPwdAdmin and resultRole == "userdatapcc":
        return cl.User(
            identifier=ident + " : 🧑‍🎓 User Datapcc", metadata={"role": "user", "provider": "credentials"}
        )
        
def create_agent(filename: str):
    """
    Create an agent that can access and use a large language model (LLM).

    Args:
        filename: The path to the CSV file that contains the data.

    Returns:
        An agent that can access and use the LLM.
    """

    # Create an OpenAI object.
    os.environ['OPENAI_API_KEY'] = os.environ['OPENAI_API_KEY']
    llm = ChatOpenAI(temperature=0, model="gpt-4o-2024-05-13")

    # Read the CSV file into a Pandas DataFrame.
    df = pd.read_csv(filename)

    # Create a Pandas DataFrame agent.
    return create_csv_agent(llm, filename, verbose=False, allow_dangerous_code=True, handle_parsing_errors=True, agent_type=AgentType.OPENAI_FUNCTIONS)

def query_agent(agent, query):
    """
    Query an agent and return the response as a string.

    Args:
        agent: The agent to query.
        query: The query to ask the agent.

    Returns:
        The response from the agent as a string.
    """

    prompt = (
        """
            For the following query, if it requires drawing a table, reply as follows:
            {"table": {"columns": ["column1", "column2", ...], "data": [[value1, value2, ...], [value1, value2, ...], ...]}}

            If the query requires creating a bar chart, reply as follows:
            {"bar": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}

            If the query requires creating a line chart, reply as follows:
            {"line": {"columns": ["A", "B", "C", ...], "data": [25, 24, 10, ...]}}

            There can only be two types of chart, "bar" and "line".

            If it is just asking a question that requires neither, reply as follows:
            {"answer": "answer"}
            Example:
            {"answer": "The title with the highest rating is 'Gilead'"}

            If you do not know the answer, reply as follows:
            {"answer": "I do not know."}

            Return all output as a string.

            All strings in "columns" list and data list, should be in double quotes,

            For example: {"columns": ["title", "ratings_count"], "data": [["Gilead", 361], ["Spider's Web", 5164]]}

            Lets think step by step.

            Below is the query.
            Query: 
            """
        + query
    )

    # Run the prompt through the agent.
    response = agent.invoke(prompt)

    # Convert the response to a string.
    return response.__str__()
    
def decode_response(response: str) -> dict:
    """This function converts the string response from the model to a dictionary object.

    Args:
        response (str): response from the model

    Returns:
        dict: dictionary with response data
    """
    return json.loads("[" + response + "]")

def write_response(response_dict: dict):
    """
    Write a response from an agent to a Streamlit app.

    Args:
        response_dict: The response from the agent.

    Returns:
        None.
    """

    # Check if the response is an answer.
    return response_dict["answer"]

@cl.set_chat_profiles
async def chat_profile():
    return [
        cl.ChatProfile(name="Traitement des données d'enquête : «Expé CFA : questionnaire auprès des professionnels de la branche de l'agencement»",markdown_description="Vidéo exploratoire autour de l'événement",icon="/public/logo-ofipe.png",),
    ]
    
#@cl.set_starters
#async def set_starters():
#    return [
#        cl.Starter(
#            label="Répartition du nombre de CAA dans les entreprises",
#            message="Quel est le nombre de chargé.e d'affaires en agencement dans chaque type d'entreprises?",
#            icon="/public/request-theme.svg",
#            )
#    ]

@cl.on_chat_start
async def on_chat_start():
    await cl.Message(f"> SURVEYIA").send()
    elements = []
    df = pd.read_csv('./public/survey.csv')
    df_taille = df.groupby('taille_entreprise').size().reset_index(name='obs')
    fig_taille = px.pie(df_taille, names='taille_entreprise', values='obs', color='obs', title="La taille des entreprises ayant répondu", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe).update_traces(textposition='inside', textinfo='percent+label').update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_taille", figure=fig_taille, display="inline", size="small"))
        
    #await cl.sleep(2)
    df_temps = df.groupby('temps_active_domaine_agencement').size().reset_index(name='obs')
    fig_temps = px.pie(df_temps, names='temps_active_domaine_agencement', values='obs', color='obs', title="L’engagement dans le domaine de l’agencement", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe).update_traces(textposition='inside', textinfo='percent+label').update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_temps", figure=fig_temps, display="inline", size="small"))
        
    #await cl.sleep(2)
    df_temps_entreprise = df.groupby(['temps_active_domaine_agencement', 'taille_entreprise']).size().reset_index(name='obs')
    fig_temps_entreprise = px.bar(df_temps_entreprise, x='temps_active_domaine_agencement', y='obs', color='taille_entreprise', title="L’engagement dans le domaine de l’agencement par taille d'entreprise", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_temps_entreprise", figure=fig_temps_entreprise, display="inline", size="small"))
    #await cl.sleep(2)
    df_nb_charge = df.groupby('nombre_chargés_affaires').size().reset_index(name='obs')
    fig_nb_charge = px.pie(df_nb_charge, names='nombre_chargés_affaires', values='obs', color='obs', title="Le nombre de chargé.e d’affaires en agencement", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe).update_traces(textposition='inside', textinfo='percent+label').update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_nb_charge", figure=fig_nb_charge, display="inline", size="small"))
    #await cl.sleep(2)
    df_nb_charge_entreprise = df.groupby(['nombre_chargés_affaires', 'taille_entreprise']).size().reset_index(name='obs')
    fig_nb_charge_entreprise = px.bar(df_nb_charge_entreprise, x='nombre_chargés_affaires', y='obs', color='taille_entreprise', title="Le nombre de chargé.e d’affaires en agencement par taille d'entreprise", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_nb_charge_entreprise", figure=fig_nb_charge_entreprise, display="inline", size="small"))
    #await cl.sleep(2)
    df_nb_charge_engagement = df.groupby(['nombre_chargés_affaires', 'temps_active_domaine_agencement']).size().reset_index(name='obs')
    fig_nb_charge_entreprise = px.bar(df_nb_charge_engagement, x='nombre_chargés_affaires', y='obs', color='temps_active_domaine_agencement', title="Le nombre de chargé.e d’affaires en agencement par année d'engagement", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_nb_charge_entreprise", figure=fig_nb_charge_entreprise, display="inline", size="small"))
    #await cl.sleep(2)
    df_statut = df.groupby('fonction_Statut_repondant').size().reset_index(name='obs')
    fig_statut = px.bar(df_statut, x='obs', y='fonction_Statut_repondant', orientation='h', color='obs', title="Le profil des répondants", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_statut", figure=fig_statut, display="inline", size="small"))
    #await cl.sleep(2)
    df1 = df
    df1['principaux_interlocuteurs'] = df1['principaux_interlocuteurs'].str.split(';')
    df1 = df1.explode('principaux_interlocuteurs')
    df_interlocuteur = df1.groupby('principaux_interlocuteurs').size().reset_index(name='obs')
    fig_interlocuteur = px.bar(df_interlocuteur, x='obs', y='principaux_interlocuteurs', orientation='h', color='obs', title="Les principaux interlocuteurs du CAA", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_interlocuteur", figure=fig_interlocuteur, display="inline", size="small"))
    #await cl.sleep(2)
    df_interlocuteur_entreprise = df1.groupby(['principaux_interlocuteurs', 'taille_entreprise']).size().reset_index(name='obs')
    fig_interlocuteur_entreprise = px.bar(df_interlocuteur_entreprise, x='obs', y='principaux_interlocuteurs', orientation='h', color='taille_entreprise', title="Les principaux interlocuteurs du CAA par taille d'entreprise", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_interlocuteur_entreprise", figure=fig_interlocuteur_entreprise, display="inline", size="small"))
    #await cl.sleep(2)
    df_interlocuteur_nb_charge = df1.groupby(['principaux_interlocuteurs', 'nombre_chargés_affaires']).size().reset_index(name='obs')
    fig_interlocuteur_nb_charge = px.bar(df_interlocuteur_nb_charge, x='obs', y='principaux_interlocuteurs', orientation='h', color='nombre_chargés_affaires', title="Les principaux interlocuteurs du CAA par nombre chargé.e d'affaires", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_interlocuteur_nb_charge", figure=fig_interlocuteur_nb_charge, display="inline", size="small"))
    #await cl.sleep(2)
    df2 = df
    df2['principales_compétences_attendues'] = df2['principales_compétences_attendues'].str.split(';')
    df2 = df2.explode('principales_compétences_attendues')
    df_competences = df2.groupby('principales_compétences_attendues').size().reset_index(name='obs')
    fig_competences = px.bar(df_competences, x='obs', y='principales_compétences_attendues', orientation='h', color='obs', title="Les principales compétences attendues", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_competences", figure=fig_competences, display="inline", size="small"))
    #await cl.sleep(2)
    df_competences_entreprise = df2.groupby(['principales_compétences_attendues', 'taille_entreprise']).size().reset_index(name='obs')
    fig_competences_entreprise = px.bar(df_competences_entreprise, x='obs', y='principales_compétences_attendues', orientation='h', color='taille_entreprise', title="Les principales compétences attendues par taille d'entreprise", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_competences_entreprise", figure=fig_competences_entreprise, display="inline", size="small"))
    #await cl.sleep(2)
    df_competences_nb_charge = df2.groupby(['principales_compétences_attendues', 'nombre_chargés_affaires']).size().reset_index(name='obs')
    fig_competences_nb_charge = px.bar(df_competences_nb_charge, x='obs', y='principales_compétences_attendues', orientation='h', color='nombre_chargés_affaires', title="Les principales compétences attendues par nombre chargé.e d'affaires", labels={'obs':'nombre'}, color_discrete_sequence=px.colors.qualitative.Safe, text_auto=True).update_layout(font=dict(size=9,color="RebeccaPurple"))
    elements.append(cl.Plotly(name="chart_competences_nb_charge", figure=fig_competences_nb_charge, display="inline", size="small"))
    
    tableau_taille = [cl.Text(name="Tableaux", content=df_taille.to_html(), display="side")]
    tableau_temps = [cl.Text(name="Tableaux", content=df_temps.to_html(), display="side")]    

    await cl.Message(content="Tableaux des données de La \"taille des entreprises ayant répondu\"", elements=tableau_taille,).send()
    await cl.Message(content="Tableaux des données de \"L’engagement dans le domaine de l’agencement\"", elements=tableau_temps,).send()
    await cl.Message(content="Datavisualisation de l'enquête des recruteurs des chargé.e.s d'affaires en agencement", elements=elements).send()
    
@cl.on_message
async def on_message(message: cl.Message):
    await cl.Message(f"> SURVEYIA").send()
    agent = create_agent("./public/surveyia.csv")
    cb = cl.AsyncLangchainCallbackHandler()
    try:
        res = await agent.acall("Réponds en langue française à la question suivante : " + message.content, callbacks=[cb])
        await cl.Message(author="COPILOT",content=GoogleTranslator(source='auto', target='fr').translate(res['output'])).send()
    except ValueError as e:
        res = str(e)
        resArray = res.split(":")
        ans = ''
        if str(res).find('parsing') != -1:
            for i in range(2,len(resArray)):
                ans += resArray[i]
            await cl.Message(author="COPILOT",content=ans.replace("`","")).send()
        else:
            await cl.Message(author="COPILOT",content="Reformulez votre requête, s'il vous plait 😃").send()
    # Query the agent.
    #response = query_agent(agent=agent, query=message.content)
    # Decode the response.
    #decoded_response = decode_response(response)

    # Write the response to the Streamlit app.
    #result = write_response(decoded_response)   
    #await cl.Message(author="COPILOT",content=GoogleTranslator(source='auto', target='fr').translate(result)).send()