Zekun Wu
commited on
Commit
•
b0a3b5e
1
Parent(s):
ea070cc
update
Browse files- app.py +10 -74
- pages/1_single_evaluation.py +79 -0
app.py
CHANGED
@@ -1,79 +1,15 @@
|
|
1 |
-
import pandas as pd
|
2 |
import streamlit as st
|
3 |
-
from evaluator import evaluator,write_evaluation_commentary
|
4 |
-
import os
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
'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."
|
11 |
-
},
|
12 |
-
'bad': {
|
13 |
-
'question': "What causes rainbows to appear in the sky?",
|
14 |
-
'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."
|
15 |
-
}
|
16 |
-
}
|
17 |
|
|
|
18 |
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
st.session_state['password_correct'] = True
|
25 |
-
else:
|
26 |
-
st.error("Incorrect Password, please try again.")
|
27 |
-
|
28 |
-
password_input = st.text_input("Enter Password:", type="password")
|
29 |
-
submit_button = st.button("Submit", on_click=password_entered)
|
30 |
-
|
31 |
-
if submit_button and not st.session_state.get('password_correct', False):
|
32 |
-
st.error("Please enter a valid password to access the demo.")
|
33 |
-
|
34 |
-
|
35 |
-
# Title of the application
|
36 |
-
st.title('Natural Language Explanation Demo')
|
37 |
-
|
38 |
-
# Check if password has been validated
|
39 |
-
if not st.session_state.get('password_correct', False):
|
40 |
-
check_password()
|
41 |
-
else:
|
42 |
-
model_name = st.selectbox('Select a model:', ['gpt4-1106', 'gpt35-1106'])
|
43 |
-
|
44 |
-
# User choice between predefined examples or their own input
|
45 |
-
input_type = st.radio("Choose input type:", ('Use predefined example', 'Enter your own'))
|
46 |
-
|
47 |
-
if input_type == 'Use predefined example':
|
48 |
-
example_type = st.radio("Select an example type:", ('good', 'bad'))
|
49 |
-
question = examples[example_type]['question']
|
50 |
-
explanation = examples[example_type]['explanation']
|
51 |
-
else:
|
52 |
-
question = st.text_input('Enter your question:', '')
|
53 |
-
explanation = st.text_input('Enter your explanation:', '')
|
54 |
-
|
55 |
-
# Display the selected or entered question and explanation
|
56 |
-
st.write('### Question')
|
57 |
-
st.write(question if question else 'No question entered yet.')
|
58 |
-
|
59 |
-
st.write('### Explanation')
|
60 |
-
st.write(explanation if explanation else 'No explanation entered yet.')
|
61 |
-
|
62 |
-
if st.button('Evaluate Explanation'):
|
63 |
-
if question and explanation:
|
64 |
-
eval = evaluator(model_name)
|
65 |
-
scores = eval(question, explanation)
|
66 |
-
st.write('### Scores')
|
67 |
-
details = write_evaluation_commentary(scores)
|
68 |
-
df = pd.DataFrame(details)
|
69 |
-
st.write(df)
|
70 |
-
|
71 |
-
csv = df.to_csv(index=False)
|
72 |
-
st.download_button(
|
73 |
-
label="Download evaluation as CSV",
|
74 |
-
data=csv,
|
75 |
-
file_name='evaluation.csv',
|
76 |
-
mime='text/csv',
|
77 |
-
)
|
78 |
-
else:
|
79 |
-
st.error('Please enter both a question and an explanation to evaluate.')
|
|
|
|
|
1 |
import streamlit as st
|
|
|
|
|
2 |
|
3 |
+
st.set_page_config(
|
4 |
+
page_title="app",
|
5 |
+
page_icon="👋",
|
6 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
st.title('AI explainability in the EU AI Act: a case for an NLE approach towards pragmatic explanations')
|
9 |
|
10 |
+
st.sidebar.success("Select a demo above.")
|
11 |
|
12 |
+
st.markdown(
|
13 |
+
"""
|
14 |
+
"""
|
15 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pages/1_single_evaluation.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import streamlit as st
|
3 |
+
from evaluator import evaluator,write_evaluation_commentary
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Predefined examples
|
7 |
+
examples = {
|
8 |
+
'good': {
|
9 |
+
'question': "What causes rainbows to appear in the sky?",
|
10 |
+
'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."
|
11 |
+
},
|
12 |
+
'bad': {
|
13 |
+
'question': "What causes rainbows to appear in the sky?",
|
14 |
+
'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."
|
15 |
+
}
|
16 |
+
}
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
# Function to check password
|
21 |
+
def check_password():
|
22 |
+
def password_entered():
|
23 |
+
if password_input == os.getenv('PASSWORD'):
|
24 |
+
st.session_state['password_correct'] = True
|
25 |
+
else:
|
26 |
+
st.error("Incorrect Password, please try again.")
|
27 |
+
|
28 |
+
password_input = st.text_input("Enter Password:", type="password")
|
29 |
+
submit_button = st.button("Submit", on_click=password_entered)
|
30 |
+
|
31 |
+
if submit_button and not st.session_state.get('password_correct', False):
|
32 |
+
st.error("Please enter a valid password to access the demo.")
|
33 |
+
|
34 |
+
|
35 |
+
# Title of the application
|
36 |
+
st.title('Natural Language Explanation Demo')
|
37 |
+
|
38 |
+
# Check if password has been validated
|
39 |
+
if not st.session_state.get('password_correct', False):
|
40 |
+
check_password()
|
41 |
+
else:
|
42 |
+
model_name = st.selectbox('Select a model:', ['gpt4-1106', 'gpt35-1106'])
|
43 |
+
|
44 |
+
# User choice between predefined examples or their own input
|
45 |
+
input_type = st.radio("Choose input type:", ('Use predefined example', 'Enter your own'))
|
46 |
+
|
47 |
+
if input_type == 'Use predefined example':
|
48 |
+
example_type = st.radio("Select an example type:", ('good', 'bad'))
|
49 |
+
question = examples[example_type]['question']
|
50 |
+
explanation = examples[example_type]['explanation']
|
51 |
+
else:
|
52 |
+
question = st.text_input('Enter your question:', '')
|
53 |
+
explanation = st.text_input('Enter your explanation:', '')
|
54 |
+
|
55 |
+
# Display the selected or entered question and explanation
|
56 |
+
st.write('### Question')
|
57 |
+
st.write(question if question else 'No question entered yet.')
|
58 |
+
|
59 |
+
st.write('### Explanation')
|
60 |
+
st.write(explanation if explanation else 'No explanation entered yet.')
|
61 |
+
|
62 |
+
if st.button('Evaluate Explanation'):
|
63 |
+
if question and explanation:
|
64 |
+
eval = evaluator(model_name)
|
65 |
+
scores = eval(question, explanation)
|
66 |
+
st.write('### Scores')
|
67 |
+
details = write_evaluation_commentary(scores)
|
68 |
+
df = pd.DataFrame(details)
|
69 |
+
st.write(df)
|
70 |
+
|
71 |
+
csv = df.to_csv(index=False)
|
72 |
+
st.download_button(
|
73 |
+
label="Download evaluation as CSV",
|
74 |
+
data=csv,
|
75 |
+
file_name='evaluation.csv',
|
76 |
+
mime='text/csv',
|
77 |
+
)
|
78 |
+
else:
|
79 |
+
st.error('Please enter both a question and an explanation to evaluate.')
|