File size: 2,616 Bytes
651ff6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a129bb
651ff6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import streamlit as st
from util.evaluator import evaluator, write_evaluation_commentary
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."
    }
}

# Function to check password
def check_password():
    password_input = st.sidebar.text_input("Enter Password:", type="password")
    submit_button = st.sidebar.button("Submit")
    if submit_button:
        if password_input == os.getenv('PASSWORD'):
            st.session_state['password_verified'] = True
            st.experimental_rerun()
        else:
            st.sidebar.error("Incorrect Password, please try again.")

def batch_evaluate(uploaded_file):
    df = pd.read_csv(uploaded_file)
    eval = evaluator(model_name='gpt4-1106')  # Assuming model name is fixed for simplicity
    results = []

    for _, row in df.iterrows():
        question = row['question']
        explanation = row['explanation']
        scores = eval(question, explanation)
        commentary = write_evaluation_commentary(scores)[["Principle", "Score"]].transpose().to_dict()
        results.append({**{'Question': question, 'Explanation': explanation}, **commentary})

    result_df = pd.DataFrame(results)
    return result_df

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

if 'password_verified' not in st.session_state or not st.session_state['password_verified']:
    check_password()
else:
    st.sidebar.success("Password Verified. Proceed with the demo.")
    st.header("Batch Evaluation of Questions and Explanations")
    uploaded_file = st.file_uploader("Upload CSV file with columns 'question' and 'explanation'", type='csv')

    if uploaded_file is not None:
        if st.button('Evaluate Explanations'):
            result_df = batch_evaluate(uploaded_file)
            st.write('### Evaluated Results')
            st.dataframe(result_df)

            csv = result_df.to_csv(index=False)
            st.download_button(
                label="Download evaluation results as CSV",
                data=csv,
                file_name='evaluated_results.csv',
                mime='text/csv'
            )