JLW commited on
Commit
6e66ac2
1 Parent(s): 663beb6

Add python code to displayed answer

Browse files
Files changed (1) hide show
  1. app.py +30 -1
app.py CHANGED
@@ -5,6 +5,19 @@ from langchain import OpenAI
5
  from langchain.chains import PALChain
6
  import datetime
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  gpt_only_prompt = "Calculate the following, giving only the final answer:\n"
9
 
10
 
@@ -57,13 +70,29 @@ def calc_gpt_pal(math_problem, pal_chain):
57
  if not pal_chain:
58
  return "<pre>Please paste your OpenAI API key</pre>"
59
 
 
 
 
 
60
  answer = pal_chain.run(math_problem)
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
63
  print("calc_gpt_pal math problem: " + math_problem)
64
  print("calc_gpt_pal answer: " + answer)
65
 
66
- html = "<pre>" + answer + "</pre>"
67
  return html
68
 
69
 
 
5
  from langchain.chains import PALChain
6
  import datetime
7
 
8
+ # Console to variable
9
+ from io import StringIO
10
+ import sys
11
+
12
+
13
+ # tmp = sys.stdout
14
+ # my_result = StringIO()
15
+ # sys.stdout = my_result
16
+ # print("HELLO");
17
+ # sys.stdout = tmp
18
+ # print(my_result.getvalue().lower())
19
+
20
+
21
  gpt_only_prompt = "Calculate the following, giving only the final answer:\n"
22
 
23
 
 
70
  if not pal_chain:
71
  return "<pre>Please paste your OpenAI API key</pre>"
72
 
73
+ import re
74
+ tmp = sys.stdout
75
+ python_code_str_io = StringIO()
76
+ sys.stdout = python_code_str_io
77
  answer = pal_chain.run(math_problem)
78
+ sys.stdout = tmp
79
+ python_code = python_code_str_io.getvalue()
80
+
81
+ # Keep everything after the second set of """ in python_code
82
+ python_code = re.sub(r'.*"""', '', python_code, flags=re.DOTALL)
83
+
84
+ # Remove everything after the first [0m in python_code
85
+ python_code = re.sub(r'\[0m.*', '', python_code, flags=re.DOTALL)
86
+
87
+ # Remove leading and trailing whitespace from each line in python_code
88
+ python_code = os.linesep.join([s.strip() for s in python_code.splitlines()])
89
+
90
 
91
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
92
  print("calc_gpt_pal math problem: " + math_problem)
93
  print("calc_gpt_pal answer: " + answer)
94
 
95
+ html = "<pre>" + python_code + "\n\n" + answer + "</pre>"
96
  return html
97
 
98