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 "
Please paste your OpenAI API key (see https://beta.openai.com)" 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
" + generated_text + "
"
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 ("""