JLW commited on
Commit
e789a4c
·
1 Parent(s): a779dfe

Display formatted prompt

Browse files
Files changed (1) hide show
  1. app.py +82 -52
app.py CHANGED
@@ -6,20 +6,21 @@ from langchain.prompts import PromptTemplate
6
  from langchain.chains import LLMChain
7
  import datetime
8
 
9
- NUM_WORDS_DEFAULT = 20
 
 
 
 
10
  FORMALITY_DEFAULT = "Casual"
11
  TEMPERATURE_DEFAULT = 0.5
12
  EMOTION_DEFAULT = "N/A"
13
  PROMPT_TEMPLATE = PromptTemplate(
14
  input_variables=["original_words", "num_words", "formality", "emotions"],
15
- template="Restate, into an HTML div, using approximately {num_words} words, in a {formality} manner, "
16
- "expressing {emotions} the following: \n{original_words}\n",
17
  )
18
 
19
 
20
- # 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: "
21
-
22
-
23
  def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
24
  if api_key:
25
  print("temperature: ", temperature)
@@ -39,33 +40,63 @@ def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formalit
39
  if not openai_api_key or openai_api_key == "":
40
  return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  llm_chain.llm.temperature = temperature
43
  print("llm_chain.llm.temperature: ", llm_chain.llm.temperature)
44
 
45
- emotions = ""
46
- if anticipation_level != "N/A":
47
- emotions = emotions + anticipation_level + ", "
48
- if joy_level != "N/A":
49
- emotions = emotions + joy_level + ", "
50
- if trust_level != "N/A":
51
- emotions = emotions + trust_level + ", "
52
- if fear_level != "N/A":
53
- emotions = emotions + fear_level + ", "
54
- if surprise_level != "N/A":
55
- emotions = emotions + surprise_level + ", "
56
- if sadness_level != "N/A":
57
- emotions = emotions + sadness_level + ", "
58
- if disgust_level != "N/A":
59
- emotions = emotions + disgust_level + ", "
60
- if anger_level != "N/A":
61
- emotions = emotions + anger_level + ", "
62
-
63
- if emotions == "":
64
- emotions = "no emotion, "
65
-
66
- html = llm_chain.run({'original_words': desc, 'num_words': num_words, 'formality': formality.lower(),
67
- 'emotions': emotions.lower()})
68
- return html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
 
71
  def update_temperature(temp_slider, temp_state):
@@ -118,13 +149,13 @@ with block:
118
 
119
  table = gr.Markdown("")
120
  with gr.Row():
121
- request = gr.Textbox(label="GPT prompt: " + PROMPT_TEMPLATE.template_format,
122
  value="The quick brown fox jumped over the lazy dog.",
123
  placeholder="Ex: The quick brown fox jumped over the lazy dog.")
124
 
125
  submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
126
 
127
- num_words_slider = gr.Slider(label="Number of words", value=NUM_WORDS_DEFAULT, minimum=10, maximum=100, step=10)
128
  num_words_slider.change(update_num_words,
129
  inputs=[num_words_slider, num_words_state],
130
  outputs=[num_words_state])
@@ -134,7 +165,7 @@ with block:
134
  inputs=[formality_radio, formality_state],
135
  outputs=[formality_state])
136
 
137
- with gr.Accordion("Emotions", open=False):
138
  anticipation_level_radio = gr.Radio(label="Anticipation level",
139
  choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
140
  value=EMOTION_DEFAULT)
@@ -150,15 +181,15 @@ with block:
150
  outputs=[joy_level_state])
151
 
152
  trust_level_radio = gr.Radio(label="Trust level",
153
- choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
154
- value=EMOTION_DEFAULT)
155
  trust_level_radio.change(update_foo,
156
- inputs=[trust_level_radio, trust_level_state],
157
- outputs=[trust_level_state])
158
 
159
  fear_level_radio = gr.Radio(label="Fear level",
160
- choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
161
- value=EMOTION_DEFAULT)
162
  fear_level_radio.change(update_foo,
163
  inputs=[fear_level_radio, fear_level_state],
164
  outputs=[fear_level_state])
@@ -171,26 +202,25 @@ with block:
171
  outputs=[surprise_level_state])
172
 
173
  sadness_level_radio = gr.Radio(label="Sadness level",
174
- choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
175
- value=EMOTION_DEFAULT)
176
  sadness_level_radio.change(update_foo,
177
- inputs=[sadness_level_radio, sadness_level_state],
178
- outputs=[sadness_level_state])
179
 
180
  disgust_level_radio = gr.Radio(label="Disgust level",
181
- choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
182
- value=EMOTION_DEFAULT)
183
  disgust_level_radio.change(update_foo,
184
- inputs=[disgust_level_radio, disgust_level_state],
185
- outputs=[disgust_level_state])
186
 
187
  anger_level_radio = gr.Radio(label="Anger level",
188
- choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
189
- value=EMOTION_DEFAULT)
190
  anger_level_radio.change(update_foo,
191
- inputs=[anger_level_radio, anger_level_state],
192
- outputs=[anger_level_state])
193
-
194
 
