Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -145,18 +145,204 @@ def chat_fn(message, history):
|
|
| 145 |
|
| 146 |
# ================= GRADIO ====================
|
| 147 |
|
| 148 |
-
demo = gr.ChatInterface(
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
)
|
| 158 |
|
| 159 |
|
| 160 |
# ===================LANCEMENT ================
|
| 161 |
|
| 162 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
# ================= GRADIO ====================
|
| 147 |
|
| 148 |
+
# demo = gr.ChatInterface(
|
| 149 |
+
# fn=chat_fn,
|
| 150 |
+
# title="🤖 RAG:Specialist en Science Animale 👌",
|
| 151 |
+
# description="Posez vos questions sur l'élévage et la pêche",
|
| 152 |
+
# examples=[
|
| 153 |
+
# "C'est quoi la pêche ?",
|
| 154 |
+
# "Explique l'élévage",
|
| 155 |
+
# "Quelle est la différence entre l'élévage et pêche ?"
|
| 156 |
+
# ]
|
| 157 |
+
# )
|
| 158 |
|
| 159 |
|
| 160 |
# ===================LANCEMENT ================
|
| 161 |
|
| 162 |
+
# demo.launch()
|
| 163 |
+
|
| 164 |
+
# ================= CSS POUR LE STYLE =====================
|
| 165 |
+
|
| 166 |
+
CSS personnalisé=""
|
| 167 |
+
.sidebar {
|
| 168 |
+
arrière-plan:#202123 !important;
|
| 169 |
+
hauteur minimale:100vh;
|
| 170 |
+
}
|
| 171 |
+
.zone-principale {
|
| 172 |
+
arrière-plan:#343541 !important;
|
| 173 |
+
}
|
| 174 |
+
.conteneur de chatbot {
|
| 175 |
+
hauteur : calc(100vh - 200px) !important;
|
| 176 |
+
}
|
| 177 |
+
""
|
| 178 |
+
|
| 179 |
+
# ================= INTERFACE GRADIO (compatible Gradio 6.0) =====================
|
| 180 |
+
|
| 181 |
+
avec gr.Blocks() à titre de démonstration :
|
| 182 |
+
avec gr.Row(hauteur_égale)=Vrai):
|
| 183 |
+
#Colonne gauche : Historique
|
| 184 |
+
avec gr.Column(scale=1, min_width=250, elem_classes="sidebar") :
|
| 185 |
+
gr.Réduction("## 📚 Historique")
|
| 186 |
+
nouveau_bouton_de_chat = gr.Bouton("➕ Nouvelle conversation",variante="secondaire")
|
| 187 |
+
histoire_radio = gr.Radio(
|
| 188 |
+
choix=[],
|
| 189 |
+
étiquette="Conversations",
|
| 190 |
+
interactif=Vrai
|
| 191 |
+
)
|
| 192 |
+
bouton_effacer=gr.Button("🗑️ Effacer",variante="stop", taille="sm"
|
| 193 |
+
|
| 194 |
+
# Colonne droite : Chat
|
| 195 |
+
avec gr.Column(échelle)=3, elem_classes="main-area"):
|
| 196 |
+
gr.Markdown("# 🤖 Assistant Élevage et Pêche")
|
| 197 |
+
|
| 198 |
+
chatbot = gr.Chatbot(
|
| 199 |
+
étiquette="",
|
| 200 |
+
hauteur=500,
|
| 201 |
+
afficher_étiquette=Faux,
|
| 202 |
+
avatar_images=(Aucun, "🐟")
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
avec gr.Rangée():
|
| 206 |
+
msg_input = gr.Textbox(
|
| 207 |
+
placeholder="Posez votre question sur l'élevage ou la pêche...",
|
| 208 |
+
afficher_étiquette=Faux,
|
| 209 |
+
échelle=9,
|
| 210 |
+
conteneur=Faux
|
| 211 |
+
)
|
| 212 |
+
envoyer_btn = gr.Button("📤", variante="primaire", échelle=1)
|
| 213 |
+
|
| 214 |
+
gr.Exemples(
|
| 215 |
+
examples=["C'est quoi l'élevage ?", "Explique la pêche", "Différence entre élevage et pêche ?"],
|
| 216 |
+
entrées=msg_input
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
#
|
| 220 |
+
conversations_state = gr.State([])
|
| 221 |
+
current_session_id = gr.State(str(uuid.uuid4()))
|
| 222 |
+
|
| 223 |
+
# ================= FONCTIONS =================
|
| 224 |
+
|
| 225 |
+
def créer_nouvelle_conversation(conversations):
|
| 226 |
+
"""Nouvelle conversation""
|
| 227 |
+
nouvel_id = str(uuid.uuid4())
|
| 228 |
+
conversations.append({"id": new_id, "title": "Nouveau chat", "messages": []})
|
| 229 |
+
choix = [c["titre"] pour c dans conversations]
|
| 230 |
+
renvoie conversations, nouvel_id, [], gr.update(choix=choix, valeur=None)
|
| 231 |
+
|
| 232 |
+
def charger_chat(titre_sélectionné, conversations):
|
| 233 |
+
"""Charger une conversation"""
|
| 234 |
+
si le titre sélectionné n'est pas sélectionné ou s'il ne s'agit pas de conversations :
|
| 235 |
+
retour [], ""
|
| 236 |
+
|
| 237 |
+
pour conv dans les conversations :
|
| 238 |
+
si conv["title"] == selected_title :
|
| 239 |
+
historique = []
|
| 240 |
+
user_msg = Aucun
|
| 241 |
+
pour msg dans conv["messages"] :
|
| 242 |
+
si msg["role"] == "user):
|
| 243 |
+
user_msg = msg["content"]
|
| 244 |
+
autre:
|
| 245 |
+
si user_msg :
|
| 246 |
+
histoire.append([user_msg, msg["content"]])
|
| 247 |
+
user_msg = Aucun
|
| 248 |
+
retourner l'historique, conv["id"]
|
| 249 |
+
retour [], ""
|
| 250 |
+
|
| 251 |
+
def envoyer_message(message, historique_chat, conversations, id_session) :
|
| 252 |
+
"""Envoyer un message"""
|
| 253 |
+
si ce n'est pas un message ou si ce n'est pas message.strip() :
|
| 254 |
+
retourner "", chat_history, conversations, session_id, gr.update()
|
| 255 |
+
|
| 256 |
+
# Gérer la session
|
| 257 |
+
si session_id n'est pas présent :
|
| 258 |
+
session_id = str(uuid.uuid4())
|
| 259 |
+
|
| 260 |
+
# Mettre à jour la conversation dans l'historique
|
| 261 |
+
conv_existe = Faux
|
| 262 |
+
pour conv dans les conversations :
|
| 263 |
+
si conv["id"] == session_id :
|
| 264 |
+
conv_existe = Vrai
|
| 265 |
+
conv["messages"].append({"role": "user", "content": message})
|
| 266 |
+
si conv["title"] == "Nouveau chat":
|
| 267 |
+
conv["title"] = message[:40] + "..."
|
| 268 |
+
casser
|
| 269 |
+
|
| 270 |
+
si conv_exists n'existe pas :
|
| 271 |
+
conversations.append({
|
| 272 |
+
"id": session_id,
|
| 273 |
+
"titre": message[:40] + "...",
|
| 274 |
+
"messages": [{"role": "utilisateur", "content": message}]
|
| 275 |
+
})
|
| 276 |
+
|
| 277 |
+
# Ajouter le message au chatbot
|
| 278 |
+
chat_history.append([message, None])
|
| 279 |
+
|
| 280 |
+
essayer:
|
| 281 |
+
résultat = convers_chain.invoke(
|
| 282 |
+
{"entrée": message},
|
| 283 |
+
config={"configurable": {"session_id": session_id}}
|
| 284 |
+
)
|
| 285 |
+
réponse = result.get("answer", "Désolé, je n'ai pas compris.")
|
| 286 |
+
sauf Exception comme e :
|
| 287 |
+
réponse = f"❌ Erreur : {str(e)}"
|
| 288 |
+
|
| 289 |
+
# Sauvegarder la réponse
|
| 290 |
+
pour conv dans les conversations :
|
| 291 |
+
si conv["id"] == session_id :
|
| 292 |
+
conv["messages"].append({"role": "assistant", "content": response})
|
| 293 |
+
casser
|
| 294 |
+
|
| 295 |
+
# Mettre à jour le chatbot
|
| 296 |
+
chat_history[-1] = [message, réponse]
|
| 297 |
+
|
| 298 |
+
# Mettre à jour la liste
|
| 299 |
+
choix = [c["titre"] pour c dans conversations]
|
| 300 |
+
|
| 301 |
+
renvoie "", chat_history, conversations, session_id, gr.update(choices=choices, value=conversations[-1]["title"] si conversations sinon None)
|
| 302 |
+
|
| 303 |
+
def clear_all():
|
| 304 |
+
"""Tout effacer"""
|
| 305 |
+
magasin.effacer()
|
| 306 |
+
renvoie [], [], [], gr.update(choices=[])
|
| 307 |
+
|
| 308 |
+
# ================= ÉVÉNEMENTS =================
|
| 309 |
+
|
| 310 |
+
msg_input.submit(
|
| 311 |
+
envoyer_message,
|
| 312 |
+
entrées=[msg_input, chatbot, conversations_state, current_session_id],
|
| 313 |
+
outputs=[msg_input, chatbot, conversations_state, current_session_id, history_radio]
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
envoyer_btn.cliquer(
|
| 317 |
+
envoyer_message,
|
| 318 |
+
entrées=[msg_input, chatbot, conversations_state, current_session_id],
|
| 319 |
+
outputs=[msg_input, chatbot, conversations_state, current_session_id, history_radio]
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
nouveau_bouton_de_chat.cliquer(
|
| 323 |
+
créer_une_nouvelle_conversation,
|
| 324 |
+
entrées=[état_des_conversations],
|
| 325 |
+
outputs=[conversations_state, current_session_id, chatbot, history_radio]
|
| 326 |
+
)
|
| 327 |
+
|
| 328 |
+
histoire_radio.changer(
|
| 329 |
+
charger_chat,
|
| 330 |
+
entrées=[history_radio, conversations_state],
|
| 331 |
+
sorties=[chatbot, id_session_actuelle]
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
effacer_btn.cliquer(
|
| 335 |
+
effacer_tout,
|
| 336 |
+
outputs=[chatbot, conversations_state, current_session_id, history_radio]
|
| 337 |
+
)
|
| 338 |
+
|
| 339 |
+
# ================= LANCEMENT ====================
|
| 340 |
+
|
| 341 |
+
si __name__ == "__main__":
|
| 342 |
+
print("🚀Lancement de l'application...")
|
| 343 |
+
démo.lancement(
|
| 344 |
+
nom_serveur="0.0.0.0",
|
| 345 |
+
port_serveur=7860,
|
| 346 |
+
css=custom_css,
|
| 347 |
+
thème="doux"
|
| 348 |
+
)
|