Eric Michael Martinez commited on
Commit
484ce68
·
1 Parent(s): bd0ba0a
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -2,6 +2,10 @@ import gradio as gr
2
  import openai
3
  import examples as chatbot_examples
4
 
 
 
 
 
5
  # Define a function to get the AI's reply using the OpenAI API
6
  def get_ai_reply(model, system_message, message, history_state):
7
  # Initialize the messages list with the system message
@@ -31,8 +35,9 @@ def chat(model, system_message, message, chatbot_messages, history_state):
31
  # Try to get the AI's reply using the get_ai_reply function
32
  try:
33
  ai_reply = get_ai_reply(model, system_message, message, history_state)
34
- except:
35
  # If an error occurs, return None and the current chatbot_messages and history_state
 
36
  return None, chatbot_messages, history_state
37
 
38
  # Append the user's message and the AI's reply to the chatbot_messages list
@@ -56,9 +61,12 @@ def get_chatbot_app(additional_examples=[]):
56
 
57
  # Define a function to choose an example based on the index
58
  def choose_example(index):
59
- system_message = examples[index]["system_message"].strip()
60
- user_message = examples[index]["message"].strip()
61
- return system_message, user_message, [], []
 
 
 
62
 
63
  # Create the Gradio interface using the Blocks layout
64
  with gr.Blocks() as app:
@@ -91,7 +99,12 @@ def get_chatbot_app(additional_examples=[]):
91
  example_load_btn.click(choose_example, inputs=[example_dropdown], outputs=[system_message, message, chatbot, history_state])
92
  # Connect the send button to the chat function
93
  btn.click(chat, inputs=[model_selector, system_message, message, chatbot, history_state], outputs=[message, chatbot, history_state])
94
- # Launch the Gradio interface
 
 
 
 
 
95
  return app
96
 
97
  # Call the launch_chatbot function to start the chatbot interface using Gradio
 
2
  import openai
3
  import examples as chatbot_examples
4
 
5
+ def save_settings(openai_api_base, openai_api_key):
6
+ openai.api_base=openai_api_base
7
+ openai.api_key=openai_api_key
8
+
9
  # Define a function to get the AI's reply using the OpenAI API
10
  def get_ai_reply(model, system_message, message, history_state):
11
  # Initialize the messages list with the system message
 
35
  # Try to get the AI's reply using the get_ai_reply function
36
  try:
37
  ai_reply = get_ai_reply(model, system_message, message, history_state)
38
+ except Exception as e:
39
  # If an error occurs, return None and the current chatbot_messages and history_state
40
+ print(e)
41
  return None, chatbot_messages, history_state
42
 
43
  # Append the user's message and the AI's reply to the chatbot_messages list
 
61
 
62
  # Define a function to choose an example based on the index
63
  def choose_example(index):
64
+ if(index!=None):
65
+ system_message = examples[index]["system_message"].strip()
66
+ user_message = examples[index]["message"].strip()
67
+ return system_message, user_message, [], []
68
+ else:
69
+ return "", "", [], []
70
 
71
  # Create the Gradio interface using the Blocks layout
72
  with gr.Blocks() as app:
 
99
  example_load_btn.click(choose_example, inputs=[example_dropdown], outputs=[system_message, message, chatbot, history_state])
100
  # Connect the send button to the chat function
101
  btn.click(chat, inputs=[model_selector, system_message, message, chatbot, history_state], outputs=[message, chatbot, history_state])
102
+ with gr.Tab("Settings"):
103
+ openai_api_base = gr.Textbox(label="OpenAI API Base", value=openai.api_base)
104
+ openai_api_key = gr.Textbox(label="OpenAI API Key", type="password", value=openai.api_key)
105
+ save_settings_btn = gr.Button(value="Save")
106
+ save_settings_btn.click(save_settings, inputs=[openai_api_base, openai_api_key])
107
+ # Return the app
108
  return app
109
 
110
  # Call the launch_chatbot function to start the chatbot interface using Gradio