Spaces:
Build error
Build error
Require user to enter OpenAI API key
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
from langchain import OpenAI
|
@@ -7,6 +8,18 @@ import datetime
|
|
7 |
gpt_only_prompt = "Calculate the following, giving only the final answer:\n"
|
8 |
prompt = ""
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def openai_create(prompt):
|
11 |
print("prompt: " + prompt)
|
12 |
|
@@ -27,19 +40,19 @@ def openai_create(prompt):
|
|
27 |
def calc_gpt_only(math_problem):
|
28 |
answer = openai_create(gpt_only_prompt + math_problem + "\n")
|
29 |
|
30 |
-
print("
|
|
|
31 |
print("calc_gpt_only answer: " + answer)
|
32 |
|
33 |
html = "<pre>" + answer + "</pre>"
|
34 |
return html
|
35 |
|
36 |
|
37 |
-
def calc_gpt_pal(math_problem):
|
38 |
-
llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)
|
39 |
-
pal_chain = PALChain.from_math_prompt(llm, verbose=True)
|
40 |
answer = pal_chain.run(math_problem)
|
41 |
|
42 |
-
print("
|
|
|
43 |
print("calc_gpt_pal answer: " + answer)
|
44 |
|
45 |
html = "<pre>" + answer + "</pre>"
|
@@ -49,7 +62,12 @@ def calc_gpt_pal(math_problem):
|
|
49 |
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
50 |
|
51 |
with block:
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
53 |
answer_html = gr.Markdown()
|
54 |
|
55 |
request = gr.Textbox(label="Math question:",
|
@@ -62,7 +80,11 @@ with block:
|
|
62 |
examples=["42 times 81",
|
63 |
"Olivia has $23. She bought five bagels for $3 each. How much money does she have left?",
|
64 |
"What is the sum of the first 21 Fibonacci numbers?",
|
65 |
-
"Jane quit her job on Mar 20, 2020. 176 days have passed since then. What is the date tomorrow in
|
|
|
|
|
|
|
|
|
66 |
inputs=request
|
67 |
)
|
68 |
|
@@ -76,7 +98,13 @@ with block:
|
|
76 |
|
77 |
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain π¦οΈπ</a></center>")
|
78 |
|
|
|
|
|
79 |
gpt_only.click(calc_gpt_only, inputs=[request], outputs=[answer_html])
|
80 |
-
gpt_pal.click(calc_gpt_pal, inputs=[request], outputs=[answer_html])
|
|
|
|
|
|
|
|
|
81 |
|
82 |
block.launch()
|
|
|
1 |
+
import os
|
2 |
import openai
|
3 |
import gradio as gr
|
4 |
from langchain import OpenAI
|
|
|
8 |
gpt_only_prompt = "Calculate the following, giving only the final answer:\n"
|
9 |
prompt = ""
|
10 |
|
11 |
+
os.environ["OPENAI_API_KEY"] = ""
|
12 |
+
|
13 |
+
|
14 |
+
def set_openai_api_key(api_key, pal_chain_sta):
|
15 |
+
if api_key != "":
|
16 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
17 |
+
|
18 |
+
llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)
|
19 |
+
pal_chain = PALChain.from_math_prompt(llm, verbose=True)
|
20 |
+
return pal_chain
|
21 |
+
|
22 |
+
|
23 |
def openai_create(prompt):
|
24 |
print("prompt: " + prompt)
|
25 |
|
|
|
40 |
def calc_gpt_only(math_problem):
|
41 |
answer = openai_create(gpt_only_prompt + math_problem + "\n")
|
42 |
|
43 |
+
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
|
44 |
+
print("calc_gpt_only math problem: " + math_problem)
|
45 |
print("calc_gpt_only answer: " + answer)
|
46 |
|
47 |
html = "<pre>" + answer + "</pre>"
|
48 |
return html
|
49 |
|
50 |
|
51 |
+
def calc_gpt_pal(math_problem, pal_chain):
|
|
|
|
|
52 |
answer = pal_chain.run(math_problem)
|
53 |
|
54 |
+
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
|
55 |
+
print("calc_gpt_pal math problem: " + math_problem)
|
56 |
print("calc_gpt_pal answer: " + answer)
|
57 |
|
58 |
html = "<pre>" + answer + "</pre>"
|
|
|
62 |
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
63 |
|
64 |
with block:
|
65 |
+
with gr.Row():
|
66 |
+
title = gr.Markdown("""<h3><center>Comparing GPT math techniques</center></h3>""")
|
67 |
+
|
68 |
+
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...)",
|
69 |
+
show_label=False, lines=1, type='password')
|
70 |
+
|
71 |
answer_html = gr.Markdown()
|
72 |
|
73 |
request = gr.Textbox(label="Math question:",
|
|
|
80 |
examples=["42 times 81",
|
81 |
"Olivia has $23. She bought five bagels for $3 each. How much money does she have left?",
|
82 |
"What is the sum of the first 21 Fibonacci numbers?",
|
83 |
+
"Jane quit her job on Mar 20, 2020. 176 days have passed since then. What is the date tomorrow in "
|
84 |
+
"MM/DD/YYYY?",
|
85 |
+
"If y = 8β5x+4x2, what is the value of y when x = β2?",
|
86 |
+
"A line parallel to y = 4x + 6 passes through (5, 10). What is the y-coordinate of the point where "
|
87 |
+
"this line crosses the y-axis?"],
|
88 |
inputs=request
|
89 |
)
|
90 |
|
|
|
98 |
|
99 |
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain π¦οΈπ</a></center>")
|
100 |
|
101 |
+
pal_chain_state = gr.State()
|
102 |
+
|
103 |
gpt_only.click(calc_gpt_only, inputs=[request], outputs=[answer_html])
|
104 |
+
gpt_pal.click(calc_gpt_pal, inputs=[request, pal_chain_state], outputs=[answer_html])
|
105 |
+
|
106 |
+
openai_api_key_textbox.change(set_openai_api_key,
|
107 |
+
inputs=[openai_api_key_textbox, pal_chain_state],
|
108 |
+
outputs=[pal_chain_state])
|
109 |
|
110 |
block.launch()
|