chandralegend
commited on
Commit
•
8dcfc9b
1
Parent(s):
167eebb
added the initial implementation
Browse files- .env +1 -0
- .vscode/settings.json +6 -0
- app.py +75 -0
- requirements.txt +2 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
1 |
+
OPENAI_API_KEY=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
|
.vscode/settings.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"[python]": {
|
3 |
+
"editor.defaultFormatter": "ms-python.black-formatter"
|
4 |
+
},
|
5 |
+
"python.formatting.provider": "none"
|
6 |
+
}
|
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
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):
|
41 |
+
response = openai.ChatCompletion.create(
|
42 |
+
model="gpt-3.5-turbo",
|
43 |
+
messages=[
|
44 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
45 |
+
{"role": "user", "content": USER_PROMPT_1},
|
46 |
+
{"role": "assistant", "content": ASSISTANT_PROMPT_1},
|
47 |
+
{"role": "user", "content": final_prompt},
|
48 |
+
],
|
49 |
+
)
|
50 |
+
|
51 |
+
return response["choices"][0]["message"]["content"].strip(" \n")
|
52 |
+
|
53 |
+
|
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)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
openai
|