import gradio as gr import openai_secret_manager # Function to get the secret values def get_secrets(): secrets = openai_secret_manager.get_secret("your_api_key_secret_name") instructor_prompt = openai_secret_manager.get_secret("your_instructor_prompt_secret_name") return secrets, instructor_prompt # Get the secrets secrets, instructor_prompt = get_secrets() # Set your API key api_key = secrets["api_key"] with gr.Blocks() as demo: chat_history = gr.State(value=[]) def chat_with_instructor(message, history): import openai openai.api_key = api_key chat_input = instructor_prompt + "\nUser: " + message + "\nInstructor:" response = openai.Completion.create( engine="text-davinci-002", prompt=chat_input, max_tokens=50 # Adjust this for response length ).choices[0].text.strip() chat_history.value.append( { "user": message, "instructor": instructor_prompt, "bot": response, } ) return response def generate_json(chat_history): return chat_history.value chatbox = gr.ChatInterface( fn=chat_with_instructor, title="Chat with Instructor", description="Chat with the instructor using a predefined prompt.", examples=["Hi, can you help me with this?"], submit_btn="Send", stop_btn="Stop", retry_btn="Retry", undo_btn="Undo last message", clear_btn="Start a new conversation" ).queue() chat_history_json = gr.JSON(generate_json(chat_history)) gr.Markdown("### 📩 Generate the JSON file for your chat history!") gr.Interface(fn=generate_json, inputs=None, outputs=[chat_history_json]) demo.queue() demo.launch()