File size: 2,109 Bytes
5463463
dcb4889
d3f1a70
5463463
2855b18
 
 
 
 
 
 
 
 
 
3cd0fc7
2855b18
5463463
d3f1a70
a091e6a
 
 
 
 
d3f1a70
 
 
 
a091e6a
d3f1a70
a091e6a
 
d3f1a70
2446b60
d3f1a70
2855b18
d3f1a70
2324c23
d3f1a70
 
 
 
 
 
 
a4d1088
2855b18
 
8d1089d
0734055
8d1089d
0734055
5a98944
3ceaa76
0734055
8d1089d
2855b18
d3f1a70
a091e6a
261c184
0734055
261c184
4c6d207
261c184
a091e6a
2855b18
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)