File size: 1,996 Bytes
f04f807
4bcae31
 
 
f04f807
4bcae31
 
 
 
 
 
 
 
491a9c7
 
 
 
4bcae31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import streamlit as st
import torch
from transformers import DistilBertForSequenceClassification, DistilBertTokenizer
from torch.nn.functional import softmax

# Load the model and tokenizer
model = DistilBertForSequenceClassification.from_pretrained('./fine_tuned_distilbert')
tokenizer = DistilBertTokenizer.from_pretrained('./fine_tuned_distilbert')

# Device setup
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

mapping = {"Remembering": 0, "Understanding": 1, "Applying": 2, "Analyzing": 3, "Evaluating": 4, "Creating": 5}

# Reverse the mapping to get the class name from the index
reverse_mapping = {v: k for k, v in mapping.items()}

def predict_with_loaded_model(text):
    # Tokenize the input text
    inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=512)
    input_ids = inputs['input_ids'].to(device)

    model.eval()
    with torch.no_grad():
        # Get the raw logits from the model
        outputs = model(input_ids)
        logits = outputs.logits
        
        # Apply softmax to get probabilities
        probabilities = softmax(logits, dim=-1)
    
    # Convert probabilities to a list or dictionary of class probabilities
    probabilities = probabilities.squeeze().cpu().numpy()
    
    # Map the probabilities to the class labels using the reverse mapping
    class_probabilities = {reverse_mapping[i]: prob for i, prob in enumerate(probabilities)}
    
    return class_probabilities

# Streamlit App
st.title("Question Bloom Score Prediction")

# Create an input box for the user to enter a question
question = st.text_area("Enter a question:")

# If a question is entered, make the prediction
if question:
    class_probabilities = predict_with_loaded_model(question)

    # Display the probabilities for each class label
    st.write("**Class Probabilities (Bloom Scores)**")
    for class_label, prob in class_probabilities.items():
        st.write(f"{class_label}: {prob:.4f}")