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

Add languages

Browse files
Files changed (1) hide show
  1. app.py +99 -74
app.py CHANGED
@@ -14,10 +14,11 @@ 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
 
@@ -36,7 +37,8 @@ def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
36
 
37
  def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formality,
38
  anticipation_level, joy_level, trust_level,
39
- fear_level, surprise_level, sadness_level, disgust_level, anger_level):
 
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
 
@@ -81,18 +83,23 @@ def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formalit
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
 
@@ -139,6 +146,7 @@ with block:
139
  sadness_level_state = gr.State(EMOTION_DEFAULT)
140
  disgust_level_state = gr.State(EMOTION_DEFAULT)
141
  anger_level_state = gr.State(EMOTION_DEFAULT)
 
142
 
143
  with gr.Row():
144
  temperature_slider = gr.Slider(label="GPT Temperature", value=TEMPERATURE_DEFAULT, minimum=0.0, maximum=1.0,
@@ -153,81 +161,97 @@ with block:
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])
162
-
163
- formality_radio = gr.Radio(label="Formality", choices=["Casual", "Polite", "Honorific"], value=FORMALITY_DEFAULT)
164
- formality_radio.change(update_formality,
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)
172
- anticipation_level_radio.change(update_foo,
173
- inputs=[anticipation_level_radio, anticipation_level_state],
174
- outputs=[anticipation_level_state])
175
-
176
- joy_level_radio = gr.Radio(label="Joy level",
177
- choices=[EMOTION_DEFAULT, "Serenity", "Joy", "Ecstasy"],
178
- value=EMOTION_DEFAULT)
179
- joy_level_radio.change(update_foo,
180
- inputs=[joy_level_radio, joy_level_state],
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])
196
-
197
- surprise_level_radio = gr.Radio(label="Surprise level",
198
- choices=[EMOTION_DEFAULT, "Distraction", "Surprise", "Amazement"],
199
- value=EMOTION_DEFAULT)
200
- surprise_level_radio.change(update_foo,
201
- inputs=[surprise_level_radio, surprise_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=[
227
  request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
228
  formality_state,
229
  anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
230
- surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state],
 
231
  outputs=[table])
232
 
233
  request.submit(desc2sheet,
@@ -235,7 +259,8 @@ with block:
235
  request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
236
  formality_state,
237
  anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
238
- surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state],
 
239
  outputs=[table])
240
 
241
  openai_api_key_textbox.change(set_openai_api_key,
 
14
  FORMALITY_DEFAULT = "Casual"
15
  TEMPERATURE_DEFAULT = 0.5
16
  EMOTION_DEFAULT = "N/A"
17
+ TRANSLATE_TO_DEFAULT = "Don't translate"
18
  PROMPT_TEMPLATE = PromptTemplate(
19
+ input_variables=["original_words", "num_words", "formality", "emotions", "translate_to"],
20
+ template="Express {num_words} in a {formality} manner, "
21
+ "{emotions} {translate_to} the following: \n{original_words}\n",
22
  )
23
 
24
 
 
37
 
38
  def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formality,
39
  anticipation_level, joy_level, trust_level,
40
+ fear_level, surprise_level, sadness_level, disgust_level, anger_level,
41
+ translate_to):
42
  if not openai_api_key or openai_api_key == "":
43
  return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
44
 
 
83
  # If there are any emotions, join the emotions list into a string that begins with "Emotions: ", with the word "and" before the last emotion
84
  if len(emotions) > 0:
85
  if len(emotions) == 1:
86
+ emotions_str = "with emotion of " + emotions[0] + ", "
87
  else:
88
+ emotions_str = "with emotions of " + ", ".join(emotions[:-1]) + " and " + emotions[-1] + ", "
89
+
90
+ translate_to_str = ""
91
+ if translate_to != TRANSLATE_TO_DEFAULT:
92
+ translate_to_str = "translated to " + translate_to + ", "
93
 
94
  formatted_prompt = PROMPT_TEMPLATE.format(
95
  original_words=desc,
96
  num_words=num_words_prompt,
97
  formality=formality,
98
  emotions=emotions_str,
99
+ translate_to=translate_to_str,
100
  )
101
  generated_text = llm_chain.run({'original_words': desc, 'num_words': num_words_prompt, 'formality': formality,
102
+ 'emotions': emotions_str, 'translate_to': translate_to_str})
103
 
104
  prompt_plus_generated = "#### GPT prompt:\n" + formatted_prompt + "#### Generated text:\n" + generated_text
105
 
 
146
  sadness_level_state = gr.State(EMOTION_DEFAULT)
147
  disgust_level_state = gr.State(EMOTION_DEFAULT)
148
  anger_level_state = gr.State(EMOTION_DEFAULT)
149
+ translate_to_state = gr.State(TRANSLATE_TO_DEFAULT)
150
 
151
  with gr.Row():
152
  temperature_slider = gr.Slider(label="GPT Temperature", value=TEMPERATURE_DEFAULT, minimum=0.0, maximum=1.0,
 
161
  value="The quick brown fox jumped over the lazy dog.",
162
  placeholder="Ex: The quick brown fox jumped over the lazy dog.")
