File size: 1,362 Bytes
997dd7e
0ff5bf3
 
997dd7e
0ff5bf3
 
 
ae2aca3
997dd7e
 
0ff5bf3
 
 
 
997dd7e
0ff5bf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
997dd7e
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

DEVICE = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
MODEL_ID = "LinguaCustodia/ClassiFin"
MODEL = AutoModelForSequenceClassification.from_pretrained(MODEL_ID).to(DEVICE)
TOKENIZER = AutoTokenizer.from_pretrained(MODEL_ID)


def classify(text):
    inputs = TOKENIZER(text, return_tensors="pt")
    for k, v in inputs.items():
        inputs[k] = v.to(DEVICE)

    outputs = MODEL(**inputs)
    logits = outputs.logits.cpu().clamp(min=0.0, max=1.0).squeeze().item()
    return {
        "finance": logits,
        "not finance": 1.0 - logits,
    }


EXAMPLES = [
    'A call option, often simply labeled a "call", is a contract between the buyer and the seller of the call option to exchange a security at a set price.',
    "Natural language processing (NLP) is a subfield of computer science and especially artificial intelligence. It is primarily concerned with providing computers with the ability to process data encoded in natural language and is thus closely related to information retrieval, knowledge representation and computational linguistics, a subfield of linguistics.",
]

demo = gr.Interface(
    fn=classify,
    inputs=gr.Textbox(lines=3),
    examples=EXAMPLES,
    outputs=gr.Label(),
)
demo.launch()