Ashrafb commited on
Commit
10e3b88
1 Parent(s): cf47e36

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +11 -6
main.py CHANGED
@@ -64,23 +64,28 @@ def send_it1(inputs, noise_level, proc=proc1):
64
  output = proc(prompt_with_noise)
65
  return output
66
 
67
- def generate_image(prompt_with_noise):
68
  try:
69
  global request_counter
70
  request_counter += 1
71
  timestamp = f"{time.time()}_{request_counter}"
72
- prompt_with_noise = add_random_noise(inputs, noise_level) + f" - {timestamp}"
73
  payload = {"inputs": prompt_with_noise}
74
  response = requests.post(API_URL, headers=headers, json=payload)
75
  response.raise_for_status() # Raise an exception for HTTP errors
76
  image_bytes = response.content
77
  image = Image.open(io.BytesIO(image_bytes))
78
  return image
 
 
 
 
79
  except Exception as e:
80
- # Handle the error gracefully, such as logging the error or returning a default image
81
- raise gr.Error(f"Error generating image: {e}")
82
- return "Experiencing high demand. Please retry shortly. Thank you for your patience" # Return None or a default image in case of error
83
-
 
84
 
85
  def get_prompts(prompt_text):
86
  if not prompt_text:
 
64
  output = proc(prompt_with_noise)
65
  return output
66
 
67
+ def generate_image(inputs):
68
  try:
69
  global request_counter
70
  request_counter += 1
71
  timestamp = f"{time.time()}_{request_counter}"
72
+ prompt_with_noise = add_random_noise(inputs) + f" - {timestamp}"
73
  payload = {"inputs": prompt_with_noise}
74
  response = requests.post(API_URL, headers=headers, json=payload)
75
  response.raise_for_status() # Raise an exception for HTTP errors
76
  image_bytes = response.content
77
  image = Image.open(io.BytesIO(image_bytes))
78
  return image
79
+ except requests.exceptions.HTTPError as e:
80
+ # Handle any HTTP errors
81
+ print(f"HTTP Error: {e}")
82
+ return "An unexpected error occurred while generating the image."
83
  except Exception as e:
84
+ # Handle other exceptions
85
+ print(f"Error generating image: {e}")
86
+ return "An unexpected error occurred while generating the image. Please try again later."
87
+
88
+
89
 
90
  def get_prompts(prompt_text):
91
  if not prompt_text: