Spaces:
Sleeping
Sleeping
File size: 17,965 Bytes
36be6c9 8fdeac9 36be6c9 c53a98a 36be6c9 dd90ceb b0053e0 c53a98a e7262bc 36be6c9 c53a98a cf5703f 36be6c9 d8e675b 36be6c9 e7262bc 36be6c9 e7262bc 36be6c9 e7262bc 36be6c9 81db2e6 36be6c9 81db2e6 36be6c9 6d7bce2 36be6c9 4c614bb b0053e0 9f8dd33 6d7bce2 b0053e0 9f8dd33 b0053e0 e7262bc 36be6c9 b0053e0 e7262bc 36be6c9 e7262bc 36be6c9 e7262bc 36be6c9 e7262bc 36be6c9 b0053e0 36be6c9 2762086 36be6c9 dd90ceb 36be6c9 f1d85a1 4c614bb f1d85a1 36be6c9 6d7bce2 e7262bc 6d7bce2 e7262bc 29575cc e7262bc 29575cc 36be6c9 29575cc 82224c0 36be6c9 995b24d 36be6c9 d57e9e4 36be6c9 e7262bc 36be6c9 e7262bc 36be6c9 e7262bc 36be6c9 e7262bc 36be6c9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
import os
import openai
import gradio as gr
from langchain import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
import datetime
NUM_WORDS_DEFAULT = 80
TEMPERATURE_DEFAULT = 0.5
EMOTION_DEFAULT = "N/A"
LITERARY_STYLE_DEFAULT = "Poetry"
CHARACTER_DEFAULT = "The Old Lady Who Swallowed A Fly"
# FORMALITY_DEFAULT = "Casual"
# TRANSLATE_TO_DEFAULT = "Don't translate"
# PROMPT_TEMPLATE = PromptTemplate(
# input_variables=["original_words", "num_words", "formality", "emotions", "translate_to", "literary_style", "character"],
# template="Express {num_words} in a {formality} manner, "
# "{emotions}{translate_to}{literary_style}{character} the following: \n{original_words}\n",
# )
PROMPT_TEMPLATE = PromptTemplate(
input_variables=["num_words", "literary_style", "emotions", "character", "original_words"],
template="{num_words}{literary_style}{emotions}{character} express the following: \n{original_words}\n",
)
def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain):
if api_key:
print("temperature: ", temperature)
openai_api_key = api_key
os.environ["OPENAI_API_KEY"] = api_key
llm = OpenAI(model_name='text-davinci-003', temperature=temperature, max_tokens=512)
os.environ["OPENAI_API_KEY"] = ""
llm_chain = LLMChain(llm=llm, prompt=PROMPT_TEMPLATE, verbose=True)
return openai_api_key, llm_chain
# def transform_text(desc, openai_api_key, temperature, llm_chain, num_words, formality,
# anticipation_level, joy_level, trust_level,
# fear_level, surprise_level, sadness_level, disgust_level, anger_level,
# translate_to, literary_style, character):
def transform_text(desc, openai_api_key, temperature, llm_chain, num_words,
anticipation_level, joy_level, trust_level, fear_level,
surprise_level, sadness_level, disgust_level, anger_level,
literary_style, character):
if not openai_api_key or openai_api_key == "":
return "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>"
num_words_prompt = ""
if num_words and int(num_words) != 0:
num_words_prompt = "using up to " + str(num_words) + " words, "
# Change some arguments to lower case
# formality = formality.lower()
anticipation_level = anticipation_level.lower()
joy_level = joy_level.lower()
trust_level = trust_level.lower()
fear_level = fear_level.lower()
surprise_level = surprise_level.lower()
sadness_level = sadness_level.lower()
disgust_level = disgust_level.lower()
anger_level = anger_level.lower()
llm_chain.llm.temperature = temperature
# put all emotions into a list
emotions = []
if anticipation_level != "n/a":
emotions.append(anticipation_level)
if joy_level != "n/a":
emotions.append(joy_level)
if trust_level != "n/a":
emotions.append(trust_level)
if fear_level != "n/a":
emotions.append(fear_level)
if surprise_level != "n/a":
emotions.append(surprise_level)
if sadness_level != "n/a":
emotions.append(sadness_level)
if disgust_level != "n/a":
emotions.append(disgust_level)
if anger_level != "n/a":
emotions.append(anger_level)
emotions_str = ""
if len(emotions) > 0:
if len(emotions) == 1:
emotions_str = "with emotion of " + emotions[0] + ", "
else:
emotions_str = "with emotions of " + ", ".join(emotions[:-1]) + " and " + emotions[-1] + ", "
# translate_to_str = ""
# if translate_to != TRANSLATE_TO_DEFAULT:
# translate_to_str = "translated to " + translate_to + ", "
literary_style_str = "as a poem, "
if literary_style != LITERARY_STYLE_DEFAULT:
if literary_style == "Haiku":
literary_style_str = "as a haiku, "
elif literary_style == "Limerick":
literary_style_str = "as a limerick, "
# elif literary_style == "Joke":
# literary_style_str = "as a very funny joke with a setup and punchline, "
elif literary_style == "Knock-knock":
literary_style_str = "as a very funny knock-knock joke, "
character_str = "as if written by The Old Lady Who Swallowed A Fly, "
if character != CHARACTER_DEFAULT:
if character == "The Old Woman Who Lived In A Shoe":
character_str = "as if written by the Old Woman Who Lived In A Shoe, "
# elif character == "Mary Had A Little Lamb":
# character_str = "as if written by Mary from Mary Had A Little Lamb, "
elif character == "Humpty-Dumpty":
character_str = "as if written by Humpty Dumpty, "
# formatted_prompt = PROMPT_TEMPLATE.format(
# original_words=desc,
# num_words=num_words_prompt,
# formality=formality,
# emotions=emotions_str,
# translate_to=translate_to_str,
# literary_style=literary_style_str,
# character=character_str,
# )
formatted_prompt = PROMPT_TEMPLATE.format(
original_words=desc,
num_words=num_words_prompt,
emotions=emotions_str,
literary_style=literary_style_str,
character=character_str,
)
# generated_text = llm_chain.run({'original_words': desc, 'num_words': num_words_prompt, 'formality': formality,
# 'emotions': emotions_str, 'translate_to': translate_to_str,
# 'literary_style': literary_style_str, 'character': character_str}).strip()
generated_text = llm_chain.run({'original_words': desc, 'num_words': num_words_prompt,
'emotions': emotions_str, 'literary_style': literary_style_str,
'character': character_str}).strip()
# replace all newlines with <br> in generated_text
generated_text = generated_text.replace("\n", "<br>")
prompt_plus_generated = "<b>GPT prompt:</b> " + formatted_prompt + "<br/><br/><code>" + generated_text + "</code>"
print("\n==== date/time: " + str(datetime.datetime.now() - datetime.timedelta(hours=5)) + " ====")
print("temperature: ", temperature)
print("prompt_plus_generated: " + prompt_plus_generated)
return prompt_plus_generated
def update_foo(widget, state):
if widget:
state = widget
return state
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
with block:
openai_api_key_state = gr.State()
temperature_state = gr.State(TEMPERATURE_DEFAULT)
llm_chain_state = gr.State()
num_words_state = gr.State(NUM_WORDS_DEFAULT)
# formality_state = gr.State(FORMALITY_DEFAULT)
anticipation_level_state = gr.State(EMOTION_DEFAULT)
joy_level_state = gr.State(EMOTION_DEFAULT)
trust_level_state = gr.State(EMOTION_DEFAULT)
fear_level_state = gr.State(EMOTION_DEFAULT)
surprise_level_state = gr.State(EMOTION_DEFAULT)
sadness_level_state = gr.State(EMOTION_DEFAULT)
disgust_level_state = gr.State(EMOTION_DEFAULT)
anger_level_state = gr.State(EMOTION_DEFAULT)
# translate_to_state = gr.State(TRANSLATE_TO_DEFAULT)
literary_style_state = gr.State(LITERARY_STYLE_DEFAULT)
character_state = gr.State(CHARACTER_DEFAULT)
with gr.Row():
temperature_slider = gr.Slider(label="GPT Temperature", value=TEMPERATURE_DEFAULT, minimum=0.0, maximum=1.0,
step=0.1)
title = gr.Markdown ("""<h3><center>GPT-3 Grimm Verses</center></h3>""")
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key",
show_label=False, lines=1, type='password')
output = gr.Markdown("")
with gr.Row():
request = gr.Textbox(label="Express this: ",
value="",
placeholder="Ex: My armpits are sweating gravy.")
submit = gr.Button(value="Express", variant="secondary").style(full_width=False)
with gr.Row():
with gr.Column():
gr.Examples(
examples=[
"My armpits are sweating gravy",
"My skull is filled with solid bone",
"Something underneath the house smells horrible",
],
inputs=request
)
num_words_slider = gr.Slider(label="Max number of words to generate (0 for don't care)",
value=NUM_WORDS_DEFAULT, minimum=0, maximum=100, step=10)
num_words_slider.change(update_foo,
inputs=[num_words_slider, num_words_state],
outputs=[num_words_state])
character_radio = gr.Radio(label="As if written by:", choices=["The Old Lady Who Swallowed A Fly", "The Old Woman Who Lived In A Shoe", "Humpty-Dumpty"],
value=CHARACTER_DEFAULT)
character_radio.change(update_foo,
inputs=[character_radio, character_state],
outputs=[character_state])
literary_style_radio = gr.Radio(label="Literary style:", choices=[
LITERARY_STYLE_DEFAULT, "Haiku", "Limerick", "Knock-knock"],
value=LITERARY_STYLE_DEFAULT)
literary_style_radio.change(update_foo,
inputs=[literary_style_radio, literary_style_state],
outputs=[literary_style_state])
# formality_radio = gr.Radio(label="Formality:", choices=["Casual", "Polite", "Honorific"],
# value=FORMALITY_DEFAULT)
# formality_radio.change(update_foo,
# inputs=[formality_radio, formality_state],
# outputs=[formality_state])
# translate_to_radio = gr.Radio(label="Translate to:", choices=[TRANSLATE_TO_DEFAULT, "emojis", "Gen Z slang", "how the stereotypical Karen would say it", "Pirate"], value=TRANSLATE_TO_DEFAULT)
# translate_to_radio = gr.Radio(label="Translate to:", choices=[
# TRANSLATE_TO_DEFAULT, "Arabic", "British English", "Chinese (Simplified)", "Chinese (Traditional)",
# "Czech", "Danish", "Dutch", "emojis", "English", "Finnish", "French", "Gen Z slang", "German", "Greek",
# "Hebrew", "Hindi", "Hungarian", "Indonesian", "Italian", "Japanese",
# "how the stereotypical Karen would say it",
# "Klingon", "Korean", "Norwegian", "Old English", "Pirate", "Polish", "Portuguese", "Romanian",
# "Russian", "Spanish", "Strange Planet expospeak technical talk", "Swedish", "Thai", "Turkish", "Vietnamese", "Yoda"], value=TRANSLATE_TO_DEFAULT)
# translate_to_radio.change(update_foo,
# inputs=[translate_to_radio, translate_to_state],
# outputs=[translate_to_state])
with gr.Column():
with gr.Accordion("Emotions", open=True):
anticipation_level_radio = gr.Radio(label="Anticipation level:",
choices=[EMOTION_DEFAULT, "Interest", "Anticipation", "Vigilance"],
value=EMOTION_DEFAULT)
anticipation_level_radio.change(update_foo,
inputs=[anticipation_level_radio, anticipation_level_state],
outputs=[anticipation_level_state])
joy_level_radio = gr.Radio(label="Joy level:",
choices=[EMOTION_DEFAULT, "Serenity", "Joy", "Ecstasy"],
value=EMOTION_DEFAULT)
joy_level_radio.change(update_foo,
inputs=[joy_level_radio, joy_level_state],
outputs=[joy_level_state])
trust_level_radio = gr.Radio(label="Trust level:",
choices=[EMOTION_DEFAULT, "Acceptance", "Trust", "Admiration"],
value=EMOTION_DEFAULT)
trust_level_radio.change(update_foo,
inputs=[trust_level_radio, trust_level_state],
outputs=[trust_level_state])
fear_level_radio = gr.Radio(label="Fear level:",
choices=[EMOTION_DEFAULT, "Apprehension", "Fear", "Terror"],
value=EMOTION_DEFAULT)
fear_level_radio.change(update_foo,
inputs=[fear_level_radio, fear_level_state],
outputs=[fear_level_state])
surprise_level_radio = gr.Radio(label="Surprise level:",
choices=[EMOTION_DEFAULT, "Distraction", "Surprise", "Amazement"],
value=EMOTION_DEFAULT)
surprise_level_radio.change(update_foo,
inputs=[surprise_level_radio, surprise_level_state],
outputs=[surprise_level_state])
sadness_level_radio = gr.Radio(label="Sadness level:",
choices=[EMOTION_DEFAULT, "Pensiveness", "Sadness", "Grief"],
value=EMOTION_DEFAULT)
sadness_level_radio.change(update_foo,
inputs=[sadness_level_radio, sadness_level_state],
outputs=[sadness_level_state])
disgust_level_radio = gr.Radio(label="Disgust level:",
choices=[EMOTION_DEFAULT, "Boredom", "Disgust", "Loathing"],
value=EMOTION_DEFAULT)
disgust_level_radio.change(update_foo,
inputs=[disgust_level_radio, disgust_level_state],
outputs=[disgust_level_state])
anger_level_radio = gr.Radio(label="Anger level:",
choices=[EMOTION_DEFAULT, "Annoyance", "Anger", "Rage"],
value=EMOTION_DEFAULT)
anger_level_radio.change(update_foo,
inputs=[anger_level_radio, anger_level_state],
outputs=[anger_level_state])
gr.HTML("""
<center>Based on an app by <a target='_blank' href='https://twitter.com/JavaFXpert'>@JavaFXpert</a>.</center>""")
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
# submit.click(transform_text,
# inputs=[
# request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
# formality_state,
# anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
# surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
# translate_to_state, literary_style_state, character_state],
# outputs=[output])
submit.click(transform_text,
inputs=[
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state, literary_style_state, character_state],
outputs=[output])
# request.submit(transform_text,
# inputs=[
# request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
# formality_state,
# anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
# surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
# translate_to_state, literary_style_state, character_state],
# outputs=[output])
request.submit(transform_text,
inputs=[
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state,
anticipation_level_state, joy_level_state, trust_level_state, fear_level_state,
surprise_level_state, sadness_level_state, disgust_level_state, anger_level_state,
literary_style_state, character_state],
outputs=[output])
openai_api_key_textbox.change(set_openai_api_key,
inputs=[openai_api_key_textbox, openai_api_key_state, temperature_state,
llm_chain_state],
outputs=[openai_api_key_state, llm_chain_state])
temperature_slider.change(update_foo,
inputs=[temperature_slider, temperature_state],
outputs=[temperature_state])
block.launch()
|