Caerii commited on
Commit
e687797
·
1 Parent(s): a60208c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -10
app.py CHANGED
@@ -1,8 +1,9 @@
 
1
  import gradio as gr
2
  import openai
3
 
4
- # Replace 'your-api-key' with your actual OpenAI API key
5
- openai.api_key = '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'
6
 
7
  def openai_chatbot(message, history):
8
  # Convert history format for OpenAI API
@@ -13,14 +14,17 @@ def openai_chatbot(message, history):
13
  history_openai_format.append({"role": "user", "content": message})
14
 
15
  # Call the OpenAI ChatCompletion API
16
- response = openai.ChatCompletion.create(
17
- model='gpt-3.5-turbo',
18
- messages=history_openai_format,
19
- temperature=1.0
20
- )
21
-
22
- # Extract and return the response
23
- return response.choices[0].message['content']
 
 
 
24
 
25
  chat_interface = gr.ChatInterface(
26
  fn=openai_chatbot,
 
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
 
8
  def openai_chatbot(message, history):
9
  # Convert history format for OpenAI API
 
14
  history_openai_format.append({"role": "user", "content": message})
15
 
16
  # Call the OpenAI ChatCompletion API
17
+ try:
18
+ response = openai.ChatCompletion.create(
19
+ model='gpt-3.5-turbo',
20
+ messages=history_openai_format,
21
+ temperature=1.0
22
+ )
23
+ # Extract and return the response
24
+ return response.choices[0].message['content']
25
+ except Exception as e:
26
+ print(f"An error occurred: {e}")
27
+ return "Sorry, there was an error processing your request."
28
 
29
  chat_interface = gr.ChatInterface(
30
  fn=openai_chatbot,