Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
BASE_MODEL = "AlekseyDorkin/xlm-roberta-en-ru-emoji"
|
6 |
+
TOP_N = 5
|
7 |
+
|
8 |
+
model = AutoModelForSequenceClassification.from_pretrained(BASE_MODEL)
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
10 |
+
|
11 |
+
def preprocess(text):
|
12 |
+
new_text = []
|
13 |
+
for t in text.split(" "):
|
14 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
15 |
+
t = 'http' if t.startswith('http') else t
|
16 |
+
new_text.append(t)
|
17 |
+
return " ".join(new_text)
|
18 |
+
|
19 |
+
def get_top_emojis(text, top_n=TOP_N):
|
20 |
+
preprocessed = preprocess(text)
|
21 |
+
inputs = tokenizer(preprocessed)
|
22 |
+
preds = model(**inputs)
|
23 |
+
scores = torch.nn.functional.softmax(preds, axis=-1).detach().numpy()
|
24 |
+
ranking = np.argsort(scores)
|
25 |
+
ranking = ranking[::-1][:top_n]
|
26 |
+
emojis = [model.config.id2label[index] for index in ranking]
|
27 |
+
return emojis
|
28 |
+
|
29 |
+
|
30 |
+
gradio_ui = gr.Interface(
|
31 |
+
fn=get_top_emojis,
|
32 |
+
title="Predicting review scores from customer reviews",
|
33 |
+
description="Enter some review text about an Amazon product and check what the model predicts for it's star rating.",
|
34 |
+
inputs=[
|
35 |
+
gr.inputs.Textbox(lines=5, label="Paste some text here"),
|
36 |
+
],
|
37 |
+
outputs=[
|
38 |
+
gr.outputs.Textbox(label=f"№{i}") for i in range(TOP_N)
|
39 |
+
],
|
40 |
+
examples=[
|
41 |
+
["Awesome!"], ["Круто!"], ["lol"]
|
42 |
+
],
|
43 |
+
)
|
44 |
+
|
45 |
+
gradio_ui.launch(debug=True)
|