Spaces:
Sleeping
Sleeping
Oscar Wang
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,39 @@
|
|
1 |
-
import os
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
-
# Load the
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import RobertaTokenizer, RobertaForSequenceClassification
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load the model and tokenizer from the specified directory
|
6 |
+
model_path = './finetuned_roberta'
|
7 |
+
tokenizer = RobertaTokenizer.from_pretrained(model_path)
|
8 |
+
model = RobertaForSequenceClassification.from_pretrained(model_path)
|
9 |
|
10 |
+
# Define the prediction function
|
11 |
+
def classify_text(text):
|
12 |
+
# Tokenize the input text
|
13 |
+
inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=128)
|
14 |
+
|
15 |
+
# Get the model's prediction
|
16 |
+
with torch.no_grad():
|
17 |
+
outputs = model(**inputs)
|
18 |
+
|
19 |
+
# Apply softmax to get probabilities
|
20 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
21 |
+
|
22 |
+
# Get the probability of the class '1'
|
23 |
+
prob_1 = probabilities[0][1].item()
|
24 |
+
|
25 |
+
return {"Probability of being 1": prob_1}
|
26 |
|
27 |
+
# Create the Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=classify_text,
|
30 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
|
31 |
+
outputs="json",
|
32 |
+
title="Text Classification with RoBERTa",
|
33 |
+
description="Enter some text and get the probability of the text being classified as class 1.",
|
34 |
+
enable_queue=True, # Ensure the API is enabled
|
35 |
+
)
|
36 |
+
|
37 |
+
# Launch the app
|
38 |
+
if __name__ == "__main__":
|
39 |
+
iface.launch()
|