File size: 1,116 Bytes
1b28f25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForSequenceClassification, AutoTokenizer, TextClassificationPipeline

tokenizer = AutoTokenizer.from_pretrained("daspartho/text-emotion")
model = AutoModelForSequenceClassification.from_pretrained("daspartho/text-emotion") # i've uploaded the model on HuggingFace :)

pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, top_k=6)

label_map={
    'LABEL_0':'πŸ™',
    'LABEL_1':'πŸ˜ƒ',
    'LABEL_2':'πŸ₯°',
    'LABEL_3':'😠',
    'LABEL_4':'😬',
    'LABEL_5':'😳'
  }

def classify_text(text):
    predictions = pipe(text)[0]
    return {label_map[pred['label']]: float(pred['score']) for pred in predictions}

iface = gr.Interface(
    title='Text Emotion',
    description = "enter a text and the model will attempt to predict the emotion.",
    article = "<p style='text-align: center'><a href='https://github.com/daspartho/text-emotion' target='_blank'>Github</a></p>",
    fn=classify_text, 
    inputs=gr.inputs.Textbox(label="type the text here"), 
    outputs=gr.outputs.Label(label='what the model thinks'),
    )
iface.launch()