File size: 1,448 Bytes
6d5f1d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import os
import gradio as gr
openai.api_key = os.environ['openai']
def write_essay(prompt, temperature, frequency_penalty, presence_penalty, max_token, model="gpt-3.5-turbo"):
    temp_list = prompt.split()
    temperature = temperature/10
    frequency_penalty = frequency_penalty/10 - 2
    presence_penalty = presence_penalty/10 -2
    if len(temp_list) >= 500:
        new_prompt = " ".join(temp_list[0:500])
    else:
        new_prompt = prompt
    
    response = openai.ChatCompletion.create(
        model=model,
        messages=[{"role": "user", "content": new_prompt}],
        #prompt=new_prompt,
        max_tokens=int(max_token),
        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",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"),"text"
                                           ], outputs="text")
demo.launch()