import openai import gradio as gr # Set your OpenAI API key openai.api_key = "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" # Initialize chat history with a system message messages = [{"role": "system", "content": "YOU ARE THE BEST DEVELOPER"}] # Define the chatbot function def CustomChatGPT(user_message): try: # Append the user's message to the chat history messages.append({"role": "user", "content": user_message}) # Generate a response from the chatbot response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) # Extract the assistant's reply from the response assistant_reply = response["choices"][0]["message"]["content"] # Append the assistant's reply to the chat history messages.append({"role": "assistant", "content": assistant_reply}) return assistant_reply except Exception as e: return f"An error occurred: {str(e)}" # Create a Gradio interface for the chatbot interface = gr.Interface( fn=CustomChatGPT, inputs=gr.inputs.Textbox(placeholder="Ask me anything!", label="Chat with the Best Developer's Assistant"), outputs=gr.outputs.Textbox(), title="ChatGPT - The Best Developer's Assistant", description="Ask questions or seek assistance from ChatGPT! This AI-powered assistant is here to help you.", ) # Customize the interface with CSS styles interface.css = """ .chat-message { border-radius: 10px; padding: 10px; margin: 5px; max-width: 80%; } .user-message { background-color: #e1f0fa; float: right; } .assistant-message { background-color: #f9f9f9; float: left; } .output { background-color: #f9f9f9; padding: 10px; border-radius: 10px; } .input { border-radius: 10px; } .gradio-interface { max-width: 600px; margin: 0 auto; } /* Center the interface */ .gradio-interface .btn-primary { background-color: #0078d4; border: none; } .gradio-interface .btn-primary:hover { background-color: #005a9e; } .gradio-interface .btn-secondary { background-color: #f9f9f9; color: #0078d4; border: none; } .gradio-interface .btn-secondary:hover { background-color: #e1f0fa; } """ # Launch the Gradio interface interface.launch(share=True)