JLW commited on
Commit
cd33a33
1 Parent(s): 9d383c5

Implement lots of state

Browse files
Files changed (2) hide show
  1. app.py +54 -12
  2. requirements.txt +1 -0
app.py CHANGED
@@ -1,14 +1,31 @@
 
1
  import openai
2
  import gradio as gr
 
 
 
3
  import datetime
4
 
5
- initial_prompt = "List the following in a HTML table with an appropriate number of rows in an appropriate order, giving the table an appropriate caption. 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. The table contains around 5 columns with appropriate attributes: "
 
 
 
 
 
6
 
 
7
 
8
- def set_openai_api_key(api_key, openai_api_key):
 
9
  if api_key:
10
  openai_api_key = api_key
11
- return openai_api_key
 
 
 
 
 
 
12
 
13
 
14
  def openai_create(prompt, openai_api_key, temperature):
@@ -29,11 +46,11 @@ def openai_create(prompt, openai_api_key, temperature):
29
  return response.choices[0].text
30
 
31
 
32
- def desc2sheet(desc, openai_api_key, temperature):
33
  if not openai_api_key or openai_api_key == "":
34
  return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
35
 
36
- html = openai_create(initial_prompt + desc + '\n', openai_api_key, temperature)
37
  return html
38
 
39
 
@@ -42,9 +59,21 @@ def update_temperature(temp_slider, temp_state):
42
  temp_state = temp_slider
43
  return temp_state
44
 
 
 
 
 
 
 
 
45
  block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
46
 
47
  with block:
 
 
 
 
 
48
  with gr.Row():
49
  temperature_slider = gr.Slider(label="GPT Temperature", value=0.0, minimum=0.0, maximum=1.0, step=0.1)
50
  title = gr.Markdown("""<h3><center>GPT-3.5 Table-inator</center></h3>""")
@@ -53,23 +82,36 @@ with block:
53
 
54
  table = gr.Markdown("")
55
  with gr.Row():
56
- request = gr.Textbox(label="GPT prompt: " + initial_prompt,
 
57
  placeholder="Ex: Computer languages")
58
 
59
  submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
60
 
61
- openai_api_key_state = gr.State()
62
- temperature_state = gr.State(0.0)
63
 
64
- submit.click(desc2sheet, inputs=[request, openai_api_key_state, temperature_state], outputs=[table])
65
- request.submit(desc2sheet, inputs=[request, openai_api_key_state, temperature_state], outputs=[table])
 
 
 
 
 
 
 
66
 
67
  openai_api_key_textbox.change(set_openai_api_key,
68
- inputs=[openai_api_key_textbox, openai_api_key_state],
69
- outputs=[openai_api_key_state])
 
70
 
71
  temperature_slider.change(update_temperature,
72
  inputs=[temperature_slider, temperature_state],
73
  outputs=[temperature_state])
74
 
 
 
 
 
 
75
  block.launch()
 
1
+ import os
2
  import openai
3
  import gradio as gr
4
+ from langchain import OpenAI
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain.chains import LLMChain
7
  import datetime
8
 
9
+ NUM_WORDS_DEFAULT = 20
10
+ PROMPT_TEMPLATE = PromptTemplate(
11
+ input_variables=["original_words", "num_words"],
12
+ template="Restate, into an HTML div, in about {num_words} words, the following, with emotions of admiration, "
13
+ "in a formal manner, in a passive voice: \n{original_words}\n",
14
+ )
15
 
16
+ # initial_prompt = "Restate, into an HTML div, in about 60 words, the following, with emotions of admiration, in a formal manner, in a passive voice: "
17
 
18
+
19
+ def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
20
  if api_key:
21
  openai_api_key = api_key
22
+
23
+ os.environ["OPENAI_API_KEY"] = api_key
24
+ llm = OpenAI(model_name='text-davinci-003', temperature=temperature, max_tokens=512)
25
+ os.environ["OPENAI_API_KEY"] = ""
26
+
27
+ llm_chain = LLMChain(llm=llm, prompt=PROMPT_TEMPLATE, verbose=True)
28
+ return openai_api_key, llm_chain
29
 
30
 
31
  def openai_create(prompt, openai_api_key, temperature):
 
46
  return response.choices[0].text
47
 
48
 
49
+ def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words):
50
  if not openai_api_key or openai_api_key == "":
51
  return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
52
 
53
+ html = llm_chain.run({'original_words': desc, 'num_words': num_words})
54
  return html
55
 
56
 
 
59
  temp_state = temp_slider
60
  return temp_state
61
 
62
+
63
+ def update_num_words(slider, state):
64
+ if slider:
65
+ state = slider
66
+ return state
67
+
68
+
69
  block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
70
 
71
  with block:
72
+ openai_api_key_state = gr.State()
73
+ temperature_state = gr.State(0.0)
74
+ llm_chain_state = gr.State()
75
+ num_words_state = gr.State(NUM_WORDS_DEFAULT)
76
+
77
  with gr.Row():
78
  temperature_slider = gr.Slider(label="GPT Temperature", value=0.0, minimum=0.0, maximum=1.0, step=0.1)
79
  title = gr.Markdown("""<h3><center>GPT-3.5 Table-inator</center></h3>""")
 
82
 
83
  table = gr.Markdown("")
84
  with gr.Row():
85
+ request = gr.Textbox(label="GPT prompt: " + PROMPT_TEMPLATE.template_format,
86
+ value="Paris in the spring",
87
  placeholder="Ex: Computer languages")
88
 
89
  submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
90
 
91
+ num_words_slider = gr.Slider(label="Number of words", value=NUM_WORDS_DEFAULT, minimum=10, maximum=100, step=10)
 
92
 
93
+ submit.click(desc2sheet,
94
+ inputs=[
95
+ request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state],
96
+ outputs=[table])
97
+
98
+ request.submit(desc2sheet,
99
+ inputs=[
100
+ request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state],
101
+ outputs=[table])
102
 
103
  openai_api_key_textbox.change(set_openai_api_key,
104
+ inputs=[openai_api_key_textbox, openai_api_key_state, temperature_state,
105
+ llm_chain_state],
106
+ outputs=[openai_api_key_state, llm_chain_state])
107
 
108
  temperature_slider.change(update_temperature,
109
  inputs=[temperature_slider, temperature_state],
110
  outputs=[temperature_state])
111
 
112
+ num_words_slider.change(update_num_words,
113
+ inputs=[num_words_slider, num_words_state],
114
+ outputs=[num_words_state])
115
+
116
+
117
  block.launch()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  openai
2
  gradio
 
 
1
  openai
2
  gradio
3
+ langchain