Spaces:
Build error
Build error
Put OpenAI API key only temporarily in environment variable
Browse files
app.py
CHANGED
@@ -8,22 +8,26 @@ import datetime
|
|
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,
|
15 |
-
if api_key
|
16 |
-
|
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 |
|
26 |
# We use temperature of 0.0 because it gives the most predictable, factual answer (i.e. avoids hallucination).
|
|
|
27 |
response = openai.Completion.create(
|
28 |
model="text-davinci-003",
|
29 |
prompt=prompt,
|
@@ -33,12 +37,13 @@ def openai_create(prompt):
|
|
33 |
frequency_penalty=0,
|
34 |
presence_penalty=0
|
35 |
)
|
|
|
36 |
|
37 |
return response.choices[0].text
|
38 |
|
39 |
|
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)
|
@@ -98,13 +103,14 @@ with block:
|
|
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()
|
|
|
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, openai_api_key, pal_chain):
|
15 |
+
if api_key:
|
16 |
+
openai_api_key = api_key
|
17 |
|
18 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
19 |
llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512)
|
20 |
+
os.environ["OPENAI_API_KEY"] = ""
|
21 |
+
|
22 |
pal_chain = PALChain.from_math_prompt(llm, verbose=True)
|
23 |
+
return openai_api_key, pal_chain
|
24 |
|
25 |
|
26 |
+
def openai_create(prompt, openai_api_key):
|
27 |
print("prompt: " + prompt)
|
28 |
|
29 |
# We use temperature of 0.0 because it gives the most predictable, factual answer (i.e. avoids hallucination).
|
30 |
+
os.environ["OPENAI_API_KEY"] = openai_api_key
|
31 |
response = openai.Completion.create(
|
32 |
model="text-davinci-003",
|
33 |
prompt=prompt,
|
|
|
37 |
frequency_penalty=0,
|
38 |
presence_penalty=0
|
39 |
)
|
40 |
+
os.environ["OPENAI_API_KEY"] = ""
|
41 |
|
42 |
return response.choices[0].text
|
43 |
|
44 |
|
45 |
+
def calc_gpt_only(math_problem, openai_api_key):
|
46 |
+
answer = openai_create(gpt_only_prompt + math_problem + "\n", openai_api_key)
|
47 |
|
48 |
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
|
49 |
print("calc_gpt_only math problem: " + math_problem)
|
|
|
103 |
|
104 |
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
|
105 |
|
106 |
+
openai_api_key_state = gr.State()
|
107 |
pal_chain_state = gr.State()
|
108 |
|
109 |
+
gpt_only.click(calc_gpt_only, inputs=[request, openai_api_key_state], outputs=[answer_html])
|
110 |
gpt_pal.click(calc_gpt_pal, inputs=[request, pal_chain_state], outputs=[answer_html])
|
111 |
|
112 |
openai_api_key_textbox.change(set_openai_api_key,
|
113 |
+
inputs=[openai_api_key_textbox, openai_api_key_state, pal_chain_state],
|
114 |
+
outputs=[openai_api_key_state, pal_chain_state])
|
115 |
|
116 |
block.launch()
|