Caerii commited on
Commit
2b2ddcf
·
1 Parent(s): bf1f92e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -5,16 +5,15 @@ import gradio as gr
5
  # Access the API key from the environment variable
6
  openai.api_key = os.getenv('OPENAI_API_KEY')
7
 
8
- def openai_chatbot(message, history):
9
  try:
10
- # Convert history format for OpenAI API
11
  conversation_history = []
12
- for human, assistant in history:
13
- conversation_history.append(human)
14
- conversation_history.append(assistant)
 
15
  conversation_history.append(message)
16
 
17
- # Call the OpenAI API
18
  response = openai.Completion.create(
19
  model='gpt-3.5-turbo',
20
  prompt=conversation_history,
@@ -25,7 +24,6 @@ def openai_chatbot(message, history):
25
  user=None
26
  )
27
 
28
- # Extract and return the response
29
  return response.choices[0].text.strip()
30
  except Exception as e:
31
  print(f"An error occurred: {e}")
@@ -33,7 +31,10 @@ def openai_chatbot(message, history):
33
 
34
  chat_interface = gr.Interface(
35
  fn=openai_chatbot,
36
- inputs=[gr.Textbox(label="Your Message")],
 
 
 
37
  outputs=[gr.Textbox(label="OpenAI Response")],
38
  title="OpenAI Chatbot",
39
  description="Talk to a bot powered by OpenAI GPT-3."
 
5
  # Access the API key from the environment variable
6
  openai.api_key = os.getenv('OPENAI_API_KEY')
7
 
8
+ def openai_chatbot(message, history=None):
9
  try:
 
10
  conversation_history = []
11
+ if history:
12
+ for human, assistant in history:
13
+ conversation_history.append(human)
14
+ conversation_history.append(assistant)
15
  conversation_history.append(message)
16
 
 
17
  response = openai.Completion.create(
18
  model='gpt-3.5-turbo',
19
  prompt=conversation_history,
 
24
  user=None
25
  )
26
 
 
27
  return response.choices[0].text.strip()
28
  except Exception as e:
29
  print(f"An error occurred: {e}")
 
31
 
32
  chat_interface = gr.Interface(
33
  fn=openai_chatbot,
34
+ inputs=[
35
+ gr.Textbox(label="Your Message"),
36
+ gr.State(label="History")
37
+ ],
38
  outputs=[gr.Textbox(label="OpenAI Response")],
39
  title="OpenAI Chatbot",
40
  description="Talk to a bot powered by OpenAI GPT-3."