Kvikontent commited on
Commit
f265fa8
1 Parent(s): a70a8f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -2
app.py CHANGED
@@ -8,9 +8,42 @@ API_URL = "https://api-inference.huggingface.co/models/Kvikontent/kviimager2.0"
8
  api_key = os.environ.get('API_KEY')
9
  headers = {"Authorization": f"Bearer {api_key}"}
10
 
 
 
 
 
11
  def query(payload):
12
- response = requests.post(API_URL, headers=headers, json=payload)
13
- return response.content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  def generate_image_from_prompt(prompt_text):
16
  image_bytes = query({"inputs": prompt_text})
 
8
  api_key = os.environ.get('API_KEY')
9
  headers = {"Authorization": f"Bearer {api_key}"}
10
 
11
+ # Define custom Exception class for better error handling
12
+ class QueryError(Exception):
13
+ pass
14
+
15
  def query(payload):
16
+ try:
17
+ # Make sure we have valid JSON data before sending the request
18
+ assert type(payload) == dict
19
+
20
+ # Send the POST request to the API URL
21
+ response = requests.post(API_URL, headers=headers, json=payload)
22
+
23
+ # Check if the status code indicates success (HTTP Status Code 2xx)
24
+ if not str(response.status_code).startswith("2"):
25
+ raise QueryError(f"Query failed! Response status code was '{response.status_code}'")
26
+
27
+ else:
28
+ # Return the raw bytes from the response object
29
+ return response.content
30
+
31
+ except AssertionError:
32
+ print("Invalid Payload Error: Please provide a dictionary.")
33
+ except RequestException as e:
34
+ print("Request Failed: ", e)
35
+ except ConnectionError as ce:
36
+ print("Connection Error: Unable to connect to the API.", ce)
37
+ except Timeout as t:
38
+ print("Timeout Error: Request timed out while trying to reach the API.", t)
39
+ except TooManyRedirects as tmr:
40
+ print("Too Many Redirects Error: Exceeded maximum number of redirects.", tmr)
41
+ except HTTPError as he:
42
+ print("HTTP Error: Invalid HTTP response.", he)
43
+ except QueryError as qe:
44
+ print(qe)
45
+ except Exception as ex:
46
+ print("Unknown Error occurred: ", ex)
47
 
48
  def generate_image_from_prompt(prompt_text):
49
  image_bytes = query({"inputs": prompt_text})