195
  submit.click(desc2sheet,
196
  inputs=[
 
6
  from langchain.chains import LLMChain
7
  import datetime
8
 
9
+ # Console to variable
10
+ from io import StringIO
11
+ import sys
12
+
13
+ NUM_WORDS_DEFAULT = 0
14
  FORMALITY_DEFAULT = "Casual"
15
  TEMPERATURE_DEFAULT = 0.5
16
  EMOTION_DEFAULT = "N/A"
17
  PROMPT_TEMPLATE = PromptTemplate(
18
  input_variables=["original_words", "num_words", "formality", "emotions"],
19
+ template="Restate {num_words} in a {formality} manner, "
20
+ "{emotions} the following: \n{original_words}\n",
21
  )
22
 
23
 
 
 
 
24
  def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
25
  if api_key:
26
  print("temperature: ", temperature)
 
40
  if not openai_api_key or openai_api_key == "":
41
  return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
42
 
43
+ num_words_prompt = ""
44
+ if num_words and int(num_words) != 0:
45
+ num_words_prompt = "using up to " + str(num_words) + " words, "
46
+
47
+ # Change some arguments to the lower case
48
+ formality = formality.lower()
49
+ anticipation_level = anticipation_level.lower()
50
+ joy_level = joy_level.lower()
51
+ trust_level = trust_level.lower()
52
+ fear_level = fear_level.lower()
53
+ surprise_level = surprise_level.lower()
54
+ sadness_level = sadness_level.lower()
55
+ disgust_level = disgust_level.lower()
56
+ anger_level = anger_level.lower()
57
+
58
  llm_chain.llm.temperature = temperature
59
  print("llm_chain.llm.temperature: ", llm_chain.llm.temperature)
60
 
61
+ # put all emotions into a list
62
+ emotions = []
63
+ if anticipation_level != "n/a":
64
+ emotions.append(anticipation_level)
65
+ if joy_level != "n/a":
66
+ emotions.append(joy_level)
67
+ if trust_level != "n/a":
68
+ emotions.append(trust_level)
69
+ if fear_level != "n/a":
70
+ emotions.append(fear_level)
71
+ if surprise_level != "n/a":
72
+ emotions.append(surprise_level)
73
+ if sadness_level != "n/a":
74
+ emotions.append(sadness_level)
75
+ if disgust_level != "n/a":
76
+ emotions.append(disgust_level)
77
+ if anger_level != "n/a":
78
+ emotions.append(anger_level)
79
+
80
+ emotions_str = ""
81
+ # If there are any emotions, join the emotions list into a string that begins with "Emotions: ", with the word "and" before the last emotion
82
+ if len(emotions) > 0:
83
+ if len(emotions) == 1:
84
+ emotions_str = "with emotion of " + emotions[0]
85
+ else:
86
+ emotions_str = "with emotions of " + ", ".join(emotions[:-1]) + " and " + emotions[-1]
87
+
88
+ formatted_prompt = PROMPT_TEMPLATE.format(
89
+ original_words=desc,
90
+ num_words=num_words_prompt,
91
+ formality=formality,
92
+ emotions=emotions_str,
93
+ )
94
+ generated_text = llm_chain.run({'original_words': desc, 'num_words': num_words_prompt, 'formality': formality,
95
+ 'emotions': emotions_str})
96
+
97
+ prompt_plus_generated = "#### GPT prompt:\n" + formatted_prompt + "#### Generated text:\n" + generated_text
98
+
99
+ return prompt_plus_generated
100
 
101
 
102
  def update_temperature(temp_slider, temp_state):
 
149
 
150
  table = gr.Markdown("")
151
  with gr.Row():
152
+ request = gr.Textbox(label="Words to express: ",
153
  value="The quick brown fox jumped over the lazy dog.",
154
  placeholder="Ex: The quick brown fox jumped over the lazy dog.")
155
 
156
  submit = gr.Button(value="Create", variant="secondary").style(full_width=False)
157
 
158
+ num_words_slider = gr.Slider(label="Number of words to generate (0 for don't care)", value=NUM_WORDS_DEFAULT, minimum=0, maximum=100, step=10)
159
  num_words_slider.change(update_num_words,
160
  inputs=[num_words_slider, num_words_state],
161
  outputs=[num_words_state])
 
165
  inputs=[formality_radio, formality_state],
166
  outputs=[formality_state])
167
 
168
+ with gr.Accordion("Emotions", open=True):
169
  anticipation_level_radio = gr.Radio(label="Anticipation level",
170
  choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
171
  value=EMOTION_DEFAULT)
 
181
  outputs=[joy_level_state])
182
 
183
  trust_level_radio = gr.Radio(label="Trust level",
184
+ choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
185
+ value=EMOTION_DEFAULT)
186
  trust_level_radio.change(update_foo,
187
+ inputs=[trust_level_radio, trust_level_state],
188
+ outputs=[trust_level_state])
189
 
190
  fear_level_radio = gr.Radio(label="Fear level",
191
+ choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
192
+ value=EMOTION_DEFAULT)
193
  fear_level_radio.change(update_foo,
194
  inputs=[fear_level_radio, fear_level_state],
195
  outputs=[fear_level_state])
 
202
  outputs=[surprise_level_state])
203
 
204
  sadness_level_radio = gr.Radio(label="Sadness level",
205
+ choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
206
+ value=EMOTION_DEFAULT)
207
  sadness_level_radio.change(update_foo,
208
+ inputs=[sadness_level_radio, sadness_level_state],
209
+ outputs=[sadness_level_state])
210
 
211
  disgust_level_radio = gr.Radio(label="Disgust level",
212
+ choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
213
+ value=EMOTION_DEFAULT)
214
  disgust_level_radio.change(update_foo,
215
+ inputs=[disgust_level_radio, disgust_level_state],
216
+ outputs=[disgust_level_state])
217
 
218
  anger_level_radio = gr.Radio(label="Anger level",
219
+ choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
220
+ value=EMOTION_DEFAULT)
221
  anger_level_radio.change(update_foo,
222
+ inputs=[anger_level_radio, anger_level_state],
223
+ outputs=[anger_level_state])
 
224
 
225
  submit.click(desc2sheet,
226
  inputs=[