Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,8 @@ import os
|
|
4 |
|
5 |
tg.set_backward_engine(tg.get_engine("gpt-4o"))
|
6 |
|
7 |
-
|
|
|
8 |
x = (-b Β± β(b^2 - 4ac)) / 2a
|
9 |
a = 3, b = -7, c = 2
|
10 |
x = (7 Β± β((-7)^2 + 4(3)(2))) / 6
|
@@ -13,21 +14,36 @@ The solutions are:
|
|
13 |
x1 = (7 + β73)
|
14 |
x2 = (7 - β73)"""
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
solution = tg.Variable(initial_solution,
|
17 |
requires_grad=True,
|
18 |
role_description="solution to the math question")
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
role_description="system prompt")
|
24 |
-
|
25 |
-
loss_fn = tg.TextLoss(loss_system_prompt)
|
26 |
optimizer = tg.TGD([solution])
|
27 |
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
st.write(solution.value)
|
|
|
4 |
|
5 |
tg.set_backward_engine(tg.get_engine("gpt-4o"))
|
6 |
|
7 |
+
# Hardcoded examples
|
8 |
+
default_initial_solution = """To solve the equation 3x^2 - 7x + 2 = 0, we use the quadratic formula:
|
9 |
x = (-b Β± β(b^2 - 4ac)) / 2a
|
10 |
a = 3, b = -7, c = 2
|
11 |
x = (7 Β± β((-7)^2 + 4(3)(2))) / 6
|
|
|
14 |
x1 = (7 + β73)
|
15 |
x2 = (7 - β73)"""
|
16 |
|
17 |
+
default_loss_system_prompt = """You will evaluate a solution to a math question.
|
18 |
+
Do not attempt to solve it yourself, do not give a solution, only identify errors. Be super concise."""
|
19 |
+
|
20 |
+
# Display clickable examples
|
21 |
+
if st.button("Use Example Initial Solution"):
|
22 |
+
st.session_state.initial_solution = default_initial_solution
|
23 |
+
|
24 |
+
if st.button("Use Example Loss System Prompt"):
|
25 |
+
st.session_state.loss_system_prompt = default_loss_system_prompt
|
26 |
+
|
27 |
+
# Input boxes
|
28 |
+
initial_solution = st.text_area("Initial Solution", st.session_state.get("initial_solution", ""))
|
29 |
+
loss_system_prompt = st.text_area("Loss System Prompt", st.session_state.get("loss_system_prompt", ""))
|
30 |
+
num_epochs = st.number_input("Epochs", min_value=1, value=1)
|
31 |
+
|
32 |
+
# Set up the textgrad variables
|
33 |
solution = tg.Variable(initial_solution,
|
34 |
requires_grad=True,
|
35 |
role_description="solution to the math question")
|
36 |
|
37 |
+
loss_fn = tg.TextLoss(tg.Variable(loss_system_prompt,
|
38 |
+
requires_grad=False,
|
39 |
+
role_description="system prompt"))
|
|
|
|
|
|
|
40 |
optimizer = tg.TGD([solution])
|
41 |
|
42 |
+
# Training loop
|
43 |
+
for i in range(num_epochs):
|
44 |
+
loss = loss_fn(solution)
|
45 |
+
loss.backward()
|
46 |
+
optimizer.step()
|
47 |
|
48 |
+
# Output box
|
49 |
+
st.text_area("Result", solution.value)
|
|