OpenAI basic error handling

#2
Files changed (1) hide show
  1. app.py +28 -25
app.py CHANGED
@@ -202,32 +202,35 @@ def get_openAI_tags(image_urls):
202
  imageList = []
203
  for image in image_urls:
204
  imageList.append({"type": "image_url", "image_url": {"url": image}})
205
-
206
- openai_response = client.chat.completions.create(
207
- model="gpt-4o",
208
- messages=[
209
- {
210
- "role": "system",
211
- "content": [
 
 
 
 
 
 
212
  {
213
- "type": "text",
214
- "text": "You're a tagging assistant, you will help label and tag product pictures for my online e-commerce platform. Your tasks will be to return which angle the product images were taken from. You will have to choose from 'full-body', 'half-body', 'side', 'back', or 'zoomed' angles. You should label each of the images with one of these labels depending on which you think fits best (ideally, every label should be used at least once, but only if there are 5 or more images), and should respond with an unformatted dictionary where the key is a string representation of the url index of the url and the value is the assigned label."
215
- }
216
- ]
217
- },
218
- {
219
- "role": "user",
220
- "content": imageList
221
- },
222
- ],
223
- temperature=1,
224
- max_tokens=500,
225
- top_p=1,
226
- frequency_penalty=0,
227
- presence_penalty=0
228
- )
229
- response = json.loads(openai_response.choices[0].message.content)
230
- return response
231
 
232
 
233
  @spaces.GPU
 
202
  imageList = []
203
  for image in image_urls:
204
  imageList.append({"type": "image_url", "image_url": {"url": image}})
205
+ try:
206
+ openai_response = client.chat.completions.create(
207
+ model="gpt-4o",
208
+ messages=[
209
+ {
210
+ "role": "system",
211
+ "content": [
212
+ {
213
+ "type": "text",
214
+ "text": "You're a tagging assistant, you will help label and tag product pictures for my online e-commerce platform. Your tasks will be to return which angle the product images were taken from. You will have to choose from 'full-body', 'half-body', 'side', 'back', or 'zoomed' angles. You should label each of the images with one of these labels depending on which you think fits best (ideally, every label should be used at least once, but only if there are 5 or more images), and should respond with an unformatted dictionary where the key is a string representation of the url index of the url and the value is the assigned label."
215
+ }
216
+ ]
217
+ },
218
  {
219
+ "role": "user",
220
+ "content": imageList
221
+ },
222
+ ],
223
+ temperature=1,
224
+ max_tokens=500,
225
+ top_p=1,
226
+ frequency_penalty=0,
227
+ presence_penalty=0
228
+ )
229
+ response = json.loads(openai_response.choices[0].message.content)
230
+ return response
231
+ except Exception as e:
232
+ print(f"OpenAI API Error: {e}")
233
+ return {}
 
 
 
234
 
235
 
236
  @spaces.GPU