CarlosMalaga commited on
Commit
16f0e9a
1 Parent(s): 6806e2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -26
app.py CHANGED
@@ -242,14 +242,19 @@ def set_intro(css):
242
  # "[Stefano Faralli](https://corsidilaurea.uniroma1.it/it/users/stefanofaralliuniroma1it), and [Roberto Navigli](https://www.diag.uniroma1.it/navigli/)."
243
  # )
244
 
 
245
  def write_candidates_to_file(text, candidates, selected_candidates):
246
- # Write the selected candidates to a file
247
- # Ensure the directory exists
248
- os.makedirs("logs", exist_ok=True)
249
- # Write the selected candidates to a file
250
- with open("logs/selected_candidates.tsv", "a") as file:
251
- file.write(f"{text}\t{str(candidates)}\t{str(selected_candidates)}\n")
252
-
 
 
 
 
253
 
254
  def run_client():
255
  with open(Path(__file__).parent / "style.css") as f:
@@ -361,39 +366,48 @@ def run_client():
361
  response = relik_model.retrieve(text, k=10, batch_size=400, progress_bar=False)
362
  candidates_text = [pred.document.text for pred in response[0]]
363
 
 
364
  if candidates_text:
365
- st.session_state.candidates = candidates_text[:10]
366
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
367
  else:
368
  st.error("Please enter some text.")
369
-
 
370
  if st.session_state.candidates:
371
  dict_of_ents_candidates, options_candidates = get_retriever_annotations_candidates(text, st.session_state.candidates)
372
-
373
  st.markdown("<h2 style='color: black;'>Possible Candidates:</h2>", unsafe_allow_html=True)
374
-
375
- # Display the candidates with checkboxes
376
  for candidate in dict_of_ents_candidates["ents"]:
377
- if candidate in st.session_state.selected_candidates:
378
- checked = True
379
- else:
380
- checked = False
381
  if st.checkbox(candidate, key=candidate, value=checked):
382
  if candidate not in st.session_state.selected_candidates:
383
  st.session_state.selected_candidates.append(candidate)
384
  else:
385
  if candidate in st.session_state.selected_candidates:
386
  st.session_state.selected_candidates.remove(candidate)
387
-
388
- # Button to save the selected candidates to a file
389
  if st.button("Save Selected Candidates"):
390
- write_candidates_to_file(text, dict_of_ents_candidates["ents"], st.session_state.selected_candidates)
391
- st.success("Selected candidates have been saved to file.")
392
-
393
- else:
394
- st.session_state.candidates = []
395
- st.session_state.selected_candidates = []
396
- st.markdown("<h2 style='color: black;'>No Candidates Found</h2>", unsafe_allow_html=True)
397
 
398
 
399
  if __name__ == "__main__":
 
242
  # "[Stefano Faralli](https://corsidilaurea.uniroma1.it/it/users/stefanofaralliuniroma1it), and [Roberto Navigli](https://www.diag.uniroma1.it/navigli/)."
243
  # )
244
 
245
+
246
  def write_candidates_to_file(text, candidates, selected_candidates):
247
+ try:
248
+ # Ensure the directory exists
249
+ os.makedirs("logs", exist_ok=True)
250
+ # Write the selected candidates to a file
251
+ with open("logs/selected_candidates.tsv", "a") as file:
252
+ file.write(f"{text}\t{str(candidates)}\t{str(selected_candidates)}\n")
253
+ return True
254
+ except Exception as e:
255
+ st.error(f"An error occurred while writing the file: {e}")
256
+ return False
257
+
258
 
259
  def run_client():
260
  with open(Path(__file__).parent / "style.css") as f:
 
366
  response = relik_model.retrieve(text, k=10, batch_size=400, progress_bar=False)
367
  candidates_text = [pred.document.text for pred in response[0]]
368
 
369
+
370
  if candidates_text:
371
+ st.session_state.candidates = candidates_text[:5]
372
+ dict_of_ents_candidates, options_candidates = get_retriever_annotations_candidates(text, st.session_state.candidates)
373
+ st.markdown("<h2 style='color: black;'>Possible Candidates:</h2>", unsafe_allow_html=True)
374
+
375
+ for candidate in dict_of_ents_candidates["ents"]:
376
+ checked = candidate in st.session_state.selected_candidates
377
+ if st.checkbox(candidate, key=candidate, value=checked):
378
+ if candidate not in st.session_state.selected_candidates:
379
+ st.session_state.selected_candidates.append(candidate)
380
+ else:
381
+ if candidate in st.session_state.selected_candidates:
382
+ st.session_state.selected_candidates.remove(candidate)
383
+
384
+ if st.button("Save Selected Candidates"):
385
+ if write_candidates_to_file(text, dict_of_ents_candidates["ents"], st.session_state.selected_candidates):
386
+ st.success("Selected candidates have been saved to file.")
387
+ else:
388
+ st.session_state.candidates = []
389
+ st.session_state.selected_candidates = []
390
+ st.markdown("<h2 style='color: black;'>No Candidates Found</h2>", unsafe_allow_html=True)
391
+
392
  else:
393
  st.error("Please enter some text.")
394
+
395
+ # Ensure the candidates list is displayed even after interactions
396
  if st.session_state.candidates:
397
  dict_of_ents_candidates, options_candidates = get_retriever_annotations_candidates(text, st.session_state.candidates)
 
398
  st.markdown("<h2 style='color: black;'>Possible Candidates:</h2>", unsafe_allow_html=True)
 
 
399
  for candidate in dict_of_ents_candidates["ents"]:
400
+ checked = candidate in st.session_state.selected_candidates
 
 
 
401
  if st.checkbox(candidate, key=candidate, value=checked):
402
  if candidate not in st.session_state.selected_candidates:
403
  st.session_state.selected_candidates.append(candidate)
404
  else:
405
  if candidate in st.session_state.selected_candidates:
406
  st.session_state.selected_candidates.remove(candidate)
407
+
 
408
  if st.button("Save Selected Candidates"):
409
+ if write_candidates_to_file(text, dict_of_ents_candidates["ents"], st.session_state.selected_candidates):
410
+ st.success("Selected candidates have been saved to file.")
 
 
 
 
 
411
 
412
 
413
  if __name__ == "__main__":