Spaces:
Sleeping
Sleeping
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() |