EC2 Default User commited on
Commit
f7ca36c
1 Parent(s): 83bdafc
Files changed (2) hide show
  1. app.py +47 -1
  2. requirements.txt +0 -1
app.py CHANGED
@@ -1,4 +1,7 @@
1
  import streamlit as st
 
 
 
2
  import logging
3
 
4
  # Render Streamlit page
@@ -9,8 +12,51 @@ st.markdown(
9
  # Configure logger
10
  logging.basicConfig(format="\n%(asctime)s\n%(message)s", level=logging.INFO, force=True)
11
 
 
12
  def anonymise_text(text: str, metadata: str = "", white_listed_words: str = ""):
13
- st.session_state.text_anon = "this is anonymised"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  if "text" not in st.session_state:
16
  st.session_state.text = ""
 
1
  import streamlit as st
2
+ from flair.data import Sentence
3
+ from flair.models import SequenceTagger
4
+ import re
5
  import logging
6
 
7
  # Render Streamlit page
 
12
  # Configure logger
13
  logging.basicConfig(format="\n%(asctime)s\n%(message)s", level=logging.INFO, force=True)
14
 
15
+ # # Define functions
16
  def anonymise_text(text: str, metadata: str = "", white_listed_words: str = ""):
17
+ """anonymise text"""
18
+ if st.session_state.n_requests >= 5:
19
+ st.session_state.text_error = "Too many requests. Please wait a few seconds before anonymising more text."
20
+ logging.info(f"Session request limit reached: {st.session_state.n_requests}")
21
+ st.session_state.n_requests = 1
22
+ return
23
+
24
+ st.session_state.text = ""
25
+ st.session_state.text_error = ""
26
+
27
+ if not text:
28
+ st.session_state.text_error = "Please enter your text"
29
+ return
30
+
31
+ with text_spinner_placeholder:
32
+ with st.spinner("Please wait while your text is being anonymised..."):
33
+
34
+ # flagged = openai.moderate(prompt)
35
+ # if flagged:
36
+ # st.session_state.text_error = "Input flagged as inappropriate."
37
+ # logging.info(f"Topic: {topic}{mood_output}{style_output}\n")
38
+ # return
39
+
40
+ # else:
41
+ # load tagger
42
+ tagger = SequenceTagger.load("flair/ner-english-large")
43
+ sentence = Sentence(text)
44
+ # predict NER tags
45
+ tagger.predict(sentence)
46
+ # iterate over entities and redact
47
+ enitities=[e.text for e in sentence.get_spans('ner')]
48
+ regex = re.compile('|'.join(map(re.escape, enitities)))
49
+ text_anon = regex.sub("<PID>", text)
50
+
51
+ st.session_state.text_error = ""
52
+ st.session_state.n_requests += 1
53
+ st.session_state.text_anon = text_anon
54
+ logging.info(
55
+ f"text: {text}{metadata}{white_listed_words}\n"
56
+ f"text anonymised: {st.session_state.text_anon}"
57
+ )
58
+ # def anonymise_text(text: str, metadata: str = "", white_listed_words: str = ""):
59
+ # st.session_state.text_anon = "this is anonymised"
60
 
61
  if "text" not in st.session_state:
62
  st.session_state.text = ""
requirements.txt CHANGED
@@ -1,2 +1 @@
1
- transformers
2
  flair==0.11
 
 
1
  flair==0.11