Spaces:
Build error
Build error
Tweaks
Browse files
app.py
CHANGED
@@ -6,19 +6,16 @@ from langchain.prompts import PromptTemplate
|
|
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 |
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}
|
21 |
-
"{emotions}
|
22 |
)
|
23 |
|
24 |
|
@@ -35,10 +32,10 @@ def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
|
|
35 |
return openai_api_key, llm_chain
|
36 |
|
37 |
|
38 |
-
def
|
39 |
-
|
40 |
-
|
41 |
-
|
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 |
|
@@ -46,7 +43,7 @@ def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formalit
|
|
46 |
if num_words and int(num_words) != 0:
|
47 |
num_words_prompt = "using up to " + str(num_words) + " words, "
|
48 |
|
49 |
-
# Change some arguments to
|
50 |
formality = formality.lower()
|
51 |
anticipation_level = anticipation_level.lower()
|
52 |
joy_level = joy_level.lower()
|
@@ -58,7 +55,6 @@ def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formalit
|
|
58 |
anger_level = anger_level.lower()
|
59 |
|
60 |
llm_chain.llm.temperature = temperature
|
61 |
-
print("llm_chain.llm.temperature: ", llm_chain.llm.temperature)
|
62 |
|
63 |
# put all emotions into a list
|
64 |
emotions = []
|
@@ -80,7 +76,6 @@ def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formalit
|
|
80 |
emotions.append(anger_level)
|
81 |
|
82 |
emotions_str = ""
|
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] + ", "
|
@@ -91,37 +86,42 @@ def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words, formalit
|
|
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 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
def update_temperature(temp_slider, temp_state):
|
110 |
-
if temp_slider:
|
111 |
-
temp_state = temp_slider
|
112 |
-
return temp_state
|
113 |
|
|
|
|
|
114 |
|
115 |
-
|
116 |
-
if slider:
|
117 |
-
state = slider
|
118 |
-
return state
|
119 |
|
|
|
|
|
|
|
120 |
|
121 |
-
|
122 |
-
if radio:
|
123 |
-
state = radio
|
124 |
-
return state
|
125 |
|
126 |
|
127 |
def update_foo(widget, state):
|
@@ -147,6 +147,7 @@ with block:
|
|
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,
|
@@ -155,7 +156,7 @@ with block:
|
|
155 |
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key",
|
156 |
show_label=False, lines=1, type='password')
|
157 |
|
158 |
-
|
159 |
with gr.Row():
|
160 |
request = gr.Textbox(label="Words to express: ",
|
161 |
value="The quick brown fox jumped over the lazy dog.",
|
@@ -167,13 +168,13 @@ with block:
|
|
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(
|
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(
|
177 |
inputs=[formality_radio, formality_state],
|
178 |
outputs=[formality_state])
|
179 |
|
@@ -188,62 +189,71 @@ with block:
|
|
188 |
inputs=[translate_to_radio, translate_to_state],
|
189 |
outputs=[translate_to_state])
|
190 |
|
191 |
-
|
192 |
-
|
193 |
-
|
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 |
-
|
228 |
-
|
229 |
-
|
230 |
-
sadness_level_radio.change(update_foo,
|
231 |
-
inputs=[sadness_level_radio, sadness_level_state],
|
232 |
-
outputs=[sadness_level_state])
|
233 |
|
234 |
-
|
235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
236 |
value=EMOTION_DEFAULT)
|
237 |
-
|
238 |
-
inputs=[
|
239 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
247 |
|
248 |
gr.HTML("""
|
249 |
<center>This app by <a target='_blank' href='https://twitter.com/JavaFXpert'>@JavaFXpert</a> leverages GPT-3.5 for text completion to facilitate expressing yourself.
|
@@ -251,30 +261,30 @@ with block:
|
|
251 |
|
252 |
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
|
253 |
|
254 |
-
submit.click(
|
255 |
inputs=[
|
256 |
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
|
257 |
formality_state,
|
258 |
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
|
259 |
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
|
260 |
-
translate_to_state],
|
261 |
-
outputs=[
|
262 |
|
263 |
-
request.submit(
|
264 |
inputs=[
|
265 |
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
|
266 |
formality_state,
|
267 |
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
|
268 |
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
|
269 |
-
translate_to_state],
|
270 |
-
outputs=[
|
271 |
|
272 |
openai_api_key_textbox.change(set_openai_api_key,
|
273 |
inputs=[openai_api_key_textbox, openai_api_key_state, temperature_state,
|
274 |
llm_chain_state],
|
275 |
outputs=[openai_api_key_state, llm_chain_state])
|
276 |
|
277 |
-
temperature_slider.change(
|
278 |
inputs=[temperature_slider, temperature_state],
|
279 |
outputs=[temperature_state])
|
280 |
|
|
|
6 |
from langchain.chains import LLMChain
|
7 |
import datetime
|
8 |
|
|
|
|
|
|
|
|
|
9 |
NUM_WORDS_DEFAULT = 0
|
10 |
FORMALITY_DEFAULT = "Casual"
|
11 |
TEMPERATURE_DEFAULT = 0.5
|
12 |
EMOTION_DEFAULT = "N/A"
|
13 |
TRANSLATE_TO_DEFAULT = "Don't translate"
|
14 |
+
LITERARY_STYLE_DEFAULT = "Prose"
|
15 |
PROMPT_TEMPLATE = PromptTemplate(
|
16 |
+
input_variables=["original_words", "num_words", "formality", "emotions", "translate_to", "literary_style"],
|
17 |
+
template="Express {num_words}in a {formality} manner, "
|
18 |
+
"{emotions}{translate_to}{literary_style}the following: \n{original_words}\n",
|
19 |
)
|
20 |
|
21 |
|
|
|
32 |
return openai_api_key, llm_chain
|
33 |
|
34 |
|
35 |
+
def transform_text(desc, openai_api_key, temperature, llm_chain, num_words, formality,
|
36 |
+
anticipation_level, joy_level, trust_level,
|
37 |
+
fear_level, surprise_level, sadness_level, disgust_level, anger_level,
|
38 |
+
translate_to, literary_style):
|
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 |
|
|
|
43 |
if num_words and int(num_words) != 0:
|
44 |
num_words_prompt = "using up to " + str(num_words) + " words, "
|
45 |
|
46 |
+
# Change some arguments to lower case
|
47 |
formality = formality.lower()
|
48 |
anticipation_level = anticipation_level.lower()
|
49 |
joy_level = joy_level.lower()
|
|
|
55 |
anger_level = anger_level.lower()
|
56 |
|
57 |
llm_chain.llm.temperature = temperature
|
|
|
58 |
|
59 |
# put all emotions into a list
|
60 |
emotions = []
|
|
|
76 |
emotions.append(anger_level)
|
77 |
|
78 |
emotions_str = ""
|
|
|
79 |
if len(emotions) > 0:
|
80 |
if len(emotions) == 1:
|
81 |
emotions_str = "with emotion of " + emotions[0] + ", "
|
|
|
86 |
if translate_to != TRANSLATE_TO_DEFAULT:
|
87 |
translate_to_str = "translated to " + translate_to + ", "
|
88 |
|
89 |
+
literary_style_str = ""
|
90 |
+
if literary_style != LITERARY_STYLE_DEFAULT:
|
91 |
+
if literary_style == "Poetry":
|
92 |
+
literary_style_str = "as a poem, "
|
93 |
+
elif literary_style == "Haiku":
|
94 |
+
literary_style_str = "as a haiku, "
|
95 |
+
elif literary_style == "Limerick":
|
96 |
+
literary_style_str = "as a limerick, "
|
97 |
+
elif literary_style == "Joke":
|
98 |
+
literary_style_str = "as a very funny joke with a setup and punchline, "
|
99 |
+
elif literary_style == "Knock-knock":
|
100 |
+
literary_style_str = "as a very funny knock-knock joke, "
|
101 |
+
|
102 |
formatted_prompt = PROMPT_TEMPLATE.format(
|
103 |
original_words=desc,
|
104 |
num_words=num_words_prompt,
|
105 |
formality=formality,
|
106 |
emotions=emotions_str,
|
107 |
translate_to=translate_to_str,
|
108 |
+
literary_style=literary_style_str
|
109 |
)
|
|
|
|
|
110 |
|
111 |
+
generated_text = llm_chain.run({'original_words': desc, 'num_words': num_words_prompt, 'formality': formality,
|
112 |
+
'emotions': emotions_str, 'translate_to': translate_to_str,
|
113 |
+
'literary_style': literary_style_str}).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
# replace all newlines with <br> in generated_text
|
116 |
+
generated_text = generated_text.replace("\n", "<br>")
|
117 |
|
118 |
+
prompt_plus_generated = "<b>GPT prompt:</b> " + formatted_prompt + "<br/><br/><code>" + generated_text + "</code>"
|
|
|
|
|
|
|
119 |
|
120 |
+
print("\n==== date/time: " + str(datetime.datetime.now() - datetime.timedelta(hours=5)) + " ====")
|
121 |
+
print("temperature: ", temperature)
|
122 |
+
print("prompt_plus_generated: " + prompt_plus_generated)
|
123 |
|
124 |
+
return prompt_plus_generated
|
|
|
|
|
|
|
125 |
|
126 |
|
127 |
def update_foo(widget, state):
|
|
|
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 |
+
literary_style_state = gr.State(LITERARY_STYLE_DEFAULT)
|
151 |
|
152 |
with gr.Row():
|
153 |
temperature_slider = gr.Slider(label="GPT Temperature", value=TEMPERATURE_DEFAULT, minimum=0.0, maximum=1.0,
|
|
|
156 |
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key",
|
157 |
show_label=False, lines=1, type='password')
|
158 |
|
159 |
+
output = gr.Markdown("")
|
160 |
with gr.Row():
|
161 |
request = gr.Textbox(label="Words to express: ",
|
162 |
value="The quick brown fox jumped over the lazy dog.",
|
|
|
168 |
with gr.Column():
|
169 |
num_words_slider = gr.Slider(label="Max number of words to generate (0 for don't care)",
|
170 |
value=NUM_WORDS_DEFAULT, minimum=0, maximum=100, step=10)
|
171 |
+
num_words_slider.change(update_foo,
|
172 |
inputs=[num_words_slider, num_words_state],
|
173 |
outputs=[num_words_state])
|
174 |
|
175 |
formality_radio = gr.Radio(label="Formality:", choices=["Casual", "Polite", "Honorific"],
|
176 |
value=FORMALITY_DEFAULT)
|
177 |
+
formality_radio.change(update_foo,
|
178 |
inputs=[formality_radio, formality_state],
|
179 |
outputs=[formality_state])
|
180 |
|
|
|
189 |
inputs=[translate_to_radio, translate_to_state],
|
190 |
outputs=[translate_to_state])
|
191 |
|
192 |
+
literary_style_radio = gr.Radio(label="Literary style:", choices=[
|
193 |
+
LITERARY_STYLE_DEFAULT, "Poetry", "Haiku", "Limerick", "Joke", "Knock-knock"],
|
194 |
+
value=LITERARY_STYLE_DEFAULT)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
|
196 |
+
literary_style_radio.change(update_foo,
|
197 |
+
inputs=[literary_style_radio, literary_style_state],
|
198 |
+
outputs=[literary_style_state])
|
|
|
|
|
|
|
199 |
|
200 |
+
with gr.Column():
|
201 |
+
with gr.Accordion("Emotions", open=True):
|
202 |
+
anticipation_level_radio = gr.Radio(label="Anticipation level:",
|
203 |
+
choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
|
204 |
+
value=EMOTION_DEFAULT)
|
205 |
+
anticipation_level_radio.change(update_foo,
|
206 |
+
inputs=[anticipation_level_radio, anticipation_level_state],
|
207 |
+
outputs=[anticipation_level_state])
|
208 |
+
|
209 |
+
joy_level_radio = gr.Radio(label="Joy level:",
|
210 |
+
choices=[EMOTION_DEFAULT, "Serenity", "Joy", "Ecstasy"],
|
211 |
value=EMOTION_DEFAULT)
|
212 |
+
joy_level_radio.change(update_foo,
|
213 |
+
inputs=[joy_level_radio, joy_level_state],
|
214 |
+
outputs=[joy_level_state])
|
215 |
+
|
216 |
+
trust_level_radio = gr.Radio(label="Trust level:",
|
217 |
+
choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
|
218 |
+
value=EMOTION_DEFAULT)
|
219 |
+
trust_level_radio.change(update_foo,
|
220 |
+
inputs=[trust_level_radio, trust_level_state],
|
221 |
+
outputs=[trust_level_state])
|
222 |
+
|
223 |
+
fear_level_radio = gr.Radio(label="Fear level:",
|
224 |
+
choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
|
225 |
+
value=EMOTION_DEFAULT)
|
226 |
+
fear_level_radio.change(update_foo,
|
227 |
+
inputs=[fear_level_radio, fear_level_state],
|
228 |
+
outputs=[fear_level_state])
|
229 |
|
230 |
+
surprise_level_radio = gr.Radio(label="Surprise level:",
|
231 |
+
choices=[EMOTION_DEFAULT, "Distraction", "Surprise", "Amazement"],
|
232 |
+
value=EMOTION_DEFAULT)
|
233 |
+
surprise_level_radio.change(update_foo,
|
234 |
+
inputs=[surprise_level_radio, surprise_level_state],
|
235 |
+
outputs=[surprise_level_state])
|
236 |
+
|
237 |
+
sadness_level_radio = gr.Radio(label="Sadness level:",
|
238 |
+
choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
|
239 |
+
value=EMOTION_DEFAULT)
|
240 |
+
sadness_level_radio.change(update_foo,
|
241 |
+
inputs=[sadness_level_radio, sadness_level_state],
|
242 |
+
outputs=[sadness_level_state])
|
243 |
+
|
244 |
+
disgust_level_radio = gr.Radio(label="Disgust level:",
|
245 |
+
choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
|
246 |
+
value=EMOTION_DEFAULT)
|
247 |
+
disgust_level_radio.change(update_foo,
|
248 |
+
inputs=[disgust_level_radio, disgust_level_state],
|
249 |
+
outputs=[disgust_level_state])
|
250 |
+
|
251 |
+
anger_level_radio = gr.Radio(label="Anger level:",
|
252 |
+
choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
|
253 |
+
value=EMOTION_DEFAULT)
|
254 |
+
anger_level_radio.change(update_foo,
|
255 |
+
inputs=[anger_level_radio, anger_level_state],
|
256 |
+
outputs=[anger_level_state])
|
257 |
|
258 |
gr.HTML("""
|
259 |
<center>This app by <a target='_blank' href='https://twitter.com/JavaFXpert'>@JavaFXpert</a> leverages GPT-3.5 for text completion to facilitate expressing yourself.
|
|
|
261 |
|
262 |
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
|
263 |
|
264 |
+
submit.click(transform_text,
|
265 |
inputs=[
|
266 |
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
|
267 |
formality_state,
|
268 |
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
|
269 |
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
|
270 |
+
translate_to_state, literary_style_state],
|
271 |
+
outputs=[output])
|
272 |
|
273 |
+
request.submit(transform_text,
|
274 |
inputs=[
|
275 |
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
|
276 |
formality_state,
|
277 |
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
|
278 |
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
|
279 |
+
translate_to_state, literary_style_state],
|
280 |
+
outputs=[output])
|
281 |
|
282 |
openai_api_key_textbox.change(set_openai_api_key,
|
283 |
inputs=[openai_api_key_textbox, openai_api_key_state, temperature_state,
|
284 |
llm_chain_state],
|
285 |
outputs=[openai_api_key_state, llm_chain_state])
|
286 |
|
287 |
+
temperature_slider.change(update_foo,
|
288 |
inputs=[temperature_slider, temperature_state],
|
289 |
outputs=[temperature_state])
|
290 |
|