Caerii commited on
Commit
d89a126
·
1 Parent(s): a8c94a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -6
app.py CHANGED
@@ -7,9 +7,11 @@ print(gr.__version__)
7
 
8
  theme = MonolithTheme()
9
 
10
- def openai_chatbot(message, history=None):
11
- client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
 
12
 
 
13
  try:
14
  conversation_history = []
15
  if history:
@@ -23,11 +25,10 @@ def openai_chatbot(message, history=None):
23
  messages=conversation_history,
24
  )
25
 
26
- response_text = chat_completion.choices[0].message["content"]
27
-
28
  new_history = history if history else []
29
  new_history.append((message, response_text))
30
-
31
  return response_text, new_history
32
  except Exception as e:
33
  print(f"An error occurred: {e}")
@@ -48,4 +49,4 @@ chat_interface = gr.Interface(
48
  )
49
 
50
  if __name__ == "__main__":
51
- chat_interface.launch(debug=True)
 
7
 
8
  theme = MonolithTheme()
9
 
10
+ # Initialize the OpenAI client once and use it in the openai_chatbot function
11
+ api_key = os.getenv("OPENAI_API_KEY")
12
+ client = openai.OpenAI(api_key=api_key)
13
 
14
+ def openai_chatbot(message, history=None):
15
  try:
16
  conversation_history = []
17
  if history:
 
25
  messages=conversation_history,
26
  )
27
 
28
+ response_text = chat_completion.choices[0].message.content
 
29
  new_history = history if history else []
30
  new_history.append((message, response_text))
31
+
32
  return response_text, new_history
33
  except Exception as e:
34
  print(f"An error occurred: {e}")
 
49
  )
50
 
51
  if __name__ == "__main__":
52
+ chat_interface.launch(debug=True)