Spaces:
Runtime error
Runtime error
Download app.py from thanhcong2001/Extract_Entities: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/thanhcong2001/Extract_Entities/resolve/main/app.py
- Command line
-
hf download hf://spaces/thanhcong2001/Extract_Entities/app.py
-
curl -L -o app.py https://huggingface.co/spaces/thanhcong2001/Extract_Entities/resolve/main/app.py
1.01 kB
| # load dataset | |
| import pandas as pd | |
| df = pd.read_csv('sentiment_data.csv') | |
| texts = df['text'].astype(str) | |
| # load NER model | |
| import spacy | |
| model = spacy.load('en_core_web_lg') | |
| # Extract entities | |
| result = [] | |
| for t in texts: | |
| doc = model(t) | |
| entities = [(ent.text, ent.label_) for ent in doc.ents] | |
| result.append({'Text':t,'Entity':entities}) | |
| result_df = pd.DataFrame(result) | |
| # Entities visualization | |
| from spacy import displacy | |
| for t in texts: | |
| doc = model(t) | |
| displacy.render(doc,style='ent') | |
| # Count Entities | |
| from collections import Counter | |
| all_entities = [ent for ents in result_df['Entity'] for ent in ents] | |
| labels = [label for text,label in all_entities] | |
| Counter(labels).most_common() | |
| # Extract entities from input | |
| def ext_ent(sentence): | |
| doc = model(sentence) | |
| output = '' | |
| for ent in doc.ents: | |
| output += f"{ent.text} - {ent.label_}\n" | |
| return output | |
| import gradio as gr | |
| demo = gr.Interface(fn=ext_ent,inputs='text',outputs='text',title="Extract Entities") | |
| demo.launch() |