Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
|
|
2 |
import textgrad as tg
|
3 |
import os
|
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:
|
@@ -29,21 +29,23 @@ initial_solution = st.text_area("Initial Solution", st.session_state.get("initia
|
|
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 |
-
#
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
2 |
import textgrad as tg
|
3 |
import os
|
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:
|
|
|
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 |
+
# Enter button
|
33 |
+
if st.button("Enter"):
|
34 |
+
# Set up the textgrad variables
|
35 |
+
solution = tg.Variable(initial_solution,
|
36 |
+
requires_grad=True,
|
37 |
+
role_description="solution to the math question")
|
38 |
+
|
39 |
+
loss_fn = tg.TextLoss(tg.Variable(loss_system_prompt,
|
40 |
+
requires_grad=False,
|
41 |
+
role_description="system prompt"))
|
42 |
+
optimizer = tg.TGD([solution])
|
43 |
+
|
44 |
+
# Training loop
|
45 |
+
for i in range(num_epochs):
|
46 |
+
loss = loss_fn(solution)
|
47 |
+
loss.backward()
|
48 |
+
optimizer.step()
|
49 |
+
|
50 |
+
# Output box
|
51 |
+
st.text_area("Result", solution.value)
|