Instructions to use cleopatro/context_tracking with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use cleopatro/context_tracking with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("token-classification", model="cleopatro/context_tracking")# Load model directly from transformers import AutoTokenizer, AutoModelForTokenClassification tokenizer = AutoTokenizer.from_pretrained("cleopatro/context_tracking") model = AutoModelForTokenClassification.from_pretrained("cleopatro/context_tracking", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download main.py from cleopatro/context_tracking: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/cleopatro/context_tracking/resolve/main/main.py
- Command line
-
hf download hf://cleopatro/context_tracking/main.py
-
curl -L -o main.py https://huggingface.co/cleopatro/context_tracking/resolve/main/main.py
1.49 kB
| import requests | |
| import spacy | |
| from spacy import displacy | |
| from dotenv import load_dotenv | |
| import os | |
| from stm import ShortTermMemory | |
| load_dotenv() | |
| api_key = os.getenv("API_KEY") | |
| API_URL = "https://api-inference.huggingface.co/models/cleopatro/Entity_Rec" | |
| headers = {"Authorization": f"Bearer {api_key}"} | |
| NER = spacy.load("en_core_web_sm") | |
| def extract_word_and_entity_group(dict): | |
| words = [] | |
| result = [] | |
| for item in dict: | |
| word = item['word'] | |
| words.append(word) | |
| return words | |
| def get_abs(payload): | |
| response = requests.post(API_URL, headers=headers, json=payload) | |
| return response.json() | |
| def get_loc_time(sentence): | |
| text1 = NER(sentence) | |
| locations = [] | |
| times = [] | |
| for ent in text1.ents: | |
| if ent.label_ == "GPE" or ent.label_ == "LOC": | |
| locations.append(ent.text) | |
| elif ent.label_ == "TIME" or ent.label_ == "DATE": | |
| times.append(ent.text) | |
| return locations, times | |
| def get_ent(sentence): | |
| abs_dict = get_abs(sentence) | |
| abs_tags = extract_word_and_entity_group(abs_dict) | |
| loc_tags, time_tags = get_loc_time(sentence["inputs"]) | |
| return abs_tags, loc_tags, time_tags | |
| # output = get_ent({ | |
| # "inputs": "today stock prices and home loans are a pain in san fransisco.", | |
| # }) | |
| # print(output) | |
| # stm = ShortTermMemory(window_size=5, decay_rate=0.8) | |
| # stm.update('abstract', 'credit-card') | |
| # print(stm.get_memory()) # Output: {'abstract_entities': {'credit-card': 1}, 'locations': {}, 'times': {}} | |