juliensimon HF staff commited on
Commit
8af7698
1 Parent(s): 59a47e8

Initial version

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