chandralegend commited on
Commit
31bcbcd
1 Parent(s): 9564722

table added for guidlines

Browse files
Files changed (1) hide show
  1. app.py +71 -34
app.py CHANGED
@@ -4,37 +4,79 @@ import os
4
  import re
5
  import ast
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  openai.api_key = os.getenv("OPENAI_API_KEY")
8
 
9
  SYSTEM_PROMPT = "You are a smart and intelligent Named Entity Recognition (NER) system. I will provide you the definition of the entities you need to extract, the sentence from where your extract the entities and the output format with examples."
10
-
11
  USER_PROMPT_1 = "Are you clear about your role?"
12
-
13
  ASSISTANT_PROMPT_1 = "Sure, I'm ready to help you with your NER task. Please provide me with the necessary information to get started."
14
- GUIDELINES_PROMPT = (
15
- "Entity Definition:\n"
16
- "1. PERSON: Short name or full name of a person from any geographic regions.\n"
17
- "2. DATE: Any format of dates. Dates can also be in natural language.\n"
18
- "3. LOC: Name of any geographic location, like cities, countries, continents, districts etc.\n"
19
- "\n"
20
- "Output Format:\n"
21
- "{{'PERSON': [list of entities present], 'DATE': [list of entities present], 'LOC': [list of entities present]}}\n"
22
- "If no entities are presented in any categories keep it None\n"
23
- "\n"
24
- "Examples:\n"
25
- "\n"
26
- "1. Sentence: Mr. Jacob lives in Madrid since 12th January 2015.\n"
27
- "Output: {{'PERSON': ['Mr. Jacob'], 'DATE': ['12th January 2015'], 'LOC': ['Madrid']}}\n"
28
- "\n"
29
- "2. Sentence: Mr. Rajeev Mishra and Sunita Roy are friends and they meet each other on 24/03/1998.\n"
30
- "Output: {{'PERSON': ['Mr. Rajeev Mishra', 'Sunita Roy'], 'DATE': ['24/03/1998'], 'LOC': ['None']}}\n"
31
- "\n"
32
- "3. Sentence: {}\n"
33
- "Output: "
34
- )
35
-
36
-
37
- COLORED_ENTITY = {"PERSON": "red", "DATE": "blue", "LOC": "green"}
38
 
39
 
40
  def openai_chat_completion_response(final_prompt):
@@ -54,22 +96,17 @@ def openai_chat_completion_response(final_prompt):
54
  my_sentence = st.text_input("Your Sentence")
55
  if st.button("Submit"):
56
  GUIDELINES_PROMPT = GUIDELINES_PROMPT.format(my_sentence)
 
57
  ners = openai_chat_completion_response(GUIDELINES_PROMPT)
58
  ners_dictionary = ast.literal_eval(ners)
59
  for entity_type, entity_list in ners_dictionary.items():
60
  entity_list = list(set(entity_list))
 
61
  for ent in entity_list:
62
  if ent != "None":
63
  my_sentence = re.sub(
64
  ent,
65
- ":"
66
- + COLORED_ENTITY[entity_type]
67
- + "["
68
- + ent
69
- + "\["
70
- + entity_type
71
- + "\]"
72
- + "]",
73
  my_sentence,
74
  )
75
  st.markdown(my_sentence)
 
4
  import re
5
  import ast
6
 
