import gradio as gr from EurLexChat import EurLexChat import yaml import random import string import argparse import os openai_org_key = os.getenv("OPENAI_ORG_KEY") openai_key = os.getenv("OPENAI_KEY") ui_pwd = os.getenv("pwd") ui_user = os.getenv("user") qdrant_url=os.getenv("url") qdrant_key=os.getenv("qdrant_key") 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 Documents(): def __init__(self) -> None: self.documents = [] parser = argparse.ArgumentParser(description="Chat-eur-lex ui") parser.add_argument('--config_path', dest='config_path', metavar='config_path', type=str, help='The path to the config file that contains all the settings for the chat engine' , default='config.yaml') args = parser.parse_args() # Read config file with open(args.config_path, 'r') as file: config = yaml.safe_load(file) config["embeddings"]["kwargs"]["openai_api_key"] = openai_key config["embeddings"]["kwargs"]["openai_organization"] = openai_org_key config["llm"]["kwargs"]["openai_api_key"] = openai_key config["llm"]["kwargs"]["openai_organization"] = openai_org_key config["vectorDB"]["kwargs"]["url"] = qdrant_url config["vectorDB"]["kwargs"]["api_key"] = qdrant_key chat = EurLexChat(config=config) docs = Documents() def remove_doc(btn): docs.documents.pop(btn) new_accordions, new_texts = set_new_docs_ui(docs.documents) return [*new_accordions, *new_texts] def get_answer(message, history, session_id): s = session_id if len(history) == 0: docs.documents = chat.get_relevant_docs(question=message) s = generate_random_string(7) result = chat.get_answer(s, message, docs.documents) history.append((message, result.answer)) if result.new_documents: docs.documents = result.new_documents accordions, list_texts = set_new_docs_ui(docs.documents) return ['', history, gr.Column(scale=1, visible=True), *accordions, *list_texts, s] 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]['text'][:45]}...", 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(): docs.documents = [] accordions, list_texts = set_new_docs_ui(docs.documents) return ["", [], None, *accordions, *list_texts] list_texts = [] accordions = [] states = [] delete_buttons = [] block = gr.Blocks() with block: gr.Markdown("""
Chat-EUR-Lex prototype is a limited risk AI system realized by the Institute of Legal Informatics and Judicial Systems (IGSG-CNR) and Aptus.AI. The prototype is an AI chatbot, therefore you are interacting with a machine, not with a human person. The prototype uses OpenAI GPT-4 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.