Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,9 +2,9 @@ import os
|
|
2 |
import streamlit as st
|
3 |
import openai
|
4 |
import pandas as pd
|
5 |
-
import
|
6 |
-
from typing import List, Tuple # Add Tuple import here
|
7 |
from uuid import uuid4
|
|
|
8 |
|
9 |
# π Set the OpenAI API key from an environment variable
|
10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
@@ -15,30 +15,11 @@ def get_session_id():
|
|
15 |
st.session_state.session_id = str(uuid4())
|
16 |
return st.session_state.session_id
|
17 |
|
18 |
-
# π Predefined examples loaded from Python dictionaries
|
19 |
-
EXAMPLES = [
|
20 |
-
{
|
21 |
-
'Problem': 'What is deductive reasoning?',
|
22 |
-
'Rationale': 'Deductive reasoning starts from general premises to arrive at a specific conclusion.',
|
23 |
-
'Answer': 'It involves deriving specific conclusions from general premises.'
|
24 |
-
},
|
25 |
-
{
|
26 |
-
'Problem': 'What is inductive reasoning?',
|
27 |
-
'Rationale': 'Inductive reasoning involves drawing generalizations based on specific observations.',
|
28 |
-
'Answer': 'It involves forming general rules from specific examples.'
|
29 |
-
},
|
30 |
-
{
|
31 |
-
'Problem': 'Explain abductive reasoning.',
|
32 |
-
'Rationale': 'Abductive reasoning finds the most likely explanation for incomplete observations.',
|
33 |
-
'Answer': 'It involves finding the best possible explanation.'
|
34 |
-
}
|
35 |
-
]
|
36 |
-
|
37 |
# π§ STaR Algorithm Implementation
|
38 |
class SelfTaughtReasoner:
|
39 |
def __init__(self, model_engine="text-davinci-003"):
|
40 |
self.model_engine = model_engine
|
41 |
-
self.prompt_examples =
|
42 |
self.iterations = 0
|
43 |
self.generated_data = pd.DataFrame(columns=['Problem', 'Rationale', 'Answer', 'Is_Correct'])
|
44 |
self.rationalized_data = pd.DataFrame(columns=['Problem', 'Rationale', 'Answer', 'Is_Correct'])
|
@@ -150,6 +131,19 @@ class SelfTaughtReasoner:
|
|
150 |
self.fine_tune_model()
|
151 |
self.iterations += 1
|
152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
# π₯οΈ Streamlit App
|
154 |
def main():
|
155 |
st.title("π€ Self-Taught Reasoner (STaR) Demonstration")
|
@@ -160,64 +154,47 @@ def main():
|
|
160 |
|
161 |
star = st.session_state.star
|
162 |
|
163 |
-
# π Wide format layout
|
164 |
-
col1, col2 = st.columns([1, 2]) # Column widths: col1 for input, col2 for display
|
165 |
-
|
166 |
# Step 1: Few-Shot Prompt Examples
|
167 |
-
|
168 |
-
|
169 |
-
st.write("Choose an example from the dropdown or input your own.")
|
170 |
-
|
171 |
-
selected_example = st.selectbox(
|
172 |
-
"Select a predefined example",
|
173 |
-
[f"Example {i + 1}: {ex['Problem']}" for i, ex in enumerate(EXAMPLES)]
|
174 |
-
)
|
175 |
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
example_answer = EXAMPLES[example_idx]['Answer']
|
181 |
|
182 |
-
|
183 |
-
|
184 |
-
|
|
|
185 |
|
186 |
-
|
187 |
-
|
188 |
-
st.success("Example added successfully!")
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
st.subheader("Current Prompt Examples:")
|
194 |
-
for idx, example in enumerate(star.prompt_examples):
|
195 |
-
st.write(f"**Example {idx + 1}:**")
|
196 |
-
st.write(f"Problem: {example['Problem']}")
|
197 |
-
st.write(f"Rationale: {example['Rationale']}")
|
198 |
-
st.write(f"Answer: {example['Answer']}")
|
199 |
|
200 |
-
# Step 2: Input Dataset
|
201 |
st.header("Step 2: Input Dataset")
|
202 |
-
dataset_input_method = st.radio("How would you like to input the dataset?", ("Manual Entry", "Upload CSV"))
|
203 |
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
|
222 |
if 'dataset' in st.session_state:
|
223 |
st.subheader("Current Dataset:")
|
@@ -252,14 +229,5 @@ def main():
|
|
252 |
st.subheader("Answer:")
|
253 |
st.write(answer)
|
254 |
|
255 |
-
# Footer with custom HTML/JS component
|
256 |
-
st.markdown("---")
|
257 |
-
st.write("Developed as a demonstration of the STaR method with enhanced Streamlit capabilities.")
|
258 |
-
st.components.v1.html("""
|
259 |
-
<div style="text-align: center; margin-top: 20px;">
|
260 |
-
<h3>π Boost Your AI Reasoning with STaR! π</h3>
|
261 |
-
</div>
|
262 |
-
""")
|
263 |
-
|
264 |
if __name__ == "__main__":
|
265 |
main()
|
|
|
2 |
import streamlit as st
|
3 |
import openai
|
4 |
import pandas as pd
|
5 |
+
from typing import List, Tuple
|
|
|
6 |
from uuid import uuid4
|
7 |
+
import time
|
8 |
|
9 |
# π Set the OpenAI API key from an environment variable
|
10 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
15 |
st.session_state.session_id = str(uuid4())
|
16 |
return st.session_state.session_id
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# π§ STaR Algorithm Implementation
|
19 |
class SelfTaughtReasoner:
|
20 |
def __init__(self, model_engine="text-davinci-003"):
|
21 |
self.model_engine = model_engine
|
22 |
+
self.prompt_examples = [] # Initialize with an empty list
|
23 |
self.iterations = 0
|
24 |
self.generated_data = pd.DataFrame(columns=['Problem', 'Rationale', 'Answer', 'Is_Correct'])
|
25 |
self.rationalized_data = pd.DataFrame(columns=['Problem', 'Rationale', 'Answer', 'Is_Correct'])
|
|
|
131 |
self.fine_tune_model()
|
132 |
self.iterations += 1
|
133 |
|
134 |
+
# Predefined problem and answer list
|
135 |
+
EXAMPLE_PROBLEM_ANSWERS = [
|
136 |
+
{"Problem": "What is deductive reasoning?", "Answer": "It is a logical process that draws specific conclusions from general principles."},
|
137 |
+
{"Problem": "What is inductive reasoning?", "Answer": "It is reasoning that forms general principles from specific examples."},
|
138 |
+
{"Problem": "Explain abductive reasoning.", "Answer": "It involves finding the best explanation for incomplete observations."},
|
139 |
+
{"Problem": "What is the capital of France?", "Answer": "Paris."},
|
140 |
+
{"Problem": "Who wrote Hamlet?", "Answer": "William Shakespeare."}
|
141 |
+
]
|
142 |
+
|
143 |
+
# Convert the example list into 'Problem | Answer' format
|
144 |
+
def format_examples_for_text_area(examples):
|
145 |
+
return '\n'.join([f"{example['Problem']} | {example['Answer']}" for example in examples])
|
146 |
+
|
147 |
# π₯οΈ Streamlit App
|
148 |
def main():
|
149 |
st.title("π€ Self-Taught Reasoner (STaR) Demonstration")
|
|
|
154 |
|
155 |
star = st.session_state.star
|
156 |
|
|
|
|
|
|
|
157 |
# Step 1: Few-Shot Prompt Examples
|
158 |
+
st.header("Step 1: Add Few-Shot Prompt Examples")
|
159 |
+
st.write("Choose an example from the dropdown or input your own.")
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
+
selected_example = st.selectbox(
|
162 |
+
"Select a predefined example",
|
163 |
+
[f"Example {i + 1}: {ex['Problem']}" for i, ex in enumerate(EXAMPLE_PROBLEM_ANSWERS)]
|
164 |
+
)
|
|
|
165 |
|
166 |
+
# Prefill with selected example
|
167 |
+
example_idx = int(selected_example.split(" ")[1].replace(":", "")) - 1
|
168 |
+
example_problem = EXAMPLE_PROBLEM_ANSWERS[example_idx]['Problem']
|
169 |
+
example_answer = EXAMPLE_PROBLEM_ANSWERS[example_idx]['Answer']
|
170 |
|
171 |
+
st.text_area("Problem", value=example_problem, height=50, key="example_problem")
|
172 |
+
st.text_input("Answer", value=example_answer, key="example_answer")
|
|
|
173 |
|
174 |
+
if st.button("Add Example"):
|
175 |
+
star.add_prompt_example(st.session_state.example_problem, "Rationale placeholder", st.session_state.example_answer)
|
176 |
+
st.success("Example added successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
|
178 |
+
# Step 2: Input Dataset (Problem | Answer format)
|
179 |
st.header("Step 2: Input Dataset")
|
|
|
180 |
|
181 |
+
# Provide examples in the format 'Problem | Answer' as a default
|
182 |
+
prefilled_data = format_examples_for_text_area(EXAMPLE_PROBLEM_ANSWERS)
|
183 |
+
dataset_problems = st.text_area(
|
184 |
+
"Enter problems and answers in the format 'Problem | Answer', one per line.",
|
185 |
+
value=prefilled_data,
|
186 |
+
height=200
|
187 |
+
)
|
188 |
+
|
189 |
+
if st.button("Submit Dataset"):
|
190 |
+
dataset = []
|
191 |
+
lines = dataset_problems.strip().split('\n')
|
192 |
+
for line in lines:
|
193 |
+
if '|' in line:
|
194 |
+
problem, answer = line.split('|', 1)
|
195 |
+
dataset.append({'Problem': problem.strip(), 'Answer': answer.strip()})
|
196 |
+
st.session_state.dataset = pd.DataFrame(dataset)
|
197 |
+
st.success("Dataset loaded.")
|
198 |
|
199 |
if 'dataset' in st.session_state:
|
200 |
st.subheader("Current Dataset:")
|
|
|
229 |
st.subheader("Answer:")
|
230 |
st.write(answer)
|
231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
232 |
if __name__ == "__main__":
|
233 |
main()
|