Zekun Wu
commited on
Commit
•
f695432
1
Parent(s):
b489e1c
update
Browse files
pages/3_explanation_generation.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import streamlit as st
|
3 |
+
from util.assistants import GPTAgent
|
4 |
+
import os
|
5 |
+
|
6 |
+
|
7 |
+
# Function to generate explanations based on a template
|
8 |
+
def generate_explanations(model_name, questions, template, custom_template=None):
|
9 |
+
agent = GPTAgent(model_name)
|
10 |
+
explanations = []
|
11 |
+
progress_bar = st.progress(0)
|
12 |
+
total_questions = len(questions)
|
13 |
+
|
14 |
+
for i, question in enumerate(questions):
|
15 |
+
if template == "Chain of Thought":
|
16 |
+
prompt = f"""Generate an explanation using the Chain of Thought template for the following question:
|
17 |
+
|
18 |
+
Question: {question}
|
19 |
+
|
20 |
+
Let's think step by step.
|
21 |
+
|
22 |
+
Explanation:
|
23 |
+
"""
|
24 |
+
elif template == "Custom" and custom_template:
|
25 |
+
prompt = custom_template.replace("{question}", question)
|
26 |
+
else:
|
27 |
+
prompt = f"""Generate an explanation for the following question:
|
28 |
+
|
29 |
+
Question: {question}
|
30 |
+
|
31 |
+
Explanation:
|
32 |
+
"""
|
33 |
+
response = agent.invoke(prompt, temperature=0.8, max_tokens=150).strip()
|
34 |
+
explanations.append(response)
|
35 |
+
|
36 |
+
# Update progress bar
|
37 |
+
progress_bar.progress((i + 1) / total_questions)
|
38 |
+
|
39 |
+
return explanations
|
40 |
+
|
41 |
+
|
42 |
+
# Predefined examples
|
43 |
+
examples = {
|
44 |
+
'good': {
|
45 |
+
'question': "What causes rainbows to appear in the sky?",
|
46 |
+
'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."
|
47 |
+
},
|
48 |
+
'bad': {
|
49 |
+
'question': "What causes rainbows to appear in the sky?",
|
50 |
+
'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."
|
51 |
+
}
|
52 |
+
}
|
53 |
+
|
54 |
+
|
55 |
+
# Function to check password
|
56 |
+
def check_password():
|
57 |
+
def password_entered():
|
58 |
+
if password_input == os.getenv('PASSWORD'):
|
59 |
+
st.session_state['password_correct'] = True
|
60 |
+
else:
|
61 |
+
st.error("Incorrect Password, please try again.")
|
62 |
+
|
63 |
+
password_input = st.text_input("Enter Password:", type="password")
|
64 |
+
submit_button = st.button("Submit", on_click=password_entered)
|
65 |
+
|
66 |
+
if submit_button and not st.session_state.get('password_correct', False):
|
67 |
+
st.error("Please enter a valid password to access the demo.")
|
68 |
+
|
69 |
+
|
70 |
+
# Title of the application
|
71 |
+
st.title('Natural Language Explanation Generation')
|
72 |
+
|
73 |
+
# Sidebar description of the application
|
74 |
+
st.sidebar.write("""
|
75 |
+
### Welcome to the Natural Language Explanation Generation Demo
|
76 |
+
This application allows you to generate high-quality explanations for various questions using different templates. Upload a CSV of questions, select an explanation template, and generate explanations.
|
77 |
+
""")
|
78 |
+
|
79 |
+
# Check if password has been validated
|
80 |
+
if not st.session_state.get('password_correct', False):
|
81 |
+
check_password()
|
82 |
+
else:
|
83 |
+
st.sidebar.success("Password Verified. Proceed with the demo.")
|
84 |
+
|
85 |
+
st.write("""
|
86 |
+
### Instructions for Uploading CSV
|
87 |
+
Please upload a CSV file with the following column:
|
88 |
+
- `question`: The question you want explanations for.
|
89 |
+
|
90 |
+
**Example CSV Format:**
|
91 |
+
""")
|
92 |
+
|
93 |
+
# Display an example DataFrame
|
94 |
+
example_data_gen = {
|
95 |
+
"question": [
|
96 |
+
"What causes rainbows to appear in the sky?",
|
97 |
+
"Why is the sky blue?"
|
98 |
+
]
|
99 |
+
}
|
100 |
+
example_df_gen = pd.DataFrame(example_data_gen)
|
101 |
+
st.dataframe(example_df_gen)
|
102 |
+
|
103 |
+
uploaded_file_gen = st.file_uploader("Upload CSV file with 'question' column", type=['csv'])
|
104 |
+
|
105 |
+
if uploaded_file_gen is not None:
|
106 |
+
template = st.selectbox("Select an explanation template", ["Default", "Chain of Thought", "Custom"])
|
107 |
+
model_name = st.selectbox('Select a model:', ['gpt4-1106', 'gpt35-1106'])
|
108 |
+
|
109 |
+
custom_template = ""
|
110 |
+
if template == "Custom":
|
111 |
+
custom_template = st.text_area("Enter your custom template",
|
112 |
+
value="Generate an explanation for the following question:\n\nQuestion: {question}\n\nExplanation:")
|
113 |
+
|
114 |
+
if st.button('Generate Explanations'):
|
115 |
+
questions_df = pd.read_csv(uploaded_file_gen)
|
116 |
+
questions = questions_df['question'].tolist()
|
117 |
+
explanations = generate_explanations(model_name, questions, template, custom_template)
|
118 |
+
|
119 |
+
result_df_gen = pd.DataFrame({
|
120 |
+
'question': questions,
|
121 |
+
'explanation': explanations
|
122 |
+
})
|
123 |
+
|
124 |
+
st.write('### Generated Explanations')
|
125 |
+
st.dataframe(result_df_gen)
|
126 |
+
|
127 |
+
# Create a CSV download link
|
128 |
+
csv_gen = result_df_gen.to_csv(index=False)
|
129 |
+
st.download_button(
|
130 |
+
label="Download generated explanations as CSV",
|
131 |
+
data=csv_gen,
|
132 |
+
file_name='generated_explanations.csv',
|
133 |
+
mime='text/csv',
|
134 |
+
)
|