adrianmoses commited on
Commit
1c9915c
1 Parent(s): 3f29a04

uses huggingface demo and autonlp model

Browse files
Files changed (2) hide show
  1. app.py +38 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch.nn.functional as F
3
+ import numpy as np
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+
6
+
7
+ repo_name = "adrianmoses/autonlp-auto-nlp-lyrics-classification-19333717"
8
+ tokenizer = AutoTokenizer.from_pretrained(repo_name)
9
+ model = AutoModelForSequenceClassification.from_pretrained(repo_name)
10
+
11
+ labels = model.config.id2label
12
+
13
+ def predict(lyrics):
14
+ inputs = tokenizer(lyrics, padding=True, truncation=True, return_tensors="pt")
15
+ outputs = model(**inputs)
16
+ predictions = F.softmax(outputs.logits, dim=-1)
17
+ predictions = predictions.detach().numpy()[0]
18
+ predictions = predictions*100
19
+ sorted_indexes = np.argsort(predictions)
20
+ return "These lyrics are {:.2f}% {}, {:.2f}% {} and {:.2f}% {}.".format(
21
+ predictions[sorted_indexes[-1]], labels[sorted_indexes[-1]],
22
+ predictions[sorted_indexes[-2]], labels[sorted_indexes[-2]],
23
+ predictions[sorted_indexes[-3]], labels[sorted_indexes[-3]])
24
+
25
+
26
+ col1, col2 = st.columns(2)
27
+ lyrics = col1.text_area("Lyrics")
28
+ clicked = col1.button("Submit")
29
+
30
+
31
+ output = ""
32
+ if clicked:
33
+ output = predict(lyrics)
34
+
35
+ col2.write(output)
36
+
37
+
38
+
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers==4.12.3
2
+ torch
3
+ numpy