JLW commited on
Commit
a761657
·
1 Parent(s): 0b99565

Debug OpenAI API key issue

Browse files
Files changed (1) hide show
  1. app.py +21 -24
app.py CHANGED
@@ -1,13 +1,10 @@
1
- import os
2
  import openai
3
  import gradio as gr
4
  import datetime
5
 
6
- gpt_temperature = 0.1
7
 
8
- 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: "
9
- title_prompt = "Write a title without quotation marks for a table that contains the following information: "
10
- prompt = ""
11
 
12
 
13
  def set_openai_api_key(api_key, openai_api_key):
@@ -16,50 +13,47 @@ def set_openai_api_key(api_key, openai_api_key):
16
  return openai_api_key
17
 
18
 
19
- def openai_create(prompt, openai_api_key):
20
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
21
  print("prompt: " + prompt)
22
- print("gpt_temperature: ", gpt_temperature)
23
- print("in openai_create openai_api_key: ", openai_api_key)
24
 
25
- #os.environ["OPENAI_API_KEY"] = openai_api_key
26
  openai.api_key = openai_api_key
27
  response = openai.Completion.create(
28
-
29
  model="text-davinci-003",
30
  prompt=prompt,
31
- temperature=gpt_temperature,
32
  max_tokens=2000,
33
  top_p=1,
34
  frequency_penalty=0,
35
  presence_penalty=0
36
  )
37
- #os.environ["OPENAI_API_KEY"] = ""
38
-
39
  return response.choices[0].text
40
 
41
 
42
- def desc2sheet(desc, openai_api_key):
43
  if not openai_api_key or openai_api_key == "":
44
  return "<pre>Please paste your OpenAI API key</pre>"
45
 
46
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
47
  print("desc: " + desc)
48
- print("gpt_temperature: ", gpt_temperature)
49
- print("in desc2sheet openai_api_key: ", openai_api_key)
50
 
51
- html = openai_create(initial_prompt + desc + '\n', openai_api_key)
52
  return html
53
 
54
- def update_temperature(temperature):
55
- global gpt_temperature
56
- gpt_temperature = temperature
 
 
 
57
 
58
  block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
59
 
60
  with block:
61
  with gr.Row():
62
- temperature = gr.Slider(label="GPT Temperature", value=gpt_temperature, minimum=0.0, maximum=1.0, step=0.1)
63
  title = gr.Markdown("""<h3><center>GPT-3.5 Table-inator</center></h3>""")
64
  openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...)",
65
  show_label=False, lines=1, type='password')
@@ -72,14 +66,17 @@ with block:
72
  submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
73
 
74
  openai_api_key_state = gr.State()
 
75
 
76
- submit.click(desc2sheet, inputs=[request, openai_api_key_state], outputs=[table])
77
- request.submit(desc2sheet, inputs=[request, openai_api_key_state], outputs=[table])
78
 
79
  openai_api_key_textbox.change(set_openai_api_key,
80
  inputs=[openai_api_key_textbox, openai_api_key_state],
81
  outputs=[openai_api_key_state])
82
 
83
- temperature.change(update_temperature, temperature)
 
 
84
 
85
  block.launch()
 
 
1
  import openai
2
  import gradio as gr
3
  import datetime
4
 
5
+ # gpt_temperature = 0.1
6
 
7
+ initial_prompt = "List the following in a HTML table with about 7 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: "
 
 
8
 
9
 
10
  def set_openai_api_key(api_key, openai_api_key):
 
13
  return openai_api_key
14
 
15
 
16
+ def openai_create(prompt, openai_api_key, temperature):
17
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
18
  print("prompt: " + prompt)
19
+ print("temperature: ", temperature)
 
20
 
 
21
  openai.api_key = openai_api_key
22
  response = openai.Completion.create(
 
23
  model="text-davinci-003",
24
  prompt=prompt,
25
+ temperature=temperature,
26
  max_tokens=2000,
27
  top_p=1,
28
  frequency_penalty=0,
29
  presence_penalty=0
30
  )
 
 
31
  return response.choices[0].text
32
 
33
 
34
+ def desc2sheet(desc, openai_api_key, temperature):
35
  if not openai_api_key or openai_api_key == "":
36
  return "<pre>Please paste your OpenAI API key</pre>"
37
 
38
  print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
39
  print("desc: " + desc)
40
+ print("temperature: ", temperature)
 
41
 
42
+ html = openai_create(initial_prompt + desc + '\n', openai_api_key, temperature)
43
  return html
44
 
45
+
46
+ def update_temperature(temp_slider, temp_state):
47
+ if temp_slider:
48
+ temp_state = temp_slider
49
+ return temp_state
50
+
51
 
52
  block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
53
 
54
  with block:
55
  with gr.Row():
56
+ temperature_slider = gr.Slider(label="GPT Temperature", value=0.0, minimum=0.0, maximum=1.0, step=0.1)
57
  title = gr.Markdown("""<h3><center>GPT-3.5 Table-inator</center></h3>""")
58
  openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key (sk-...)",
59
  show_label=False, lines=1, type='password')
 
66
  submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
67
 
68
  openai_api_key_state = gr.State()
69
+ temperature_state = gr.State(0.0)
70
 
71
+ submit.click(desc2sheet, inputs=[request, openai_api_key_state, temperature_state], outputs=[table])
72
+ request.submit(desc2sheet, inputs=[request, openai_api_key_state, temperature_state], outputs=[table])
73
 
74
  openai_api_key_textbox.change(set_openai_api_key,
75
  inputs=[openai_api_key_textbox, openai_api_key_state],
76
  outputs=[openai_api_key_state])
77
 
78
+ temperature_slider.change(update_temperature,
79
+ inputs=[temperature_slider, temperature_state],
80
+ outputs=[temperature_state])
81
 
82
  block.launch()