cyberandy commited on
Commit
6127056
·
verified ·
1 Parent(s): 8c61d2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -10
app.py CHANGED
@@ -19,7 +19,12 @@ import json
19
  import os
20
  from openai import OpenAI
21
 
22
-
 
 
 
 
 
23
  # ---------------------------------------------------------------------------- #
24
  # App Config. & Styling
25
  # ---------------------------------------------------------------------------- #
@@ -71,7 +76,6 @@ elif not openai_api_key:
71
  st.error("The OpenAI API key is not properly configured. Check your environment variables!")
72
  else:
73
  # Generate the report by calling the ChatGPT Turbo API and the WooRank API
74
-
75
  # First, let's create a simple PromptTemplate class since it's not imported
76
  class PromptTemplate:
77
  def __init__(self, template, input_variables):
@@ -106,8 +110,12 @@ else:
106
  YOU ARE WRITING THE REPORT IN HTML USING A TEMPLATE.'''
107
  }]
108
 
109
- # Initialize OpenAI client
110
- client = OpenAI(api_key=openai_api_key)
 
 
 
 
111
 
112
  # Construct messages for the chat API
113
  messages = []
@@ -205,16 +213,26 @@ else:
205
  user_message = prompt.format(**run_statement)
206
  messages.append({"role": "user", "content": user_message})
207
 
208
- # Make the API call
209
  try:
210
  response = client.chat.completions.create(
211
- model="gpt-4", # Fixed model name from "gpt-4o"
212
- messages=messages
 
 
213
  )
214
- out = response.choices[0].message.content
 
 
 
215
  except Exception as e:
216
- out = f"Sorry, there was an error with the OpenAI API: {e}"
217
- print(f"OpenAI API Error: {str(e)}") # Log the error for debugging
 
 
 
 
 
218
 
219
  return out
220
 
 
19
  import os
20
  from openai import OpenAI
21
 
22
+ # Unset any proxy environment variables that might be causing issues
23
+ proxy_vars = ['HTTP_PROXY', 'HTTPS_PROXY', 'http_proxy', 'https_proxy']
24
+ for var in proxy_vars:
25
+ if var in os.environ:
26
+ del os.environ[var]
27
+
28
  # ---------------------------------------------------------------------------- #
29
  # App Config. & Styling
30
  # ---------------------------------------------------------------------------- #
 
76
  st.error("The OpenAI API key is not properly configured. Check your environment variables!")
77
  else:
78
  # Generate the report by calling the ChatGPT Turbo API and the WooRank API
 
79
  # First, let's create a simple PromptTemplate class since it's not imported
80
  class PromptTemplate:
81
  def __init__(self, template, input_variables):
 
110
  YOU ARE WRITING THE REPORT IN HTML USING A TEMPLATE.'''
111
  }]
112
 
113
+ # Initialize OpenAI client with basic configuration
114
+ client = OpenAI(
115
+ api_key=openai_api_key,
116
+ base_url="https://api.openai.com/v1",
117
+ timeout=60.0 # 60 seconds timeout
118
+ )
119
 
120
  # Construct messages for the chat API
121
  messages = []
 
213
  user_message = prompt.format(**run_statement)
214
  messages.append({"role": "user", "content": user_message})
215
 
216
+ # Make the API call with better error handling
217
  try:
218
  response = client.chat.completions.create(
219
+ model="gpt-4",
220
+ messages=messages,
221
+ temperature=0.7,
222
+ max_tokens=1500
223
  )
224
+ if hasattr(response.choices[0].message, 'content'):
225
+ out = response.choices[0].message.content
226
+ else:
227
+ out = "Error: No content in response"
228
  except Exception as e:
229
+ error_msg = str(e)
230
+ print(f"OpenAI API Error: {error_msg}") # Log the error for debugging
231
+ if "proxies" in error_msg:
232
+ # Handle proxy-related errors
233
+ out = "Error: Proxy configuration issue. Please check your environment settings."
234
+ else:
235
+ out = f"Sorry, there was an error with the OpenAI API: {error_msg}"
236
 
237
  return out
238