Spaces:
Sleeping
Sleeping
Commit
·
a42c61e
1
Parent(s):
798f95e
......
Browse files- tabs/chatbot_tab.py +65 -112
- tabs/google_drive_read_preprompt.py +113 -0
tabs/chatbot_tab.py
CHANGED
|
@@ -23,7 +23,7 @@ from langgraph.graph.message import add_messages
|
|
| 23 |
from typing_extensions import Annotated, TypedDict
|
| 24 |
from dotenv import load_dotenv
|
| 25 |
import time
|
| 26 |
-
|
| 27 |
import warnings
|
| 28 |
warnings.filterwarnings('ignore')
|
| 29 |
|
|
@@ -41,6 +41,7 @@ os.getenv("LANGCHAIN_API_KEY")
|
|
| 41 |
os.getenv("MISTRAL_API_KEY")
|
| 42 |
os.getenv("OPENAI_API_KEY")
|
| 43 |
|
|
|
|
| 44 |
prompt = ChatPromptTemplate.from_messages(
|
| 45 |
[
|
| 46 |
(
|
|
@@ -75,6 +76,10 @@ app = workflow.compile(checkpointer=memory)
|
|
| 75 |
selected_index1 = 0
|
| 76 |
selected_index2 = 0
|
| 77 |
selected_index3 = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
selected_options4 = []
|
| 79 |
selected_options5 = []
|
| 80 |
selected_options6 = []
|
|
@@ -84,20 +89,26 @@ context=""
|
|
| 84 |
human_message1=""
|
| 85 |
thread_id =""
|
| 86 |
virulence = 1
|
|
|
|
| 87 |
if 'model' in st.session_state:
|
| 88 |
used_model = st.session_state.model
|
| 89 |
|
| 90 |
# @st.cache_data
|
| 91 |
def init():
|
| 92 |
-
global config,thread_id, context,human_message1,ai_message1,language, app, model_speech,prompt,model
|
| 93 |
-
global selected_index1, selected_index2, selected_index3,
|
|
|
|
| 94 |
|
| 95 |
-
model_speech = whisper.load_model("base")
|
| 96 |
|
| 97 |
-
if st.button(label=tr("Nouvelle conversation"), type="primary"):
|
| 98 |
selected_index1 = 0
|
| 99 |
selected_index2 = 0
|
| 100 |
selected_index3 = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
selected_options4 = []
|
| 102 |
selected_options5 = []
|
| 103 |
selected_options6 = []
|
|
@@ -116,98 +127,73 @@ def init():
|
|
| 116 |
model = ChatMistralAI(model=st.session_state.model)
|
| 117 |
if 'model' in st.session_state:
|
| 118 |
used_model=st.session_state.model
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
selected_option1 = st.selectbox(tr("Interlocuteur"),translated_options1, index = selected_index1) # index=int(var1_init))
|
| 124 |
selected_index1 = translated_options1.index(selected_option1)
|
| 125 |
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
translated_options2 = [tr(o) for o in options2]
|
| 129 |
-
selected_option2 = st.selectbox(tr("Activité"),translated_options2, index = selected_index2) # index=int(var2_init))
|
| 130 |
selected_index2 = translated_options2.index(selected_option2)
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"Services et conseil spécialisés"]
|
| 135 |
-
translated_options3 = [tr(o) for o in options3]
|
| 136 |
-
selected_option3 = st.selectbox(tr("Domaine d'activité"),translated_options3, index=selected_index3) #index=int(var3_init))
|
| 137 |
selected_index3 = translated_options3.index(selected_option3)
|
| 138 |
|
| 139 |
-
context = tr(f"""Tu es un {
|
| 140 |
-
Cette entreprise propose des {
|
| 141 |
""")
|
| 142 |
context = st.text_area(label=tr("Résumé du Contexte (modifiable):"), value=context)
|
| 143 |
st.markdown('''
|
| 144 |
------------------------------------------------------------------------------------
|
| 145 |
''')
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
]
|
| 151 |
-
selected_options4 = st.multiselect(tr("Problématiques"),[tr(o) for o in options4], default=[tr(o) for o in selected_options4])
|
| 152 |
problematique = selected_options4
|
| 153 |
if problematique != []:
|
| 154 |
-
markdown_text4 = """\n"""+tr(
|
| 155 |
markdown_text4 = markdown_text4+"".join(f"\n- {o}" for o in problematique)
|
| 156 |
st.write(markdown_text4)
|
| 157 |
else: markdown_text4 = ""
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
"Mettre en oeuvre des meilleures pratiques commerciales"
|
| 163 |
-
]
|
| 164 |
-
selected_options5 = st.multiselect(tr("Processus"),[tr(o) for o in options5],default=[tr(o) for o in selected_options5])
|
| 165 |
processus = selected_options5
|
| 166 |
if processus != []:
|
| 167 |
-
markdown_text5 = """\n\n"""+tr(
|
| 168 |
markdown_text5 = markdown_text5+"".join(f"\n- {o}" for o in processus)
|
| 169 |
st.write(markdown_text5)
|
| 170 |
else: markdown_text5 = ""
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
"Augmenter taux de conversion d’affaires gagnées",
|
| 176 |
-
"Améliorer l’efficience et la confiance des forces de ventes",
|
| 177 |
-
"Réduire temps de monté en compétence des nouvelles embauches",
|
| 178 |
-
"Fidéliser les clients"
|
| 179 |
-
]
|
| 180 |
-
selected_options6 = st.multiselect(tr("Objectifs d'amélioration"),[tr(o) for o in options6],default=[tr(o) for o in selected_options6])
|
| 181 |
objectifs = selected_options6
|
| 182 |
if objectifs != []:
|
| 183 |
-
markdown_text6 = """\n\n"""+tr(
|
| 184 |
markdown_text6 = markdown_text6+"".join(f"\n- {o}" for o in objectifs)
|
| 185 |
st.write(markdown_text6)
|
| 186 |
else: markdown_text6 = ""
|
| 187 |
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
"Outils de gestion des présentations clients tels que Logiciel Powerpoint ou Google slide",
|
| 192 |
-
"Conseil externe en positionnement marché & produit",
|
| 193 |
-
"Services externes de formation des équipes commerciales"
|
| 194 |
-
]
|
| 195 |
-
selected_options7 = st.multiselect(tr("Solutions utilisées"),[tr(o) for o in options7],default=[tr(o) for o in selected_options7])
|
| 196 |
solutions_utilisees = selected_options7
|
| 197 |
if solutions_utilisees != []:
|
| 198 |
-
markdown_text7 = """\n\n"""+tr(
|
| 199 |
markdown_text7 = markdown_text7+"".join(f"\n- {o}" for o in solutions_utilisees)
|
| 200 |
st.write(markdown_text7)
|
| 201 |
st.write("")
|
| 202 |
else: markdown_text7 = ""
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
"Obtenir du prospect qu'il achète ou s'engage à acheter la solution que je propose"]
|
| 207 |
-
translated_options8 = [tr(o) for o in options8]
|
| 208 |
-
selected_option8 = st.selectbox(tr("Objectif du vendeur lors de sa conversation avec le prospect:"),translated_options8, index = selected_index8)
|
| 209 |
selected_index8 = translated_options8.index(selected_option8)
|
| 210 |
-
markdown_text8 = """\n\n"""+tr(
|
| 211 |
|
| 212 |
|
| 213 |
col1, col2, col3 = st.columns(3)
|
|
@@ -215,14 +201,14 @@ Cette entreprise propose des {options3[selected_index3]}.
|
|
| 215 |
virulence = st.slider(tr("Virulence (choisissez une valeur entre 1 et 5)"), min_value=1, max_value=5, step=1,value=virulence)
|
| 216 |
markdown_text9 = """\n\n"""+tr(f"""Le prospect est très occupé et n'aime pas être dérangé inutilement.
|
| 217 |
Tu vas utiliser une échelle de 1 à 5 de virulence du prospect à l'égard du vendeur.
|
| 218 |
-
Pour cette simulation utilise le
|
| 219 |
|
| 220 |
human_message1 = tr("""Je souhaites que nous ayons une conversation verbale entre un commercial de mon entreprise, et toi que je prospecte.
|
| 221 |
-
Mon entreprise propose une solution logicielle pour gérer la proposition de valeur d’
|
| 222 |
""")+markdown_text4+markdown_text5+markdown_text6+markdown_text7+markdown_text8+markdown_text9+tr(f"""
|
| 223 |
|
| 224 |
Je suis le vendeur.
|
| 225 |
-
Répond à mes questions en tant que {
|
| 226 |
et mon équipe de vente n'est pas performante.
|
| 227 |
|
| 228 |
Attention: Ce n'est pas toi qui m'aide, c'est moi qui t'aide avec ma solution.
|
|
@@ -234,7 +220,7 @@ Attention: Ce n'est pas toi qui m'aide, c'est moi qui t'aide avec ma solution.
|
|
| 234 |
------------------------------------------------------------------------------------
|
| 235 |
''')
|
| 236 |
|
| 237 |
-
ai_message1 = tr(f"J'ai bien compris, je suis un {
|
| 238 |
|
| 239 |
|
| 240 |
|
|
@@ -312,7 +298,7 @@ def play_audio(custom_sentence, Lang_target, speed=1.0):
|
|
| 312 |
|
| 313 |
|
| 314 |
def run():
|
| 315 |
-
global thread_id, config, model_speech, language,prompt,model, model_name
|
| 316 |
|
| 317 |
st.write("")
|
| 318 |
st.write("")
|
|
@@ -445,51 +431,18 @@ def run():
|
|
| 445 |
st.markdown(message["content"])
|
| 446 |
else:
|
| 447 |
st.write("**thread_id:** "+thread_id)
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
output = app.invoke(
|
| 464 |
-
{"messages": q2,"language": language},
|
| 465 |
-
config,
|
| 466 |
-
)
|
| 467 |
-
custom_sentence = output["messages"][-1].content
|
| 468 |
-
st.write(custom_sentence)
|
| 469 |
-
st.write("")
|
| 470 |
-
if (used_model[:3] == 'mis'):
|
| 471 |
-
time.sleep(2)
|
| 472 |
-
|
| 473 |
-
st.divider()
|
| 474 |
-
st.write("")
|
| 475 |
-
q3 = st.text_input(label="", value=tr("Peux tu me donner une analyse succinte de la tonalité du vendeur ?"),label_visibility="collapsed")
|
| 476 |
-
output = app.invoke(
|
| 477 |
-
{"messages": q3,"language": language},
|
| 478 |
-
config,
|
| 479 |
-
)
|
| 480 |
-
custom_sentence = output["messages"][-1].content
|
| 481 |
-
st.write(custom_sentence)
|
| 482 |
-
st.write("")
|
| 483 |
-
if (used_model[:3] == 'mis'):
|
| 484 |
-
time.sleep(2)
|
| 485 |
-
|
| 486 |
-
st.divider()
|
| 487 |
-
st.write("")
|
| 488 |
-
q3 = st.text_input(label="", value=tr("Le vendeur a-t-il atteint son objectif ? Si ce n'est pas cas, est il loin de l'avoir atteint ? Dans tous les cas, explique ta réponse."),label_visibility="collapsed")
|
| 489 |
-
output = app.invoke(
|
| 490 |
-
{"messages": q3,"language": language},
|
| 491 |
-
config,
|
| 492 |
-
)
|
| 493 |
-
custom_sentence = output["messages"][-1].content
|
| 494 |
-
st.write(custom_sentence)
|
| 495 |
-
st.write("")
|
|
|
|
| 23 |
from typing_extensions import Annotated, TypedDict
|
| 24 |
from dotenv import load_dotenv
|
| 25 |
import time
|
| 26 |
+
from tabs.google_drive_read_preprompt import read_param, format_param
|
| 27 |
import warnings
|
| 28 |
warnings.filterwarnings('ignore')
|
| 29 |
|
|
|
|
| 41 |
os.getenv("MISTRAL_API_KEY")
|
| 42 |
os.getenv("OPENAI_API_KEY")
|
| 43 |
|
| 44 |
+
|
| 45 |
prompt = ChatPromptTemplate.from_messages(
|
| 46 |
[
|
| 47 |
(
|
|
|
|
| 76 |
selected_index1 = 0
|
| 77 |
selected_index2 = 0
|
| 78 |
selected_index3 = 0
|
| 79 |
+
selected_indices4 = []
|
| 80 |
+
selected_indices5 = []
|
| 81 |
+
selected_indices6 = []
|
| 82 |
+
selected_indices7 = []
|
| 83 |
selected_options4 = []
|
| 84 |
selected_options5 = []
|
| 85 |
selected_options6 = []
|
|
|
|
| 89 |
human_message1=""
|
| 90 |
thread_id =""
|
| 91 |
virulence = 1
|
| 92 |
+
question = []
|
| 93 |
if 'model' in st.session_state:
|
| 94 |
used_model = st.session_state.model
|
| 95 |
|
| 96 |
# @st.cache_data
|
| 97 |
def init():
|
| 98 |
+
global config,thread_id, context,human_message1,ai_message1,language, app, model_speech,prompt,model,question
|
| 99 |
+
global selected_index1, selected_index2, selected_index3, selected_indices4,selected_indices5,selected_indices6,selected_indices7
|
| 100 |
+
global selected_options4,selected_options5,selected_options6,selected_options7, selected_index8, virulence, used_model
|
| 101 |
|
| 102 |
+
model_speech = whisper.load_model("base")
|
| 103 |
|
| 104 |
+
if (st.button(label=tr("Nouvelle conversation"), type="primary")):
|
| 105 |
selected_index1 = 0
|
| 106 |
selected_index2 = 0
|
| 107 |
selected_index3 = 0
|
| 108 |
+
selected_indices4 = []
|
| 109 |
+
selected_indices5 = []
|
| 110 |
+
selected_indices6 = []
|
| 111 |
+
selected_indices7 = []
|
| 112 |
selected_options4 = []
|
| 113 |
selected_options5 = []
|
| 114 |
selected_options6 = []
|
|
|
|
| 127 |
model = ChatMistralAI(model=st.session_state.model)
|
| 128 |
if 'model' in st.session_state:
|
| 129 |
used_model=st.session_state.model
|
| 130 |
+
|
| 131 |
+
label, question, options = format_param()
|
| 132 |
+
translated_options1 = [tr(o) for o in options[0]]
|
| 133 |
+
selected_option1 = st.selectbox(tr(label[0]),translated_options1, index = selected_index1) # index=int(var1_init))
|
|
|
|
| 134 |
selected_index1 = translated_options1.index(selected_option1)
|
| 135 |
|
| 136 |
+
translated_options2 = [tr(o) for o in options[1]]
|
| 137 |
+
selected_option2 = st.selectbox(tr(label[1]),translated_options2, index = selected_index2) # index=int(var2_init))
|
|
|
|
|
|
|
| 138 |
selected_index2 = translated_options2.index(selected_option2)
|
| 139 |
|
| 140 |
+
translated_options3 = [tr(o) for o in options[2]]
|
| 141 |
+
selected_option3 = st.selectbox(tr(label[2]),translated_options3, index=selected_index3) #index=int(var3_init))
|
|
|
|
|
|
|
|
|
|
| 142 |
selected_index3 = translated_options3.index(selected_option3)
|
| 143 |
|
| 144 |
+
context = tr(f"""Tu es un {options[0][selected_index1]}, d'une {options[1][selected_index2]}.
|
| 145 |
+
Cette entreprise propose des {options[2][selected_index3]}.
|
| 146 |
""")
|
| 147 |
context = st.text_area(label=tr("Résumé du Contexte (modifiable):"), value=context)
|
| 148 |
st.markdown('''
|
| 149 |
------------------------------------------------------------------------------------
|
| 150 |
''')
|
| 151 |
|
| 152 |
+
translated_options4 = [tr(o) for o in options[3]]
|
| 153 |
+
selected_options4 = st.multiselect(tr(label[3]),translated_options4, default=[translated_options4[o] for o in selected_indices4])
|
| 154 |
+
selected_indices4 = [translated_options4.index(o) for o in selected_options4]
|
|
|
|
|
|
|
| 155 |
problematique = selected_options4
|
| 156 |
if problematique != []:
|
| 157 |
+
markdown_text4 = """\n"""+tr(question[3])
|
| 158 |
markdown_text4 = markdown_text4+"".join(f"\n- {o}" for o in problematique)
|
| 159 |
st.write(markdown_text4)
|
| 160 |
else: markdown_text4 = ""
|
| 161 |
|
| 162 |
+
translated_options5 = [tr(o) for o in options[4]]
|
| 163 |
+
selected_options5 = st.multiselect(tr(label[4]),translated_options5, default=[translated_options5[o] for o in selected_indices5])
|
| 164 |
+
selected_indices5 = [translated_options5.index(o) for o in selected_options5]
|
|
|
|
|
|
|
|
|
|
| 165 |
processus = selected_options5
|
| 166 |
if processus != []:
|
| 167 |
+
markdown_text5 = """\n\n"""+tr(question[4])
|
| 168 |
markdown_text5 = markdown_text5+"".join(f"\n- {o}" for o in processus)
|
| 169 |
st.write(markdown_text5)
|
| 170 |
else: markdown_text5 = ""
|
| 171 |
|
| 172 |
+
translated_options6 = [tr(o) for o in options[5]]
|
| 173 |
+
selected_options6 = st.multiselect(tr(label[5]),translated_options6, default=[translated_options6[o] for o in selected_indices6])
|
| 174 |
+
selected_indices6 = [translated_options6.index(o) for o in selected_options6]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
objectifs = selected_options6
|
| 176 |
if objectifs != []:
|
| 177 |
+
markdown_text6 = """\n\n"""+tr(question[5])
|
| 178 |
markdown_text6 = markdown_text6+"".join(f"\n- {o}" for o in objectifs)
|
| 179 |
st.write(markdown_text6)
|
| 180 |
else: markdown_text6 = ""
|
| 181 |
|
| 182 |
+
translated_options7 = [tr(o) for o in options[6]]
|
| 183 |
+
selected_options7 = st.multiselect(tr(label[6]),translated_options7, default=[translated_options7[o] for o in selected_indices7])
|
| 184 |
+
selected_indices7 = [translated_options7.index(o) for o in selected_options7]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
solutions_utilisees = selected_options7
|
| 186 |
if solutions_utilisees != []:
|
| 187 |
+
markdown_text7 = """\n\n"""+tr(question[6])
|
| 188 |
markdown_text7 = markdown_text7+"".join(f"\n- {o}" for o in solutions_utilisees)
|
| 189 |
st.write(markdown_text7)
|
| 190 |
st.write("")
|
| 191 |
else: markdown_text7 = ""
|
| 192 |
|
| 193 |
+
translated_options8 = [tr(o) for o in options[7]]
|
| 194 |
+
selected_option8 = st.selectbox(tr(label[7]),translated_options8, index = selected_index8)
|
|
|
|
|
|
|
|
|
|
| 195 |
selected_index8 = translated_options8.index(selected_option8)
|
| 196 |
+
markdown_text8 = """\n\n"""+tr(question[7])+"""\n"""+(f"""{translated_options8[selected_index8]}""")
|
| 197 |
|
| 198 |
|
| 199 |
col1, col2, col3 = st.columns(3)
|
|
|
|
| 201 |
virulence = st.slider(tr("Virulence (choisissez une valeur entre 1 et 5)"), min_value=1, max_value=5, step=1,value=virulence)
|
| 202 |
markdown_text9 = """\n\n"""+tr(f"""Le prospect est très occupé et n'aime pas être dérangé inutilement.
|
| 203 |
Tu vas utiliser une échelle de 1 à 5 de virulence du prospect à l'égard du vendeur.
|
| 204 |
+
Pour cette simulation utilise le niveau {virulence}""")
|
| 205 |
|
| 206 |
human_message1 = tr("""Je souhaites que nous ayons une conversation verbale entre un commercial de mon entreprise, et toi que je prospecte.
|
| 207 |
+
Mon entreprise propose une solution logicielle pour gérer la proposition de valeur d’entreprise B2B qui commercialise des solutions technologiques.
|
| 208 |
""")+markdown_text4+markdown_text5+markdown_text6+markdown_text7+markdown_text8+markdown_text9+tr(f"""
|
| 209 |
|
| 210 |
Je suis le vendeur.
|
| 211 |
+
Répond à mes questions en tant que {options[0][selected_index1]}, connaissant mal le concept de proposition de valeur,
|
| 212 |
et mon équipe de vente n'est pas performante.
|
| 213 |
|
| 214 |
Attention: Ce n'est pas toi qui m'aide, c'est moi qui t'aide avec ma solution.
|
|
|
|
| 220 |
------------------------------------------------------------------------------------
|
| 221 |
''')
|
| 222 |
|
| 223 |
+
ai_message1 = tr(f"J'ai bien compris, je suis un {options[0][selected_index1]} prospecté et je réponds seulement à tes questions. Je réponds à une seule question à la fois, sans commencer mes réponses par 'En tant que {options[0][selected_index1]}'")
|
| 224 |
|
| 225 |
|
| 226 |
|
|
|
|
| 298 |
|
| 299 |
|
| 300 |
def run():
|
| 301 |
+
global thread_id, config, model_speech, language,prompt,model, model_name, question
|
| 302 |
|
| 303 |
st.write("")
|
| 304 |
st.write("")
|
|
|
|
| 431 |
st.markdown(message["content"])
|
| 432 |
else:
|
| 433 |
st.write("**thread_id:** "+thread_id)
|
| 434 |
+
for i in range(8,len(question)):
|
| 435 |
+
st.write("")
|
| 436 |
+
|
| 437 |
+
q = st.text_input(label="", value=tr(question[i]),label_visibility="collapsed")
|
| 438 |
+
output = app.invoke(
|
| 439 |
+
{"messages": q,"language": language},
|
| 440 |
+
config,
|
| 441 |
+
)
|
| 442 |
+
custom_sentence = output["messages"][-1].content
|
| 443 |
+
st.write(custom_sentence)
|
| 444 |
+
st.write("")
|
| 445 |
+
if (used_model[:3] == 'mis'):
|
| 446 |
+
time.sleep(2)
|
| 447 |
+
|
| 448 |
+
st.divider()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tabs/google_drive_read_preprompt.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from googleapiclient.discovery import build
|
| 5 |
+
from google.auth.credentials import AnonymousCredentials
|
| 6 |
+
from google.auth.transport.requests import Request
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from google.oauth2.credentials import Credentials
|
| 9 |
+
|
| 10 |
+
if st.session_state.Cloud != 0:
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Charger les secrets depuis les variables d'environnement
|
| 14 |
+
CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
|
| 15 |
+
CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
|
| 16 |
+
REFRESH_TOKEN = os.getenv("GOOGLE_REFRESH_TOKEN")
|
| 17 |
+
GOOGLE_PREPROMPT_FILE_ID = os.getenv("GOOGLE_PREPROMPT_FILE_ID")
|
| 18 |
+
|
| 19 |
+
# URL pour rafraîchir le token
|
| 20 |
+
TOKEN_URL = "https://oauth2.googleapis.com/token"
|
| 21 |
+
|
| 22 |
+
# Fonction pour obtenir un token d'accès
|
| 23 |
+
def get_access_token():
|
| 24 |
+
data = {
|
| 25 |
+
"client_id": CLIENT_ID,
|
| 26 |
+
"client_secret": CLIENT_SECRET,
|
| 27 |
+
"refresh_token": REFRESH_TOKEN,
|
| 28 |
+
"grant_type": "refresh_token",
|
| 29 |
+
}
|
| 30 |
+
response = requests.post(TOKEN_URL, data=data)
|
| 31 |
+
response_data = response.json()
|
| 32 |
+
if "access_token" in response_data:
|
| 33 |
+
return response_data["access_token"]
|
| 34 |
+
else:
|
| 35 |
+
raise Exception(f"Erreur d'obtention du token d'accès : {response_data}")
|
| 36 |
+
|
| 37 |
+
# Obtenir le token d'accès
|
| 38 |
+
access_token = get_access_token()
|
| 39 |
+
|
| 40 |
+
# Fonction pour lire le contenu d'un Google Doc
|
| 41 |
+
def read_google_doc(doc_id):
|
| 42 |
+
# Créer les credentials à partir du token d'accès
|
| 43 |
+
creds = Credentials(token=access_token)
|
| 44 |
+
|
| 45 |
+
# Construire le service Google Docs avec les credentials
|
| 46 |
+
docs_service = build('docs', 'v1', credentials=creds)
|
| 47 |
+
|
| 48 |
+
# Requête pour obtenir le contenu du document
|
| 49 |
+
try:
|
| 50 |
+
document = docs_service.documents().get(documentId=doc_id).execute()
|
| 51 |
+
content = document.get('body', {}).get('content', [])
|
| 52 |
+
|
| 53 |
+
# Initialiser une liste pour stocker chaque ligne
|
| 54 |
+
lines = []
|
| 55 |
+
|
| 56 |
+
# Extraire chaque ligne de texte
|
| 57 |
+
for element in content:
|
| 58 |
+
if 'paragraph' in element:
|
| 59 |
+
paragraph_text = "" # Regrouper tout le texte d'un paragraphe
|
| 60 |
+
for paragraph in element['paragraph']['elements']:
|
| 61 |
+
t = paragraph.get('textRun', {}).get('content', '')
|
| 62 |
+
paragraph_text += t # Ajouter le texte à la ligne du paragraphe
|
| 63 |
+
|
| 64 |
+
# Ajouter le paragraphe complet s'il contient du texte
|
| 65 |
+
if paragraph_text.strip():
|
| 66 |
+
lines.append(paragraph_text.strip())
|
| 67 |
+
|
| 68 |
+
return lines # Retourne une liste contenant toutes les lignes
|
| 69 |
+
except Exception as e:
|
| 70 |
+
raise Exception(f"Erreur lors de la lecture du document : {e}")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def read_param():
|
| 74 |
+
# Lire et afficher le contenu d'un fichier
|
| 75 |
+
try:
|
| 76 |
+
lines = read_google_doc(GOOGLE_PREPROMPT_FILE_ID)
|
| 77 |
+
print("\nContenu du document ligne par ligne :")
|
| 78 |
+
# for idx, line in enumerate(lines, start=1):
|
| 79 |
+
# print(f"Ligne {idx}: {line}")
|
| 80 |
+
return lines
|
| 81 |
+
except Exception as e:
|
| 82 |
+
st.write(f"Erreur : {e}")
|
| 83 |
+
|
| 84 |
+
def format_param():
|
| 85 |
+
try:
|
| 86 |
+
lines = read_param()
|
| 87 |
+
label = []
|
| 88 |
+
question = []
|
| 89 |
+
options = [[] for _ in range(8)]
|
| 90 |
+
i = 0
|
| 91 |
+
for p in range(8):
|
| 92 |
+
while (lines[i][:3] == "==="):
|
| 93 |
+
i +=1
|
| 94 |
+
label.append(lines[i][8:])
|
| 95 |
+
i +=1
|
| 96 |
+
if p not in [0,1,2]:
|
| 97 |
+
question.append(lines[i])
|
| 98 |
+
i +=1
|
| 99 |
+
else: question.append("")
|
| 100 |
+
while (lines[i][:3] != "==="):
|
| 101 |
+
options[p].append(lines[i])
|
| 102 |
+
i +=1
|
| 103 |
+
i+=1
|
| 104 |
+
while i<len(lines):
|
| 105 |
+
question.append(lines[i])
|
| 106 |
+
i +=1
|
| 107 |
+
print("label:\n",label)
|
| 108 |
+
print("question:\n",question)
|
| 109 |
+
print("options:\n",options)
|
| 110 |
+
except Exception as e:
|
| 111 |
+
st.write(f"Erreur : {e}")
|
| 112 |
+
return label, question, options
|
| 113 |
+
|