Caerii commited on
Commit
376cb52
·
1 Parent(s): 73f17c3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -1,34 +1,40 @@
1
  import os
2
- import gradio as gr
3
  import openai
 
4
 
5
  # Access the API key from the environment variable
6
  openai.api_key = os.getenv('OPENAI_API_KEY')
7
- print(openai.api_key)
8
 
9
  def openai_chatbot(message, history):
10
- # Convert history format for OpenAI API
11
- history_openai_format = []
12
- for human, assistant in history:
13
- history_openai_format.append({"role": "user", "content": human})
14
- history_openai_format.append({"role": "assistant", "content": assistant})
15
- history_openai_format.append({"role": "user", "content": message})
16
-
17
- # Call the OpenAI ChatCompletion API
18
  try:
19
- response = openai.ChatCompletion.create(
 
 
 
 
 
 
 
 
20
  model='gpt-3.5-turbo',
21
- messages=history_openai_format,
22
- temperature=1.0
 
 
 
 
23
  )
 
24
  # Extract and return the response
25
- return response.choices[0].message['content']
26
  except Exception as e:
27
  print(f"An error occurred: {e}")
28
  return "Sorry, there was an error processing your request."
29
 
30
- chat_interface = gr.ChatInterface(
31
  fn=openai_chatbot,
 
 
32
  title="OpenAI Chatbot",
33
  description="Talk to a bot powered by OpenAI GPT-3."
34
  )
 
1
  import os
 
2
  import openai
3
+ import gradio as gr
4
 
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,
21
+ temperature=1.0,
22
+ max_tokens=150,
23
+ n=1,
24
+ stop=None,
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}")
32
  return "Sorry, there was an error processing your request."
33
 
34
+ chat_interface = gr.Interface(
35
  fn=openai_chatbot,
36
+ inputs=[gr.inputs.Textbox(label="Your Message")],
37
+ outputs=[gr.outputs.Textbox(label="OpenAI Response")],
38
  title="OpenAI Chatbot",
39
  description="Talk to a bot powered by OpenAI GPT-3."
40
  )