File size: 1,559 Bytes
64c6199
849bf14
69fafb8
8a82df0
 
 
 
 
517a606
 
 
 
 
 
 
 
 
8a82df0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517a606
 
 
 
 
792173c
517a606
 
 
792173c
517a606
 
 
 
 
 
 
 
8a82df0
517a606
 
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
52
53
54
import subprocess
subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv"])
subprocess.check_call(["pip", "install", "-q", "openai"])
import gradio as gr
from transformers import TFAutoModelForCausalLM, AutoTokenizer
import openai
from dotenv import load_dotenv
import os

# load environment variables from .env file
load_dotenv()

# access the value of the OPENAI_API_KEY environment variable
api_key = os.getenv("OPENAI_API_KEY")

# install required packages
subprocess.check_call(["pip", "install", "-q", "gradio", "transformers", "python-dotenv", "openai"])

def openai_chat(prompt):
    completions = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        temperature=0.5,
    )

    message = completions.choices[0].text
    return message.strip()

def chatbot(input, history=[]):
    output = openai_chat(input)
    history.append((input, output))
    return output, history

# define Gradio interface
inputs = [
    gr.inputs.Textbox(label="Input"),
    gr.outputs.Label(type="hidden", label="Chat history", default=[])
]
outputs = [
    gr.outputs.Textbox(label="Output"),
    gr.outputs.Label(type="hidden", label="Updated chat history")
]

iface = gr.Interface(fn=chatbot,
                     inputs=inputs,
                     outputs=outputs,
                     title="OpenAI Chatbot",
                     description="A chatbot powered by OpenAI's GPT-3",
                     theme="compact")

# launch the interface
iface.launch()