File size: 994 Bytes
940bdb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# download files
import openai
import gradio as gr
# import shutup
# shutup.please()

def chatbot(api_key, input):
    openai.api_key = api_key
    messages = [{"role": "system", "content": "You are a helpful and kind AI Assistant."}]

    if input:
        messages.append({"role": "user", "content": input})
        chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=messages
        )
        reply = chat.choices[0].message.content
        messages.append({"role": "assistant", "content": reply})
        return reply

api_key_input = gr.inputs.Textbox(lines=1, label="Enter your API key")
chat_input = gr.inputs.Textbox(lines=7, label="Chat with AI")
output_text = gr.outputs.Textbox(label="Reply")

def get_reply(api_key, input):
    return chatbot(api_key, input)

gr.Interface(
    fn=get_reply,
    inputs=[api_key_input, chat_input],
    outputs=output_text,
    title="AI Chatbot",
    description="Ask anything you want",
    theme="default"
).launch()