Spaces:
Build error
Build error
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 "<pre>Please paste your OpenAI API key (see https://beta.openai.com)</pre>" | |
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("""<h3><center>GPT-3.5 Table-inator</center></h3>""") | |
openai_api_key_textbox = gr.Textbox(placeholder="Paste your OpenAI API key", | |
show_label=False, lines=1, type='password') | |
table = gr.Markdown("") | |
with gr.Row(): | |
request = gr.Textbox(label="GPT prompt: " + PROMPT_TEMPLATE.template_format, | |
value="Paris in the spring", | |
placeholder="Ex: Computer languages") | |
submit = gr.Button(value="Create", variant="secondary").style(full_width=False) | |
num_words_slider = gr.Slider(label="Number of words", value=NUM_WORDS_DEFAULT, minimum=10, maximum=100, step=10) | |
submit.click(desc2sheet, | |
inputs=[ | |
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state], | |
outputs=[table]) | |
request.submit(desc2sheet, | |
inputs=[ | |
request, openai_api_key_state, temperature_state, llm_chain_state, num_words_state], | |
outputs=[table]) | |
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_temperature, | |
inputs=[temperature_slider, temperature_state], | |
outputs=[temperature_state]) | |
num_words_slider.change(update_num_words, | |
inputs=[num_words_slider, num_words_state], | |
outputs=[num_words_state]) | |
block.launch() | |