MattiaSangermano commited on
Commit
8e023cd
1 Parent(s): e34a2a6

Fixed bug in the ui

Browse files
Files changed (1) hide show
  1. app.py +26 -24
app.py CHANGED
@@ -5,7 +5,7 @@ import string
5
  from config import CONFIG, UI_USER, UI_PWD
6
  from consts import JUSTICE_CELEXES, POLLUTION_CELEXES
7
  from enum import Enum
8
- import regex as re
9
 
10
  def generate_random_string(length):
11
  # Generate a random string of the specified length
@@ -15,9 +15,12 @@ def generate_random_string(length):
15
  return random_string
16
 
17
  class ChatBot():
18
- def __init__(self) -> None:
19
  self.documents = []
20
- self.chat = EurLexChat(config=CONFIG)
 
 
 
21
 
22
  class Versions(Enum):
23
  AKN='Akoma Ntoso'
@@ -26,28 +29,26 @@ class Versions(Enum):
26
  BASIC='All eurovoc'
27
 
28
 
29
- bot = ChatBot()
 
 
30
 
31
- justice_ids = bot.chat.get_ids_from_celexes(JUSTICE_CELEXES)
32
- pollution_ids = bot.chat.get_ids_from_celexes(POLLUTION_CELEXES)
33
 
34
-
35
- def reinit(version):
36
  bot.documents = []
 
37
  if version == Versions.AKN.value:
38
- CONFIG['vectorDB']['kwargs']['collection_name'] += "-akn"
39
- else:
40
- CONFIG['vectorDB']['kwargs']['collection_name'] = re.sub(r'-akn$', '', CONFIG['vectorDB']['kwargs']['collection_name'])
41
- bot.chat = EurLexChat(config=CONFIG)
42
- return clean_page()
43
 
44
- def remove_doc(btn):
45
  bot.documents.pop(btn)
46
  new_accordions, new_texts = set_new_docs_ui(bot.documents)
47
- return [*new_accordions, *new_texts]
48
 
49
 
50
- def get_answer(message, history, session_id, celex_type):
51
  s = session_id
52
  if celex_type == Versions.JUSTICE.value:
53
  ids_list = justice_ids
@@ -67,7 +68,7 @@ def get_answer(message, history, session_id, celex_type):
67
  if result.new_documents:
68
  bot.documents = result.new_documents
69
  accordions, list_texts = set_new_docs_ui(bot.documents)
70
- return ['', history, gr.Column(scale=1, visible=True), *accordions, *list_texts, s]
71
 
72
 
73
  def set_new_docs_ui(documents):
@@ -83,10 +84,10 @@ def set_new_docs_ui(documents):
83
  return new_accordions, new_texts
84
 
85
 
86
- def clean_page():
87
  bot.documents = []
88
  accordions, list_texts = set_new_docs_ui(bot.documents)
89
- return ["", [], None, *accordions, *list_texts, gr.Column(visible=False)]
90
 
91
  list_texts = []
92
  accordions = []
@@ -104,6 +105,7 @@ with block:
104
  gr.Markdown("""
105
  <h1><center>Chat-EUR-Lex prototype - Alpha version</center></h1>
106
  """)
 
107
  state = gr.State(value=None)
108
  with gr.Row():
109
  with gr.Column(scale=3):
@@ -138,11 +140,11 @@ with block:
138
  Contact us: <a href="mailto:chat-eur-lex@igsg.cnr.it">chat-eur-lex@igsg.cnr.it</a>.</p>
