Spaces:
Sleeping
Sleeping
| import os | |
| import io | |
| import base64 | |
| from transformers import pipeline | |
| import gradio as gr | |
| hf_api_key = os.environ['HF_API_KEY'] | |
| get_completion = pipeline("ner", model="dslim/bert-base-NER") | |
| def merge_tokens(tokens): | |
| merged_tokens = [] | |
| for token in tokens: | |
| if merged_tokens and token['entity'].startswith('I-') and merged_tokens[-1]['entity'].endswith(token['entity'][2:]): | |
| # If the current token continues the entity of the last one, merge them | |
| last_token = merged_tokens[-1] | |
| last_token['word'] += token['word'].replace('##', '') | |
| last_token['end'] = token['end'] | |
| last_token['score'] = (last_token['score'] + token['score']) / 2 | |
| else: | |
| # Otherwise, add the token to the list | |
| merged_tokens.append(token) | |
| return merged_tokens | |
| def ner(input): | |
| output = get_completion(input) | |
| merged_tokens = merge_tokens(output) | |
| return {"text": input, "entities": merged_tokens} | |
| # Create Gradio interface | |
| demo = gr.Interface(fn=ner, | |
| inputs=[gr.Textbox(label="Text to find entities", lines=2)], | |
| outputs=[gr.HighlightedText(label="Text with entities")], | |
| title="NER with dslim/bert-base-NER", | |
| description="Find entities using the `dslim/bert-base-NER` model under the hood!", | |
| flagging_mode="never", # Updated from allow_flagging | |
| examples=[ | |
| "My name is Andrew, I'm building DeeplearningAI and I live in California", | |
| "My name is Poli, I live in Vienna and work at HuggingFace" | |
| ] | |
| ) | |
| demo.launch( | |
| share=True, | |
| # server_port=int(os.environ.get('PORT2', 7860)) # Uncomment if needed | |
| ) |