import gradio as gr import openai import logging # Initialize the logger with level INFO logging.basicConfig(filename='conversation_history.log', level=logging.INFO) # Initialize the conversation history conversation_history = [ { "role": "system", "content": "Your name is Joe Chip, a world class poker player..." } ] def setup_openai(api_key): openai.api_key = api_key return "API Key Set Successfully!" def ask_joe(api_key, text): # set up the api_key setup_openai(api_key) # Add the user's message to the conversation history conversation_history.append({ "role": "user", "content": text }) # Use the conversation history as the input to the model response = openai.ChatCompletion.create( model="gpt-4", messages=conversation_history, max_tokens=500, temperature=0.3 ) # Extract the model's message from the response model_message = response.choices[0].message['content'].strip() # Add the model's message to the conversation history conversation_history.append({ "role": "assistant", "content": model_message }) # Log the conversation history logging.info(f'User: {text}') logging.info(f'AI: {model_message}') return model_message iface = gr.Interface(fn=ask_joe, inputs=[gr.inputs.Textbox(label="OpenAI API Key"), gr.inputs.Textbox(label="Your question")], outputs="text") iface.launch()