Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,107 +1,112 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
import io
|
| 3 |
-
import pandas as pd
|
| 4 |
-
from langchain_experimental.agents.agent_toolkits import create_csv_agent
|
| 5 |
-
from langchain_anthropic import ChatAnthropic
|
| 6 |
-
import gradio as gr
|
| 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 |
demo.launch(inbrowser=True)
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import io
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from langchain_experimental.agents.agent_toolkits import create_csv_agent
|
| 5 |
+
from langchain_anthropic import ChatAnthropic
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
load_dotenv()
|
| 11 |
+
|
| 12 |
+
print(os.getenv("ANTHROPIC_API_KEY"))
|
| 13 |
+
# Configuration du modèle Anthropic
|
| 14 |
+
llm = ChatAnthropic(
|
| 15 |
+
model="claude-3-7-sonnet-20250219",#claude-3-5-sonnet-20241022",
|
| 16 |
+
temperature=0.1,
|
| 17 |
+
api_key=os.getenv("ANTHROPIC_API_KEY")
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Liste des fichiers CSV disponibles
|
| 21 |
+
items = [
|
| 22 |
+
"Contacts Prescripteurs-Ensemble des données.csv",
|
| 23 |
+
"Lieux-Ensemble des données.csv",
|
| 24 |
+
"Thèmes de séjour-Ensemble des données.csv"
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
# Fonction pour interroger les données CSV
|
| 28 |
+
def query_data(query, csv_file_path):
|
| 29 |
+
# Rediriger la sortie standard vers un buffer
|
| 30 |
+
old_stdout = sys.stdout
|
| 31 |
+
new_stdout = io.StringIO()
|
| 32 |
+
sys.stdout = new_stdout
|
| 33 |
+
|
| 34 |
+
# Créer l'agent et invoquer la requête
|
| 35 |
+
agent = create_csv_agent(
|
| 36 |
+
llm,
|
| 37 |
+
csv_file_path,
|
| 38 |
+
verbose=True,
|
| 39 |
+
return_intermediate_steps=True,
|
| 40 |
+
max_iterations=8,
|
| 41 |
+
allow_dangerous_code=True,
|
| 42 |
+
handle_parsing_errors=True,
|
| 43 |
+
include_df_in_prompt=False,
|
| 44 |
+
# number_of_head_rows=1
|
| 45 |
+
)
|
| 46 |
+
try:
|
| 47 |
+
response = agent.invoke(query)
|
| 48 |
+
# Récupérer la sortie de la console
|
| 49 |
+
console_output = new_stdout.getvalue()
|
| 50 |
+
|
| 51 |
+
# Restaurer la sortie standard
|
| 52 |
+
sys.stdout = old_stdout
|
| 53 |
+
print(response)
|
| 54 |
+
# Retourner la réponse et la sortie de la console
|
| 55 |
+
return {
|
| 56 |
+
"output": response["output"],
|
| 57 |
+
"console_output": response['intermediate_steps']
|
| 58 |
+
}
|
| 59 |
+
except Exception as e:
|
| 60 |
+
console_output = new_stdout.getvalue()
|
| 61 |
+
|
| 62 |
+
# Restaurer la sortie standard
|
| 63 |
+
sys.stdout = old_stdout
|
| 64 |
+
# Retourner la réponse et la sortie de la console
|
| 65 |
+
return {
|
| 66 |
+
"output": f"**Error :** {e}",
|
| 67 |
+
"console_output": console_output
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# Fonction de chat
|
| 73 |
+
def chat(question, csv_file_path, chat_history):
|
| 74 |
+
csv_file_path +=".csv"
|
| 75 |
+
question=f"df.columns : \n{pd.read_csv(csv_file_path).columns}\n\n"+question
|
| 76 |
+
print(question)
|
| 77 |
+
# Appeler la fonction query_data
|
| 78 |
+
result = query_data(question, csv_file_path)
|
| 79 |
+
|
| 80 |
+
# Ajouter la question, la réponse finale et la sortie de la console à l'historique du chat
|
| 81 |
+
chat_history.append((question, f"**Réflexion de l'IA :**\n{result['console_output']}\n\n**Réponse finale**:\n{result['output']}"))
|
| 82 |
+
|
| 83 |
+
# Retourner l'historique mis à jour
|
| 84 |
+
return chat_history
|
| 85 |
+
|
| 86 |
+
# Création de l'interface Gradio
|
| 87 |
+
with gr.Blocks() as demo:
|
| 88 |
+
gr.Markdown("""# Your airtable assistant
|
| 89 |
+
Les conversations ne sont pas encore disponibles.""")
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
chatbot = gr.Chatbot(label="Chat") # Composant Chatbot pour afficher les messages
|
| 93 |
+
|
| 94 |
+
with gr.Row(): # Une ligne pour organiser les composants côte à côte
|
| 95 |
+
with gr.Column(): # Colonne pour la question
|
| 96 |
+
question = gr.Textbox(label="Posez votre question")
|
| 97 |
+
submit_btn = gr.Button("Envoyer")
|
| 98 |
+
with gr.Column(): # Colonne pour le dropdown
|
| 99 |
+
dropdown = gr.Dropdown(choices=[elt[:-4] for elt in items], label="Sélectionnez une table")
|
| 100 |
+
|
| 101 |
+
# Initialiser l'historique du chat
|
| 102 |
+
chat_history = gr.State([]) # Utilisé pour stocker l'historique des messages
|
| 103 |
+
|
| 104 |
+
# Lier les composants à la fonction de chat
|
| 105 |
+
submit_btn.click(
|
| 106 |
+
chat, # Fonction à appeler
|
| 107 |
+
inputs=[question, dropdown, chat_history], # Entrées
|
| 108 |
+
outputs=chatbot # Sortie
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Lancer l'interface
|
| 112 |
demo.launch(inbrowser=True)
|