ner-demo / app.py
elshehawy's picture
set aggregation_strategy="first"
3cd0fc7
raw
history blame
2.11 kB
import gradio as gr
from openai import OpenAI
import os
from transformers import pipeline
# from dotenv import load_dotenv, find_dotenv
import huggingface_hub
# _ = load_dotenv(find_dotenv()) # read local .env file
hf_token= os.environ['HF_TOKEN']
huggingface_hub.login(hf_token)
pipe = pipeline("token-classification", model="elshehawy/finer-ord-transformers", aggregation_strategy="first")
llm_model = 'gpt-3.5-turbo-0125'
# openai.api_key = os.environ['OPENAI_API_KEY']
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)
def get_completion(prompt, model=llm_model):
messages = [{"role": "user", "content": prompt}]
response = client.chat.completions.create(
messages=messages,
model=model,
temperature=0,
)
return response.choices[0].message.content
def find_orgs(sentence, choice):
prompt = f"""
In context of named entity recognition (NER), find all organizations in the text delimited by triple backticks.
text:
```
{sentence}
```
You should always start your answer with "Organizations are: "
"""
if choice=='GPT':
return get_completion(prompt)
else:
message = 'Organizations are:'
org_list = []
for ent in pipe(sentence):
if ent['entity_group'] == 'ORG' and ent['word'] not in org_list:
# message += f'\n- {ent["word"]} \t- score: {ent["score"]}'
message += f'\n- {ent["word"]}'# \t- score: {ent["score"]}'
org_list.append(ent['word'])
return message
example = """
My latest exclusive for The Hill : Conservative frustration over Republican efforts to force a House vote on reauthorizing the Export - Import Bank boiled over Wednesday during a contentious GOP meeting.
"""
radio_btn = gr.Radio(choices=['GPT', 'iSemantics'], value='iSemantics', label='Available models', show_label=True)
textbox = gr.Textbox(label="Enter your text", placeholder="", lines=4)
iface = gr.Interface(fn=find_orgs, inputs=[textbox, radio_btn], outputs="text", examples=[[example]])
iface.launch(share=True)