import openai import gradio as gr def chat_with_openai(api_key, user_input): """ Function to send user input to OpenAI’s Chat Completion API and return the response. """ openai.api_key = api_key response = openai.ChatCompletion.create( model='gpt-3.5-turbo', # You can change the model as needed messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": user_input}, ] ) return response.choices[0].message['content'] def main(): description = "This is a simple interface to interact with OpenAI’s Chat Completion API. Please enter your API key and your message." with gr.Blocks() as demo: with gr.Row(): api_key_input = gr.Textbox(label="API Key", placeholder="Enter your OpenAI API key here", show_label=True, type="password") user_input = gr.Textbox(label="Your Message", placeholder="Enter your message here") submit_btn = gr.Button("Submit") output = gr.Textbox(label="Chatbot Response") submit_btn.click(fn=chat_with_openai, inputs=[api_key_input, user_input], outputs=output) demo.launch() if __name__ == "__main__": main()