import openai import os import gradio as gr openai.api_key = os.environ['openai'] def write_essay(prompt, context, temperature, frequency_penalty, presence_penalty): model="gpt-3.5-turbo" temp_list = prompt.split() temp_list2 = context.split() temperature = temperature/10 frequency_penalty = frequency_penalty/10 - 2 presence_penalty = presence_penalty/10 -2 new_prompt = " ".join(temp_list[0:150]) new_prompt2 = " ".join(temp_list2[0:50]) response = openai.ChatCompletion.create( model=model, messages=[{"role": "system", "content": new_prompt2}, {"role": "user", "content": new_prompt},], stream=False, max_tokens=250, temperature=temperature, frequency_penalty=frequency_penalty, presence_penalty=presence_penalty ) message = response.choices[0]["message"]["content"] #print(message) return message demo = gr.Interface(fn=write_essay, inputs=["text","text", gr.Slider(0, 20, value=4, step=1, label="Creative vs Focus", info="Increase for creative and decrease for more focused"), gr.Slider(0, 40, value=20, step=1, label="Frequency of words", info="Increase to decrease repeated words"), gr.Slider(0, 40, value=20, step=1, label="Presence of words", info="Increase to decrease already appeared words") ], outputs="text") demo.launch()