menimeni123
commited on
Commit
•
8f655f1
1
Parent(s):
da97f49
latest
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import DistilBertForSequenceClassification, DistilBertTokenizer
|
2 |
+
import torch
|
3 |
+
import joblib
|
4 |
+
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model = DistilBertForSequenceClassification.from_pretrained(".")
|
7 |
+
tokenizer = DistilBertTokenizer.from_pretrained(".")
|
8 |
+
|
9 |
+
# Load the label mapping
|
10 |
+
label_mapping = joblib.load("label_mapping.joblib")
|
11 |
+
|
12 |
+
def predict(text):
|
13 |
+
# Tokenize the input text
|
14 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
15 |
+
|
16 |
+
# Make prediction
|
17 |
+
with torch.no_grad():
|
18 |
+
outputs = model(**inputs)
|
19 |
+
|
20 |
+
# Get the predicted class
|
21 |
+
predicted_class = torch.argmax(outputs.logits, dim=1).item()
|
22 |
+
|
23 |
+
# Map the predicted class to its label
|
24 |
+
predicted_label = label_mapping[predicted_class]
|
25 |
+
|
26 |
+
return predicted_label
|
27 |
+
|
28 |
+
# Test the function
|
29 |
+
print(predict("Your test text here"))
|