File size: 5,163 Bytes
0a026c0
 
0bf6595
0a026c0
a1320fa
 
 
 
 
 
 
 
 
 
 
 
0eb1a66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1320fa
0bf6595
 
 
 
 
 
 
 
 
 
0a026c0
0bf6595
 
0a026c0
a1320fa
0bf6595
 
0a026c0
a1320fa
0bf6595
 
 
 
0a026c0
171bf3d
 
a1320fa
171bf3d
 
 
 
 
 
 
a1320fa
171bf3d
 
 
 
 
 
0a026c0
0bf6595
171bf3d
 
 
 
0eb1a66
171bf3d
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import streamlit as st
from evaluator import evaluator
import os

# Predefined examples
examples = {
    'good': {
        'question': "What causes rainbows to appear in the sky?",
        'explanation': "Rainbows appear when sunlight is refracted, dispersed, and reflected inside water droplets in the atmosphere, resulting in a spectrum of light appearing in the sky."
    },
    'bad': {
        'question': "What causes rainbows to appear in the sky?",
        'explanation': "Rainbows happen because light in the sky gets mixed up and sometimes shows colors when it's raining or when there is water around."
    }
}

def write_evaluation_commentary(scores):
    for principle, score in scores.items():
        if principle == "Factually Correct":
            if score >= 0.8:
                comment = "Excellent accuracy! The information is precise and directly relevant to the question."
            elif score >= 0.5:
                comment = "Moderately accurate, but some details may not be completely correct or are somewhat irrelevant."
            else:
                comment = "The explanation contains significant inaccuracies or irrelevant information."
        elif principle == "Useful":
            if score >= 0.8:
                comment = "Highly useful! The explanation clearly enhances understanding and aids in further reasoning or decision-making."
            elif score >= 0.5:
                comment = "Somewhat useful, though it could be more insightful or practical in aiding understanding."
            else:
                comment = "The explanation does little to help understand or apply the information provided."
        elif principle == "Context Specific":
            if score >= 0.8:
                comment = "Perfectly tailored to the context of the question, addressing the specific scenario effectively."
            elif score >= 0.5:
                comment = "Generally addresses the context, but may miss specific details or nuances relevant to the question."
            else:
                comment = "Fails to address the context of the question, lacking relevance or specificity."
        elif principle == "User Specific":
            if score >= 0.8:
                comment = "The explanation is well-adapted to the user's knowledge level and interests, demonstrating thoughtfulness."
            elif score >= 0.5:
                comment = "Moderately considerate of the user's knowledge level, but could be more tailored."
            else:
                comment = "Does not consider the user's background or interests, potentially leading to confusion or disinterest."
        elif principle == "Provides Pluralism":
            if score >= 0.8:
                comment = "Provides an excellent range of perspectives or interpretations, fostering a comprehensive understanding."
            elif score >= 0.5:
                comment = "Offers some alternative perspectives, but more could be provided to enrich understanding."
            else:
                comment = "Lacks diversity in viewpoints, limiting the depth of exploration into the topic."

        st.write(f"{principle} ({score}): {comment}")

# Function to check password
def check_password():
    def password_entered():
        if password_input == os.getenv('PASSWORD'):
            st.session_state['password_correct'] = True
        else:
            st.error("Incorrect Password, please try again.")

    password_input = st.text_input("Enter Password:", type="password")
    submit_button = st.button("Submit", on_click=password_entered)

    if submit_button and not st.session_state.get('password_correct', False):
        st.error("Please enter a valid password to access the demo.")


# Title of the application
st.title('Natural Language Explanation Demo')

# Check if password has been validated
if not st.session_state.get('password_correct', False):
    check_password()
else:
    model_name = st.selectbox('Select a model:', ['gpt4-1106', 'gpt35-1106'])

    # User choice between predefined examples or their own input
    input_type = st.radio("Choose input type:", ('Use predefined example', 'Enter your own'))

    if input_type == 'Use predefined example':
        example_type = st.radio("Select an example type:", ('good', 'bad'))
        question = examples[example_type]['question']
        explanation = examples[example_type]['explanation']
    else:
        question = st.text_input('Enter your question:', '')
        explanation = st.text_input('Enter your explanation:', '')

    # Display the selected or entered question and explanation
    st.write('### Question')
    st.write(question if question else 'No question entered yet.')

    st.write('### Explanation')
    st.write(explanation if explanation else 'No explanation entered yet.')

    if st.button('Evaluate Explanation'):
        if question and explanation:
            eval = evaluator(model_name)
            scores = eval(question, explanation)
            st.write('### Scores')
            write_evaluation_commentary(scores)
        else:
            st.error('Please enter both a question and an explanation to evaluate.')