VISION23/V23ChatBot
1
1import os2import openai3import gradio as gr4 5#if you have OpenAI API key as an environment variable, enable the below6#openai.api_key = os.getenv("OPENAI_API_KEY")7 8#if you have OpenAI API key as a string, enable the below9openai.api_key = "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"10 11start_sequence = "\nAI:"12restart_sequence = "\nHuman: "13 14prompt = "The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and very friendly.\n\nHuman: Hello, who are you?\nAI: I am an AI created by OpenAI. How can I help you today?\nHuman: "15 16def openai_create(prompt):17 18 response = openai.Completion.create(19 model="text-davinci-003",20 prompt=prompt,21 temperature=0.9,22 max_tokens=150,23 top_p=1,24 frequency_penalty=0,25 presence_penalty=0.6,26 stop=[" Human:", " AI:"]27 )28 29 return response.choices[0].text30 31 32 33def chatgpt_clone(input, history):34 history = history or []35 s = list(sum(history, ()))36 s.append(input)37 inp = ' '.join(s)38 output = openai_create(inp)39 history.append((input, output))40 return history, history41 42 43block = gr.Blocks()44 45 46with block:47 gr.Markdown("""<h1><center> V23 CHATBOT </center></h1>48 """)49 chatbot = gr.Chatbot()50 message = gr.Textbox(placeholder=prompt)51 state = gr.State()52 submit = gr.Button("SEND")53 submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot, state])54 55block.launch(debug = True)