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()