|
import os
|
|
from dotenv import load_dotenv
|
|
from openai import Client
|
|
import gradio as gr
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
api_key = os.getenv('OPENAI_API_KEY')
|
|
|
|
if not api_key:
|
|
raise ValueError("API key not found. Please set your OPENAI_API_KEY in the environment.")
|
|
|
|
|
|
client = Client(api_key=api_key)
|
|
|
|
def chatbot(input):
|
|
"""Chatbot function using the OpenAI API"""
|
|
if input:
|
|
messages = [
|
|
{"role": "system", "content": "You are a helpful and kind AI Assistant."},
|
|
{"role": "user", "content": input},
|
|
]
|
|
response = client.chat.complete(
|
|
engine="text-davinci-003",
|
|
messages=messages,
|
|
max_tokens=150,
|
|
n=1,
|
|
stop=None,
|
|
temperature=0.7,
|
|
)
|
|
reply = response.choices[0].text
|
|
return reply
|
|
|
|
input_text = gr.Textbox(lines=7, label="Chat with AI")
|
|
output_text = gr.Textbox(label="Reply")
|
|
|
|
chatbot_interface = gr.Interface(fn=chatbot, inputs=input_text, outputs=output_text, title="AI Chatbot",
|
|
description="Ask anything you want", theme="compact")
|
|
chatbot_interface.launch(share=True)
|
|
|