import gradio as gr from EurLexChat import EurLexChat import random import string from config import CONFIG, UI_USER, UI_PWD from consts import JUSTICE_CELEXES, POLLUTION_CELEXES from enum import Enum from copy import deepcopy def generate_random_string(length): # Generate a random string of the specified length # using letters and numbers characters = string.ascii_letters + string.digits random_string = ''.join(random.choice(characters) for _ in range(length)) return random_string class ChatBot(): def __init__(self, config) -> None: self.documents = [] self.config = deepcopy(config) self.chat = EurLexChat(config=self.config) def __deepcopy__(self, _): return ChatBot(self.config) class Versions(Enum): AKN='Akoma Ntoso' JUSTICE='Organisation of the legal system (1226) eurovoc' POLLUTION='Pollution (2524) eurovoc' BASIC='All eurovoc' chat_init = EurLexChat(config=CONFIG) justice_ids = chat_init.get_ids_from_celexes(JUSTICE_CELEXES) pollution_ids = chat_init.get_ids_from_celexes(POLLUTION_CELEXES) def reinit(version, bot): bot.documents = [] config = deepcopy(CONFIG) if version == Versions.AKN.value: config['vectorDB']['kwargs']['collection_name'] += "-akn" bot.chat = EurLexChat(config=config) return clean_page(bot) def remove_doc(btn, bot): bot.documents.pop(btn) new_accordions, new_texts = set_new_docs_ui(bot.documents) return [*new_accordions, *new_texts, bot] def get_answer(message, history, session_id, bot): s = session_id celex_type = Versions.BASIC.value if celex_type == Versions.JUSTICE.value: ids_list = justice_ids elif celex_type == Versions.POLLUTION.value: ids_list = pollution_ids elif celex_type == Versions.BASIC.value or celex_type == Versions.AKN.value: ids_list = None else: raise ValueError(f'Wrong celex_type: {celex_type}') if len(history) == 0: bot.documents = [] #docs.documents = chat.get_relevant_docs(question=message, ids_list=ids_list) s = generate_random_string(7) result = bot.chat.get_answer(s, message, bot.documents, ids_list=ids_list) history.append((message, result.answer)) if result.new_documents: bot.documents = result.new_documents accordions, list_texts = set_new_docs_ui(bot.documents) return ['', history, gr.Column(scale=1, visible=True), *accordions, *list_texts, s, bot] def set_new_docs_ui(documents): new_accordions = [] new_texts = [] for i in range(len(accordions)): if i < len(documents): new_accordions.append(gr.update(accordions[i].elem_id, label=f"{documents[i]['celex']}: {documents[i]['text'][:40]}...", visible=True, open=False)) new_texts.append(gr.update(list_texts[i].elem_id, value=f"{documents[i]['text']}...", visible=True)) else: new_accordions.append(gr.update(accordions[i].elem_id, label="", visible=False)) new_texts.append(gr.update(list_texts[i].elem_id, value="", visible=False)) return new_accordions, new_texts def clean_page(bot): bot.documents = [] accordions, list_texts = set_new_docs_ui(bot.documents) return ["", [], None, *accordions, *list_texts, gr.Column(visible=False), bot] list_texts = [] accordions = [] states = [] delete_buttons = [] if CONFIG['vectorDB'].get('rerank'): n_context_docs = CONFIG['vectorDB']['rerank']['kwargs']['top_n'] else: n_context_docs = CONFIG['vectorDB']['retriever_args']['search_kwargs']['k'] block = gr.Blocks() with block: gr.Markdown("""
Chat-EUR-Lex prototype is a limited risk AI system realized by Aptus.AI and the Institute of Legal Informatics and Judicial Systems (IGSG-CNR). The prototype is an AI chatbot, therefore you are interacting with a machine, not with a human person. The prototype uses Aptus.AI RAG system and OpenAI GPT-4o language model.
Chat-EUR-Lex project is funded by the European Union within the framework of the NGI Search project under grant agreement No 101069364. Views and opinions expressed are however those of the author(s) only and do not necessarily reflect those of the European Union or European Commission. Contact us: chat-eur-lex@igsg.cnr.it.