|
import gradio as gr |
|
import openai |
|
import os |
|
from dotenv import load_dotenv |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
API_KEY = os.getenv("OPENAI_API_KEY") |
|
openai.api_key = API_KEY |
|
|
|
|
|
def chat_with_model(prompt): |
|
if not API_KEY: |
|
return "Error: API key not found. Please set it in the environment variables." |
|
|
|
try: |
|
response = openai.ChatCompletion.create( |
|
model="gpt-4", |
|
messages=[{"role": "user", "content": prompt}] |
|
) |
|
return response["choices"][0]["message"]["content"] |
|
except openai.error.OpenAIError as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=chat_with_model, |
|
inputs="text", |
|
outputs="text", |
|
title="ZEN AI Chatbot", |
|
description="A simple chatbot powered by OpenAI's GPT models." |
|
) |
|
|
|
if __name__ == "__main__": |
|
iface.launch() |