File size: 1,260 Bytes
333cd19
 
 
 
 
 
 
 
 
 
f0ad92c
333cd19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pickle

import streamlit as st
import model


def main():
    st.title("Model Prediction")

    # st.write(f"Session ID: {st.session_state.key}")
    session_id = st.session_state.key

    if not os.path.isdir(f"models/{session_id}"):
        st.write("Model is not available")
        st.stop()

    model_options = [model_name for model_name in os.listdir(f"models/{session_id}")]

    models = {
        model_name: os.path.abspath(os.path.join(f"models/{session_id}", model_name))
        for model_name in model_options
    }

    model_name = st.selectbox("Select a model", options=model_options)

    # Text input
    text = st.text_area("Enter some text here", height=200)

    # Prediction button
    if st.button("Predict"):

        with open(f"{models[model_name]}/label.pkl", "rb") as f:
            label_map = pickle.load(f)

        classifier = model.create_classifier(models[model_name])

        prediction = classifier([text])

        prediction_class = prediction[0].item()

        confidence_score = classifier.predict_proba([text])[0][prediction_class].item()

        st.write(
            "The predicted label is:",
            label_map[prediction_class],
            f"{round(confidence_score*100,2)}%",
        )