139
  </div>""")
140
 
141
- drop_down.change(reinit, inputs=[drop_down], outputs=[message, chatbot, state, *accordions, *list_texts, col])
142
- clear.click(clean_page, outputs=[message, chatbot, state, *accordions, *list_texts, col])
143
- message.submit(get_answer, inputs=[message, chatbot, state, drop_down], outputs=[message, chatbot, col, *accordions, *list_texts, state])
144
- submit.click(get_answer, inputs=[message, chatbot, state, drop_down], outputs=[message, chatbot, col, *accordions, *list_texts, state])
145
  for i, b in enumerate(delete_buttons):
146
- b.click(remove_doc, inputs=states[i], outputs=[*accordions, *list_texts])
147
 
148
  block.launch(debug=True, auth=(UI_USER, UI_PWD))
 
5
  from config import CONFIG, UI_USER, UI_PWD
6
  from consts import JUSTICE_CELEXES, POLLUTION_CELEXES
7
  from enum import Enum
8
+ from copy import deepcopy
9
 
10
  def generate_random_string(length):
11
  # Generate a random string of the specified length
 
15
  return random_string
16
 
17
  class ChatBot():
18
+ def __init__(self, config) -> None:
19
  self.documents = []
20
+ self.config = deepcopy(config)
21
+ self.chat = EurLexChat(config=self.config)
22
+ def __deepcopy__(self, _):
23
+ return ChatBot(self.config)
24
 
25
  class Versions(Enum):
26
  AKN='Akoma Ntoso'
 
29
  BASIC='All eurovoc'
30
 
31
 
32
+ chat_init = EurLexChat(config=CONFIG)
33
+ justice_ids = chat_init.get_ids_from_celexes(JUSTICE_CELEXES)
34
+ pollution_ids = chat_init.get_ids_from_celexes(POLLUTION_CELEXES)
35
 
 
 
36
 
37
+ def reinit(version, bot):
 
38
  bot.documents = []
39
+ config = deepcopy(CONFIG)
40
  if version == Versions.AKN.value:
41
+ config['vectorDB']['kwargs']['collection_name'] += "-akn"
42
+ bot.chat = EurLexChat(config=config)
43
+ return clean_page(bot)
 
 
44
 
45
+ def remove_doc(btn, bot):
46
  bot.documents.pop(btn)
47
  new_accordions, new_texts = set_new_docs_ui(bot.documents)
48
+ return [*new_accordions, *new_texts, bot]
49
 
50
 
51
+ def get_answer(message, history, session_id, celex_type, bot):
52
  s = session_id
53
  if celex_type == Versions.JUSTICE.value:
54
  ids_list = justice_ids
 
68
  if result.new_documents:
69
  bot.documents = result.new_documents
70
  accordions, list_texts = set_new_docs_ui(bot.documents)
71
+ return ['', history, gr.Column(scale=1, visible=True), *accordions, *list_texts, s, bot]
72
 
73
 
74
  def set_new_docs_ui(documents):
 
84
  return new_accordions, new_texts
85
 
86
 
87
+ def clean_page(bot):
88
  bot.documents = []
89
  accordions, list_texts = set_new_docs_ui(bot.documents)
90
+ return ["", [], None, *accordions, *list_texts, gr.Column(visible=False), bot]
91
 
92
  list_texts = []
93
  accordions = []
 
105
  gr.Markdown("""
106
  <h1><center>Chat-EUR-Lex prototype - Alpha version</center></h1>
107
  """)
108
+ bot = gr.State(value=ChatBot(CONFIG))
109
  state = gr.State(value=None)
110
  with gr.Row():
111
  with gr.Column(scale=3):
 
140
  Contact us: <a href="mailto:chat-eur-lex@igsg.cnr.it">chat-eur-lex@igsg.cnr.it</a>.</p>
141
  </div>""")
142
 
143
+ drop_down.change(reinit, inputs=[drop_down, bot], outputs=[message, chatbot, state, *accordions, *list_texts, col])
144
+ clear.click(clean_page, inputs=[bot], outputs=[message, chatbot, state, *accordions, *list_texts, col, bot])
145
+ message.submit(get_answer, inputs=[message, chatbot, state, drop_down, bot], outputs=[message, chatbot, col, *accordions, *list_texts, state, bot])
146
+ submit.click(get_answer, inputs=[message, chatbot, state, drop_down, bot], outputs=[message, chatbot, col, *accordions, *list_texts, state, bot])
147
  for i, b in enumerate(delete_buttons):
148
+ b.click(remove_doc, inputs=[states[i],bot], outputs=[*accordions, *list_texts, bot])
149
 
150
  block.launch(debug=True, auth=(UI_USER, UI_PWD))