Spaces:
Sleeping
Sleeping
added model
Browse files
app.py
CHANGED
@@ -1,4 +1,52 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from transformers import DistilBertForSequenceClassification, DistilBertTokenizer
|
4 |
+
from torch.nn.functional import softmax
|
5 |
|
6 |
+
# Load the model and tokenizer
|
7 |
+
model = DistilBertForSequenceClassification.from_pretrained('./fine_tuned_distilbert')
|
8 |
+
tokenizer = DistilBertTokenizer.from_pretrained('./fine_tuned_distilbert')
|
9 |
+
|
10 |
+
# Device setup
|
11 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
+
model.to(device)
|
13 |
+
|
14 |
+
# Reverse mapping of categories to class labels
|
15 |
+
reverse_mapping = {0: "BT1", 1: "BT2", 2: "BT3", 3: "BT4", 4: "BT5", 5: "BT6"}
|
16 |
+
|
17 |
+
def predict_with_loaded_model(text):
|
18 |
+
# Tokenize the input text
|
19 |
+
inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=512)
|
20 |
+
input_ids = inputs['input_ids'].to(device)
|
21 |
+
|
22 |
+
model.eval()
|
23 |
+
with torch.no_grad():
|
24 |
+
# Get the raw logits from the model
|
25 |
+
outputs = model(input_ids)
|
26 |
+
logits = outputs.logits
|
27 |
+
|
28 |
+
# Apply softmax to get probabilities
|
29 |
+
probabilities = softmax(logits, dim=-1)
|
30 |
+
|
31 |
+
# Convert probabilities to a list or dictionary of class probabilities
|
32 |
+
probabilities = probabilities.squeeze().cpu().numpy()
|
33 |
+
|
34 |
+
# Map the probabilities to the class labels using the reverse mapping
|
35 |
+
class_probabilities = {reverse_mapping[i]: prob for i, prob in enumerate(probabilities)}
|
36 |
+
|
37 |
+
return class_probabilities
|
38 |
+
|
39 |
+
# Streamlit App
|
40 |
+
st.title("Question Bloom Score Prediction")
|
41 |
+
|
42 |
+
# Create an input box for the user to enter a question
|
43 |
+
question = st.text_area("Enter a question:")
|
44 |
+
|
45 |
+
# If a question is entered, make the prediction
|
46 |
+
if question:
|
47 |
+
class_probabilities = predict_with_loaded_model(question)
|
48 |
+
|
49 |
+
# Display the probabilities for each class label
|
50 |
+
st.write("**Class Probabilities (Bloom Scores)**")
|
51 |
+
for class_label, prob in class_probabilities.items():
|
52 |
+
st.write(f"{class_label}: {prob:.4f}")
|