added main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import spacy
|
| 3 |
+
from spacy import displacy
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
api_key = os.getenv("API_KEY")
|
| 10 |
+
|
| 11 |
+
API_URL = "https://api-inference.huggingface.co/models/cleopatro/Entity_Rec"
|
| 12 |
+
headers = {"Authorization": f"Bearer {api_key}"}
|
| 13 |
+
NER = spacy.load("en_core_web_sm")
|
| 14 |
+
|
| 15 |
+
def extract_word_and_entity_group(dict):
|
| 16 |
+
words = []
|
| 17 |
+
result = []
|
| 18 |
+
|
| 19 |
+
for item in dict:
|
| 20 |
+
word = item['word']
|
| 21 |
+
words.append(word)
|
| 22 |
+
|
| 23 |
+
return words
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_abs(payload):
|
| 27 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 28 |
+
return response.json()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def get_loc_time(sentence):
|
| 32 |
+
text1 = NER(sentence)
|
| 33 |
+
locations = []
|
| 34 |
+
times = []
|
| 35 |
+
for ent in text1.ents:
|
| 36 |
+
if ent.label_ == "GPE" or ent.label_ == "LOC":
|
| 37 |
+
locations.append(ent.text)
|
| 38 |
+
elif ent.label_ == "TIME" or ent.label_ == "DATE":
|
| 39 |
+
times.append(ent.text)
|
| 40 |
+
return locations, times
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_ent(sentence):
|
| 44 |
+
abs_dict = get_abs(sentence)
|
| 45 |
+
abs_tags = extract_word_and_entity_group(abs_dict)
|
| 46 |
+
loc_tags, time_tags = get_loc_time(sentence["inputs"])
|
| 47 |
+
return abs_tags, loc_tags, time_tags
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
output = get_ent({
|
| 52 |
+
"inputs": "today stock prices and home loans are a pain in san fransisco.",
|
| 53 |
+
})
|
| 54 |
+
|
| 55 |
+
print(output)
|