JLW's picture
Debug OpenAI API key issue
0b99565
raw
history blame
3.23 kB
import os
import openai
import gradio as gr
import datetime
gpt_temperature = 0.1
initial_prompt = "List the following in a HTML table with about 10 rows in an appropriate order, giving the table an appropriate caption. The table contains about 5 columns with appropriate attributes. Each non-numeric cell in the table, regardless of column, contains a link whenever possible to an appropriate Wikipedia page that opens in a new tab. No other links besides Wikipedia pages are permitted: "
title_prompt = "Write a title without quotation marks for a table that contains the following information: "
prompt = ""
def set_openai_api_key(api_key, openai_api_key):
if api_key:
openai_api_key = api_key
return openai_api_key
def openai_create(prompt, openai_api_key):
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
print("prompt: " + prompt)
print("gpt_temperature: ", gpt_temperature)
print("in openai_create openai_api_key: ", openai_api_key)
#os.environ["OPENAI_API_KEY"] = openai_api_key
openai.api_key = openai_api_key
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=gpt_temperature,
max_tokens=2000,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
#os.environ["OPENAI_API_KEY"] = ""
return response.choices[0].text
def desc2sheet(desc, openai_api_key):
if not openai_api_key or openai_api_key == "":
return "<pre>Please paste your OpenAI API key</pre>"
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
print("desc: " + desc)
print("gpt_temperature: ", gpt_temperature)
print("in desc2sheet openai_api_key: ", openai_api_key)
html = openai_create(initial_prompt + desc + '\n', openai_api_key)
return html
def update_temperature(temperature):
global gpt_temperature
gpt_temperature = temperature
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
with block:
with gr.Row():
temperature = gr.Slider(label="GPT Temperature", value=gpt_temperature, minimum=0.0, maximum=1.0, step=0.1)
title = gr.Markdown("""<h3><center>GPT-3.5 Table-inator</center></h3>""")
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...)",
show_label=False, lines=1, type='password')
table = gr.Markdown("")
with gr.Row():
request = gr.Textbox(label=initial_prompt,
placeholder="Ex: 12 computer languages and their inventors by year of birth")
submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
openai_api_key_state = gr.State()
submit.click(desc2sheet, inputs=[request, openai_api_key_state], outputs=[table])
request.submit(desc2sheet, inputs=[request, openai_api_key_state], outputs=[table])
openai_api_key_textbox.change(set_openai_api_key,
inputs=[openai_api_key_textbox, openai_api_key_state],
outputs=[openai_api_key_state])
temperature.change(update_temperature, temperature)
block.launch()