7
+ st.title("Named Entity Recognition (NER) with GPT-3")
8
+
9
+ if "guidelines" not in st.session_state:
10
+ st.session_state["guidelines"] = [
11
+ {
12
+ "entity": "PERSON",
13
+ "definition": "Short name or full name of a person from any geographic regions.",
14
+ "color": "red",
15
+ },
16
+ {
17
+ "entity": "DATE",
18
+ "definition": "Any format of dates. Dates can also be in natural language.",
19
+ "color": "green",
20
+ },
21
+ {
22
+ "entity": "LOC",
23
+ "definition": "Name of any geographic location, like cities, countries, continents, districts etc.",
24
+ "color": "blue",
25
+ },
26
+ ]
27
+
28
+
29
+ st.header("Guidelines")
30
+
31
+ # display guidelines in a table
32
+ st.table(st.session_state["guidelines"])
33
+
34
+ st.write("You can add new guidelines here.")
35
+ new_entity = st.text_input("Entity")
36
+ new_definition = st.text_input("Definition")
37
+ color = st.color_picker("Pick a color", "#00f900")
38
+ if st.button("Add Guideline"):
39
+ st.session_state["guidelines"].append(
40
+ {"entity": new_entity, "definition": new_definition, "color": color}
41
+ )
42
+ new_entity = ""
43
+ new_definition = ""
44
+
45
+
46
+ examples = [
47
+ {
48
+ "sentence": "Mr. Jacob lives in Madrid since 12th January 2015.",
49
+ "output": "{{'PERSON': ['Mr. Jacob'], 'DATE': ['12th January 2015'], 'LOC': ['Madrid']}}",
50
+ },
51
+ {
52
+ "sentence": "Mr. Rajeev Mishra and Sunita Roy are friends and they meet each other on 24/03/1998.",
53
+ "output": "{{'PERSON': ['Mr. Rajeev Mishra', 'Sunita Roy'], 'DATE': ['24/03/1998'], 'LOC': ['None']}}",
54
+ },
55
+ ]
56
+
57
+
58
+ def generate_guidelines_prompt(guidelines):
59
+ guidelines_prompt = "Entity Definition:\n"
60
+ for guideline in guidelines:
61
+ guidelines_prompt += f"{guideline['entity']}: {guideline['definition']}\n"
62
+ guidelines_prompt += "\nOutput Format:\n"
63
+ guidelines_prompt += "{{'PERSON': [list of entities present], 'DATE': [list of entities present], 'LOC': [list of entities present]}}\n"
64
+ guidelines_prompt += "If no entities are presented in any categories keep it None\n"
65
+ guidelines_prompt += "\nExamples:\n\n"
66
+ for i, example in enumerate(examples):
67
+ guidelines_prompt += f"{i+1}. Sentence: {example['sentence']}\n"
68
+ guidelines_prompt += f"Output: {example['output']}\n\n"
69
+ guidelines_prompt += str(len(examples) + 1) + ". Sentence: {}\n"
70
+ guidelines_prompt += "Output: "
71
+ return guidelines_prompt
72
+
73
+
74
  openai.api_key = os.getenv("OPENAI_API_KEY")
75
 
76
  SYSTEM_PROMPT = "You are a smart and intelligent Named Entity Recognition (NER) system. I will provide you the definition of the entities you need to extract, the sentence from where your extract the entities and the output format with examples."
 
77
  USER_PROMPT_1 = "Are you clear about your role?"
 
78
  ASSISTANT_PROMPT_1 = "Sure, I'm ready to help you with your NER task. Please provide me with the necessary information to get started."
79
+ GUIDELINES_PROMPT = generate_guidelines_prompt(st.session_state["guidelines"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
 
82
  def openai_chat_completion_response(final_prompt):
 
96
  my_sentence = st.text_input("Your Sentence")
97
  if st.button("Submit"):
98
  GUIDELINES_PROMPT = GUIDELINES_PROMPT.format(my_sentence)
99
+ print(GUIDELINES_PROMPT)
100
  ners = openai_chat_completion_response(GUIDELINES_PROMPT)
101
  ners_dictionary = ast.literal_eval(ners)
102
  for entity_type, entity_list in ners_dictionary.items():
103
  entity_list = list(set(entity_list))
104
+ color = st.session_state["guidelines"][entity_type]["color"]
105
  for ent in entity_list:
106
  if ent != "None":
107
  my_sentence = re.sub(
108
  ent,
109
+ ":" + color + "[" + ent + "\[" + entity_type + "\]" + "]",
 
 
 
 
 
 
 
110
  my_sentence,
111
  )
112
  st.markdown(my_sentence)