juliensimon HF staff commited on
Commit
6d85d11
1 Parent(s): cec300d

Initial version

Browse files
Files changed (2) hide show
  1. app.py +26 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+
6
+ repo_name = 'spotify/autonlp-huggingface-demo-song-lyrics-18923587'
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(repo_name)
9
+ model = AutoModelForSequenceClassification.from_pretrained(repo_name)
10
+ labels = model.config.id2label
11
+
12
+ def predict(lyrics):
13
+ inputs = tokenizer(lyrics, padding=True, truncation=True, return_tensors="pt")
14
+ outputs = model(**inputs)
15
+ predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
16
+ predictions = predictions.detach().numpy()[0]
17
+ predictions = predictions*100
18
+ sorted_indexes = np.argsort(predictions)
19
+ return "These lyrics are {:.2f}% {}, {:.2f}% {} and {:.2f}% {}.".format(
20
+ predictions[sorted_indexes[-1]], labels[sorted_indexes[-1]],
21
+ predictions[sorted_indexes[-2]], labels[sorted_indexes[-2]],
22
+ predictions[sorted_indexes[-3]], labels[sorted_indexes[-3]])
23
+
24
+ input = gr.inputs.Textbox(lines=20)
25
+ iface = gr.Interface(fn=predict, inputs=input, outputs="text")
26
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
1
+ torch
2
+ transformers