Hatman commited on
Commit
bbc17d0
·
verified ·
1 Parent(s): 64b1b83

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +15 -6
main.py CHANGED
@@ -286,19 +286,28 @@ def get_random_model(models):
286
  last_two_models = last_two_models[-3:]
287
  return model
288
 
289
- def nsfw_check(attempts = 1):
290
  try:
291
  API_URL = "https://api-inference.huggingface.co/models/Falconsai/nsfw_image_detection"
292
  with open('response.png', 'rb') as f:
293
  data = f.read()
294
  response = requests.request("POST", API_URL, headers=headers, data=data)
295
- print(response.content.decode("utf-8"))
296
- scores = {item['label']: item['score'] for item in json.loads(response.content.decode("utf-8"))}
297
- error_msg = True if scores.get('nsfw') > scores.get('normal') else False
 
 
 
 
 
 
 
 
298
  return error_msg
 
 
 
299
  except Exception as e:
300
- loadTime = json.load(response.content.decode("utf-8"))
301
- time.sleep(loadTime["estimated_time"])
302
  print(f'NSFW Check Error: {e}')
303
  if attempts > 30:
304
  return True
 
286
  last_two_models = last_two_models[-3:]
287
  return model
288
 
289
+ def nsfw_check(attempts=1):
290
  try:
291
  API_URL = "https://api-inference.huggingface.co/models/Falconsai/nsfw_image_detection"
292
  with open('response.png', 'rb') as f:
293
  data = f.read()
294
  response = requests.request("POST", API_URL, headers=headers, data=data)
295
+ decoded_response = response.content.decode("utf-8")
296
+ print(decoded_response)
297
+
298
+ json_response = json.loads(decoded_response)
299
+
300
+ if "error" in json_response:
301
+ time.sleep(json_response["estimated_time"])
302
+ return nsfw_check(attempts+1)
303
+
304
+ scores = {item['label']: item['score'] for item in json_response}
305
+ error_msg = scores.get('nsfw', 0) > scores.get('normal', 0)
306
  return error_msg
307
+ except json.JSONDecodeError as e:
308
+ print(f'JSON Decoding Error: {e}')
309
+ return True
310
  except Exception as e:
 
 
311
  print(f'NSFW Check Error: {e}')
312
  if attempts > 30:
313
  return True