JLW commited on
Commit
6edcdbd
·
1 Parent(s): c561df3

Add temperature slider and OpenAI API key textbox

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