File size: 1,000 Bytes
f4f6479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import openai
import os
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Get the API key from the environment variable
API_KEY = os.getenv("OPENAI_API_KEY")
openai.api_key = API_KEY

# Function to interact with OpenAI API
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",  # Replace with your desired model
            messages=[{"role": "user", "content": prompt}]
        )
        return response["choices"][0]["message"]["content"]
    except openai.error.OpenAIError as e:
        return f"Error: {str(e)}"

# Gradio Interface
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()