import gradio as gr from agent import Agent, process_data, process_retrievers import torch from markdown_it import MarkdownIt import json def init_agent(): print("init agent ......") DATA_DIR = "data_websites" PATH_SAVE_CHUNKS = "chunks_saved.json" PATH_SAVE_CONTEXT = "chunks_with_context.json" PATH_IDX = "index_faiss_data_sh" PATH_IDX_CONTEXT = "index_faiss_context_sh" PATH_IDX_CONTEXT_AND_WT = "index_faiss_context_and_wt_sh" # embedding_model_names = ["Geotrend/distilbert-base-en-fr-cased"] embedding_model_names = [] use_context = False reformulation = False use_HyDE = False use_HyDE_cut = False use_context_and_wt = True ask_again = False TOP_K = 10 device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') list_dir, chunks = process_data(DATA_DIR, PATH_SAVE_CHUNKS, PATH_SAVE_CONTEXT, use_context_and_wt, use_context) embedding_models, BM25_retriever = process_retrievers(embedding_model_names, chunks, TOP_K, use_context, use_context_and_wt, PATH_IDX, PATH_IDX_CONTEXT, PATH_IDX_CONTEXT_AND_WT, device) agent = Agent(list_dir, chunks, embedding_models, BM25_retriever, TOP_K, reformulation, use_HyDE, use_HyDE_cut, ask_again) return agent def respond(agent, user_input, history, comments, likes): reply, sources = agent.get_a_reply(user_input) bot_response = str(reply) + "\n\n\nℹ️ *Retrouvez davantage d'informations sur la page du site internet de l'université*:\n"+ "\n".join(f"- [{src}]({src})" for src in sources) # bot_response = f"Echo: {user_input}" history.append(("user", user_input)) history.append(("bot", bot_response)) return agent, "", update_html(history, comments, likes), history, comments def add_comment(comment_input, comments, history, likes): com = comment_input.split(" : ") message_index = int(com[0]) comment_text = com[1] if message_index in comments: comments[message_index] += [comment_text] else: comments[message_index] = [comment_text] print(data) return update_html(history, comments, likes), comments def handle_feedback(history, comments, feedback, likes): com = feedback.split(" : ") message_index = int(com[0]) like_val = int(com[1]) if message_index != -1: if message_index in likes and likes[message_index] == like_val: # likes[message_index] = 0 likes.pop(message_index) else: likes[message_index] = like_val print(data) feedback = "-1 : 0" return update_html(history, comments, likes), feedback, likes def update_html(history, comments, likes): like_empty = """ """ unlike_empty = """ """ like_filled = """ """ unlike_filled = """ """ send = """ """ html = "" md = MarkdownIt() for i, (sender, msg) in reversed(list(enumerate(history))): if sender == "user": html += f'
Ce chatbot répond à des questions administratives de l'Université de Bordeaux.
Les réponses seront enregistrées dans le cadre d'une étude d'amélioration de cet outil.