huangzhii
commited on
Commit
·
7d02ab7
1
Parent(s):
36c43cc
bug fixedd
Browse files- examples/code_editor_scripts.py +46 -24
- examples/example_math_scripts.py +25 -8
examples/code_editor_scripts.py
CHANGED
@@ -7,6 +7,10 @@ import os
|
|
7 |
class CodeEditor:
|
8 |
def __init__(self, data) -> None:
|
9 |
self.data = data
|
|
|
|
|
|
|
|
|
10 |
self.llm_engine = tg.get_engine("gpt-4o")
|
11 |
print("="*50, "init", "="*50)
|
12 |
self.loss_value = ""
|
@@ -39,29 +43,30 @@ class CodeEditor:
|
|
39 |
st.session_state.code_content = self.data["default_initial_solution"]
|
40 |
|
41 |
def update_code_content(value):
|
42 |
-
st.session_state.
|
43 |
-
|
|
|
44 |
|
45 |
col1, col2 = st.columns(2)
|
46 |
with col1:
|
47 |
-
with elements("
|
48 |
-
|
49 |
-
editor.Monaco(
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
)
|
64 |
-
|
65 |
|
66 |
def _run(self):
|
67 |
# Code is the variable of interest we want to optimize -- so requires_grad=True
|
@@ -114,21 +119,38 @@ class CodeEditor:
|
|
114 |
st.session_state.results.append({
|
115 |
'iteration': st.session_state.iteration,
|
116 |
'loss_value': self.loss_value,
|
117 |
-
'gradients': self.gradients
|
|
|
118 |
})
|
119 |
|
120 |
tabs = st.tabs([f"Iteration {i+1}" for i in range(st.session_state.iteration)])
|
121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
122 |
for i, tab in enumerate(tabs):
|
123 |
with tab:
|
124 |
result = st.session_state.results[i]
|
125 |
st.markdown(f"Current iteration: **{result['iteration']}**")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
col1, col2 = st.columns([1, 1])
|
127 |
with col1:
|
128 |
-
st.markdown("
|
|
|
129 |
st.markdown(result['loss_value'])
|
130 |
with col2:
|
131 |
-
st.markdown("
|
132 |
for j, g in enumerate(result['gradients']):
|
133 |
-
st.markdown(f"### Gradient {j}")
|
134 |
st.markdown(g.value)
|
|
|
7 |
class CodeEditor:
|
8 |
def __init__(self, data) -> None:
|
9 |
self.data = data
|
10 |
+
# Initialize only if not already set to ensure it retains the original content
|
11 |
+
if 'original_code_content' not in st.session_state:
|
12 |
+
st.session_state.original_code_content = self.data["default_initial_solution"]
|
13 |
+
|
14 |
self.llm_engine = tg.get_engine("gpt-4o")
|
15 |
print("="*50, "init", "="*50)
|
16 |
self.loss_value = ""
|
|
|
43 |
st.session_state.code_content = self.data["default_initial_solution"]
|
44 |
|
45 |
def update_code_content(value):
|
46 |
+
if st.session_state.iteration == 0:
|
47 |
+
st.session_state.code_content = value
|
48 |
+
# print(f"Code updated: {st.session_state.code_content}")
|
49 |
|
50 |
col1, col2 = st.columns(2)
|
51 |
with col1:
|
52 |
+
with elements("monaco_editors_widget_original"):
|
53 |
+
st.markdown(f"**Initial solution:**")
|
54 |
+
# code = editor.Monaco(
|
55 |
+
# height=300,
|
56 |
+
# defaultLanguage="python",
|
57 |
+
# defaultValue=st.session_state.original_code_content,
|
58 |
+
# onChange=update_code_content,
|
59 |
+
# label="Initial Solution Viewer",
|
60 |
+
# )
|
61 |
+
|
62 |
+
code = st.text_area("Edit your code here:", st.session_state.original_code_content, height=300)
|
63 |
+
# Update session state when text changes
|
64 |
+
if code is not None and st.session_state.original_code_content != code:
|
65 |
+
update_code_content(code)
|
66 |
+
|
67 |
+
# if st.session_state.code_content != code:
|
68 |
+
# update_code_content(code)
|
69 |
+
# with col2:
|
70 |
|
71 |
def _run(self):
|
72 |
# Code is the variable of interest we want to optimize -- so requires_grad=True
|
|
|
119 |
st.session_state.results.append({
|
120 |
'iteration': st.session_state.iteration,
|
121 |
'loss_value': self.loss_value,
|
122 |
+
'gradients': self.gradients,
|
123 |
+
'code_content': st.session_state.code_content,
|
124 |
})
|
125 |
|
126 |
tabs = st.tabs([f"Iteration {i+1}" for i in range(st.session_state.iteration)])
|
127 |
|
128 |
+
# Include Highlight.js library and a theme CSS
|
129 |
+
st.markdown("""
|
130 |
+
<link rel="stylesheet"
|
131 |
+
href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/styles/default.min.css">
|
132 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.0/highlight.min.js"></script>
|
133 |
+
<script>hljs.highlightAll();</script>
|
134 |
+
""", unsafe_allow_html=True)
|
135 |
+
|
136 |
for i, tab in enumerate(tabs):
|
137 |
with tab:
|
138 |
result = st.session_state.results[i]
|
139 |
st.markdown(f"Current iteration: **{result['iteration']}**")
|
140 |
+
|
141 |
+
st.markdown("### Current solution")
|
142 |
+
st.markdown(f"""
|
143 |
+
<pre><code class="language-python">{result["code_content"]}</code></pre>
|
144 |
+
""", unsafe_allow_html=True)
|
145 |
+
|
146 |
+
|
147 |
col1, col2 = st.columns([1, 1])
|
148 |
with col1:
|
149 |
+
st.markdown("### Loss value")
|
150 |
+
st.markdown("**Loss value is based on previous code.**")
|
151 |
st.markdown(result['loss_value'])
|
152 |
with col2:
|
153 |
+
st.markdown("### Code gradients")
|
154 |
for j, g in enumerate(result['gradients']):
|
155 |
+
# st.markdown(f"### Gradient {j}")
|
156 |
st.markdown(g.value)
|
examples/example_math_scripts.py
CHANGED
@@ -8,6 +8,15 @@ import os
|
|
8 |
class MathSolution:
|
9 |
def __init__(self, data) -> None:
|
10 |
self.data = data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
self.llm_engine = tg.get_engine("gpt-4o")
|
12 |
print("="*50, "init", "="*50)
|
13 |
self.loss_value = ""
|
@@ -18,15 +27,22 @@ class MathSolution:
|
|
18 |
tg.set_backward_engine(self.llm_engine, override=True)
|
19 |
|
20 |
def load_layout(self):
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
col1, col2 = st.columns([1, 1])
|
22 |
with col1:
|
23 |
-
self.initial_solution = st.text_area("Initial solution:", self.data["default_initial_solution"], height=300)
|
24 |
-
with col2:
|
25 |
-
self.loss_system_prompt = st.text_area("Loss system prompt:", self.data["default_loss_system_prompt"], height=300)
|
26 |
-
|
27 |
-
if "current_solution" not in st.session_state:
|
28 |
-
st.session_state.current_solution = self.data["default_initial_solution"]
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def _run(self):
|
32 |
# Set up the textgrad variables
|
@@ -71,10 +87,11 @@ class MathSolution:
|
|
71 |
st.markdown(result['response'])
|
72 |
col1, col2 = st.columns([1, 1])
|
73 |
with col1:
|
74 |
-
st.markdown("
|
|
|
75 |
st.markdown(result['loss_value'])
|
76 |
with col2:
|
77 |
-
st.markdown("
|
78 |
for j, g in enumerate(result['gradients']):
|
79 |
st.markdown(f"### Gradient")
|
80 |
st.markdown(g.value)
|
|
|
8 |
class MathSolution:
|
9 |
def __init__(self, data) -> None:
|
10 |
self.data = data
|
11 |
+
if 'default_initial_solution' not in st.session_state:
|
12 |
+
st.session_state.default_initial_solution = self.data["default_initial_solution"]
|
13 |
+
if 'loss_system_prompt' not in st.session_state:
|
14 |
+
st.session_state.loss_system_prompt = self.data["default_loss_system_prompt"]
|
15 |
+
if 'instruction' not in st.session_state:
|
16 |
+
st.session_state.instruction = self.data["instruction"]
|
17 |
+
if 'current_solution' not in st.session_state:
|
18 |
+
st.session_state.current_solution = self.data["default_initial_solution"]
|
19 |
+
|
20 |
self.llm_engine = tg.get_engine("gpt-4o")
|
21 |
print("="*50, "init", "="*50)
|
22 |
self.loss_value = ""
|
|
|
27 |
tg.set_backward_engine(self.llm_engine, override=True)
|
28 |
|
29 |
def load_layout(self):
|
30 |
+
|
31 |
+
def update_solution_content(value):
|
32 |
+
if st.session_state.iteration == 0:
|
33 |
+
st.session_state.current_solution = value
|
34 |
+
# print(f"Code updated: {st.session_state.code_content}")
|
35 |
+
|
36 |
col1, col2 = st.columns([1, 1])
|
37 |
with col1:
|
38 |
+
# self.initial_solution = st.text_area("Initial solution:", self.data["default_initial_solution"], height=300)
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
solution = st.text_area("Initial solution:", st.session_state.default_initial_solution, height=300)
|
41 |
+
# Update session state when text changes
|
42 |
+
if solution is not None and st.session_state.default_initial_solution != solution:
|
43 |
+
update_solution_content(solution)
|
44 |
+
with col2:
|
45 |
+
self.loss_system_prompt = st.text_area("Loss system prompt:", st.session_state.loss_system_prompt, height=300)
|
46 |
|
47 |
def _run(self):
|
48 |
# Set up the textgrad variables
|
|
|
87 |
st.markdown(result['response'])
|
88 |
col1, col2 = st.columns([1, 1])
|
89 |
with col1:
|
90 |
+
st.markdown("### Loss value")
|
91 |
+
st.markdown("**Loss value is based on previous code.**")
|
92 |
st.markdown(result['loss_value'])
|
93 |
with col2:
|
94 |
+
st.markdown("### Code gradients")
|
95 |
for j, g in enumerate(result['gradients']):
|
96 |
st.markdown(f"### Gradient")
|
97 |
st.markdown(g.value)
|