Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("juliensimon/autonlp-imdb-demo-hf-16622775")
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained("juliensimon/autonlp-imdb-demo-hf-16622775")
|
| 9 |
+
|
| 10 |
+
def predict(review):
|
| 11 |
+
inputs = tokenizer(review, padding=True, truncation=True, return_tensors="pt")
|
| 12 |
+
outputs = model(**inputs)
|
| 13 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 14 |
+
predictions = predictions.detach().numpy()[0]
|
| 15 |
+
index = np.argmax(predictions)
|
| 16 |
+
score = predictions[index]
|
| 17 |
+
return "This revied os {:.3f}% {}".format(100*score, "negative" if index == 0 else "positive")
|
| 18 |
+
|
| 19 |
+
iface = gr.Interface(fn=predict, inputs='text', outputs='text')
|
| 20 |
+
iface.launch()
|