File size: 2,491 Bytes
1eece24
 
 
a666a38
1eece24
9dfe88f
1eece24
 
 
 
 
 
 
 
 
 
 
 
 
7682c87
1eece24
e128c45
3bf5e3c
a752cc8
7682c87
1eece24
7682c87
174c08f
37d5d29
5909573
1eece24
 
 
46623a1
 
1eece24
9b3da76
1eece24
 
7682c87
1eece24
 
6a4de7c
 
 
 
f1603d1
 
b0291ca
c66a633
 
1eece24
9b3da76
 
 
1eece24
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import numpy as np
import torch

BASE_MODEL = "amazon-sagemaker-community/xlm-roberta-en-ru-emoji-v2"
TOP_N = 5

model = AutoModelForSequenceClassification.from_pretrained(BASE_MODEL)
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)

def preprocess(text):
    new_text = []
    for t in text.split(" "):
        t = '@user' if t.startswith('@') and len(t) > 1 else t
        t = 'http' if t.startswith('http') else t
        new_text.append(t)
    return " ".join(new_text)
    
def get_top_emojis(text):
    preprocessed = preprocess(text)
    inputs = tokenizer(preprocessed, return_tensors="pt")
    preds = model(**inputs).logits
    scores = torch.nn.functional.softmax(preds, dim=-1).detach().numpy()
    sorted_scores = [float(value) for value in np.sort(scores.squeeze())[::-1]]
    ranking = np.argsort(scores)
    ranking = ranking.squeeze()[::-1]
    emojis = [model.config.id2label[i] for i in ranking]
    return dict(zip(emojis, sorted_scores))

    
gradio_ui = gr.Interface(
    fn=get_top_emojis,
    title="Predicting emojis for tweets",
    description="Enter a tweet to predict emojis",
    inputs=[
        gr.inputs.Textbox(lines=3, label="Paste a tweet here"),
    ],
    outputs=[
        gr.outputs.Label(type="confidences", label=f"Predicted emojis", num_top_classes=TOP_N)
    ],
    examples=[
        ["it's pretty depressing when u hit pan on ur favourite highlighter"],
        ["After what just happened. In need to smoke."],
        ["I've never been happier. I'm laying awake as I watch @user sleep. Thanks for making me happy again, babe."],
        ["@user is the man"],
        ["Поприветствуем моего нового читателя @user"],
        ["сегодня у одной крутой бичи день рождения! @user поздравляю тебя с днем рождения! будь самой-самой счастливой,красота:* море любви тебе"],
        ["Никогда не явствовала себя ужаснее, чем сейчас:( я просто раздавленна"],
        ["Самое ужасное - это ожидание результатов"],
        ["печально что заряд одинаково фигово держится("],
    ],
    enable_queue=True,
    allow_screenshot=False,
    allow_flagging=False
)

gradio_ui.launch(debug=True)