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 = 20 PROMPT_TEMPLATE = PromptTemplate( input_variables=["original_words", "num_words"], template="Restate, into an HTML div, in about {num_words} words, the following, with emotions of admiration, " "in a formal manner, in a passive voice: \n{original_words}\n", ) # 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: " def set_openai_api_key(api_key, openai_api_key, temperature, llm_chain): if api_key: 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 openai_create(prompt, openai_api_key, temperature): print("\n==== date/time: " + str(datetime.datetime.now()) + " ====") print("prompt: " + prompt) print("temperature: ", temperature) openai.api_key = openai_api_key response = openai.Completion.create( model="text-davinci-003", prompt=prompt, temperature=temperature, max_tokens=2000, top_p=1, frequency_penalty=0, presence_penalty=0 ) return response.choices[0].text def desc2sheet(desc, openai_api_key, temperature, llm_chain, num_words): if not openai_api_key or openai_api_key == "": return "
Please paste your OpenAI API key (see https://beta.openai.com)" html = llm_chain.run({'original_words': desc, 'num_words': num_words}) return html def update_temperature(temp_slider, temp_state): if temp_slider: temp_state = temp_slider return temp_state def update_num_words(slider, state): if slider: state = slider return state block = gr.Blocks(css=".gradio-container {background-color: lightgray}") with block: openai_api_key_state = gr.State() temperature_state = gr.State(0.0) llm_chain_state = gr.State() num_words_state = gr.State(NUM_WORDS_DEFAULT) with gr.Row(): temperature_slider = gr.Slider(label="GPT Temperature", value=0.0, minimum=0.0, maximum=1.0, step=0.1) title = gr.Markdown("""