File size: 1,476 Bytes
945c107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import openai
import gradio as gr

openai.api_key = os.getenv("OPENAI_API_KEY")

start_sequence = "\nCoach:"
restart_sequence = "\nMe:"

prompt = "The following is a conversation with an instructor. The instructor is an expert in opportunity cost. You will act as the instructor, I will be the student."

def completion_create(prompt):
    
    response = openai.Completion.create(
        model = "text-davinci-003",
        prompt=prompt,
        temperature=0.9,
        max_tokens=150,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0.6,
        stop=[" Coach:", " Me:"])
    print("Raw response: ")
    print(response)
    return response.choices[0].text

def chat_clone(input, history):
    history = history or []
    s = list(sum(history, ()))
    print(s)
    s.append(input)
    inp = ' '.join(s)
    output = completion_create(inp)
    if (len(output) < 5): print("ERROR, short output")
    print(output)
    history.append((input, output))
    return history, history

block = gr.Blocks(css="main.css", title="Coaching Demo")

with block:
    gr.Markdown("""<h1><center>Coaching Demo</center></h1>""")
    chatbot = gr.Chatbot()
    input = gr.Textbox(placeholder=None,value="")
    state = gr.State()
    input.submit(chat_clone, inputs=[input, state], outputs=[chatbot, state])
    submit = gr.Button("Ask")
    submit.click(chat_clone, inputs=[input, state], outputs=[chatbot, state])
    
block.launch(share=False,debug=True)