163
 
164
+ submit = gr.Button(value="Express", variant="secondary").style(full_width=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
+ with gr.Row():
167
+ with gr.Column():
168
+ num_words_slider = gr.Slider(label="Max number of words to generate (0 for don't care)",
169
+ value=NUM_WORDS_DEFAULT, minimum=0, maximum=100, step=10)
170
+ num_words_slider.change(update_num_words,
171
+ inputs=[num_words_slider, num_words_state],
172
+ outputs=[num_words_state])
173
+
174
+ formality_radio = gr.Radio(label="Formality:", choices=["Casual", "Polite", "Honorific"],
175
+ value=FORMALITY_DEFAULT)
176
+ formality_radio.change(update_formality,
177
+ inputs=[formality_radio, formality_state],
178
+ outputs=[formality_state])
179
+
180
+ translate_to_radio = gr.Radio(label="Translate to:", choices=[
181
+ "Arabic", "British English", "Chinese (Simplified)", "Chinese (Traditional)", "Czech", "Danish", "Dutch", "English",
182
+ "Finnish", "French", "Gen Z slang", "German", "Greek", "Hebrew", "Hindi", "Hungarian", "Indonesian",
183
+ "Italian", "Japanese", "Klingon", "Korean", "Norwegian", "Old English", "Polish", "Portuguese",
184
+ "Romanian", "Russian", "Spanish", "Swedish", "Thai", "Turkish", "Vietnamese", "Yoda",
185
+ TRANSLATE_TO_DEFAULT], value=TRANSLATE_TO_DEFAULT)
186
+
187
+ translate_to_radio.change(update_foo,
188
+ inputs=[translate_to_radio, translate_to_state],
189
+ outputs=[translate_to_state])
190
+
191
+ with gr.Column():
192
+ anticipation_level_radio = gr.Radio(label="Anticipation level:",
193
+ choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
194
+ value=EMOTION_DEFAULT)
195
+ anticipation_level_radio.change(update_foo,
196
+ inputs=[anticipation_level_radio, anticipation_level_state],
197
+ outputs=[anticipation_level_state])
198
+
199
+ joy_level_radio = gr.Radio(label="Joy level:",
200
+ choices=[EMOTION_DEFAULT, "Serenity", "Joy", "Ecstasy"],
201
  value=EMOTION_DEFAULT)
202
+ joy_level_radio.change(update_foo,
203
+ inputs=[joy_level_radio, joy_level_state],
204
+ outputs=[joy_level_state])
205
+
206
+ trust_level_radio = gr.Radio(label="Trust level:",
207
+ choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
208
+ value=EMOTION_DEFAULT)
209
+ trust_level_radio.change(update_foo,
210
+ inputs=[trust_level_radio, trust_level_state],
211
+ outputs=[trust_level_state])
212
+
213
+ fear_level_radio = gr.Radio(label="Fear level:",
214
+ choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
215
+ value=EMOTION_DEFAULT)
216
+ fear_level_radio.change(update_foo,
217
+ inputs=[fear_level_radio, fear_level_state],
218
+ outputs=[fear_level_state])
219
 
220
+ surprise_level_radio = gr.Radio(label="Surprise level:",
221
+ choices=[EMOTION_DEFAULT, "Distraction", "Surprise", "Amazement"],
222
+ value=EMOTION_DEFAULT)
223
+ surprise_level_radio.change(update_foo,
224
+ inputs=[surprise_level_radio, surprise_level_state],
225
+ outputs=[surprise_level_state])
226
+
227
+ sadness_level_radio = gr.Radio(label="Sadness level:",
228
+ choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
229
+ value=EMOTION_DEFAULT)
230
+ sadness_level_radio.change(update_foo,
231
+ inputs=[sadness_level_radio, sadness_level_state],
232
+ outputs=[sadness_level_state])
233
+
234
+ disgust_level_radio = gr.Radio(label="Disgust level:",
235
+ choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
236
+ value=EMOTION_DEFAULT)
237
+ disgust_level_radio.change(update_foo,
238
+ inputs=[disgust_level_radio, disgust_level_state],
239
+ outputs=[disgust_level_state])
240
+
241
+ anger_level_radio = gr.Radio(label="Anger level:",
242
+ choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
243
+ value=EMOTION_DEFAULT)
244
+ anger_level_radio.change(update_foo,
245
+ inputs=[anger_level_radio, anger_level_state],
246
+ outputs=[anger_level_state])
247
 
248
  submit.click(desc2sheet,
249
  inputs=[
250
  request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
251
  formality_state,
252
  anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
253
+ surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
254
+ translate_to_state],
255
  outputs=[table])
256
 
257
  request.submit(desc2sheet,
 
259
  request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
260
  formality_state,
261
  anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
262
+ surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
263
+ translate_to_state],
264
  outputs=[table])
265
 
266
  openai_api_key_textbox.change(set_openai_api_key,