File size: 1,146 Bytes
1c9915c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import torch.nn.functional as F
import numpy as np
from transformers import AutoTokenizer, AutoModelForSequenceClassification
  

repo_name = "adrianmoses/autonlp-auto-nlp-lyrics-classification-19333717" 
tokenizer = AutoTokenizer.from_pretrained(repo_name)
model = AutoModelForSequenceClassification.from_pretrained(repo_name)

labels = model.config.id2label

def predict(lyrics):
	  inputs = tokenizer(lyrics, padding=True, truncation=True, return_tensors="pt")
	  outputs = model(**inputs)
	  predictions = F.softmax(outputs.logits, dim=-1)
	  predictions = predictions.detach().numpy()[0]
	  predictions = predictions*100
	  sorted_indexes = np.argsort(predictions)
	  return "These lyrics are {:.2f}% {}, {:.2f}% {} and {:.2f}% {}.".format(
      predictions[sorted_indexes[-1]], labels[sorted_indexes[-1]],
      predictions[sorted_indexes[-2]], labels[sorted_indexes[-2]],
      predictions[sorted_indexes[-3]], labels[sorted_indexes[-3]])


col1, col2 = st.columns(2)
lyrics = col1.text_area("Lyrics")
clicked = col1.button("Submit")


output = ""
if clicked:
    output = predict(lyrics)

col2.write(output)