Spaces:
Runtime error
Runtime error
Kvikontent
commited on
Commit
•
1a952dd
1
Parent(s):
047e1b6
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,69 @@
|
|
|
|
1 |
import requests
|
|
|
2 |
from io import BytesIO
|
3 |
-
from PIL import Image
|
4 |
-
import gradio as gr
|
5 |
import os
|
|
|
6 |
|
7 |
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 |
class QueryError(Exception):
|
12 |
pass
|
13 |
|
14 |
def query(payload):
|
15 |
try:
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
23 |
|
|
|
|
|
|
|
|
|
24 |
except AssertionError:
|
25 |
print("Invalid Payload Error: Please provide a dictionary.")
|
26 |
-
except
|
27 |
print("Request Failed: ", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
except QueryError as qe:
|
29 |
print(qe)
|
30 |
except Exception as ex:
|
31 |
print("Unknown Error occurred: ", ex)
|
32 |
|
33 |
-
def
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
import requests
|
3 |
+
import io
|
4 |
from io import BytesIO
|
|
|
|
|
5 |
import os
|
6 |
+
from PIL import Image
|
7 |
|
8 |
API_URL = "https://api-inference.huggingface.co/models/Kvikontent/kviimager2.0"
|
9 |
+
api_key = os.environ.get('API_KEY')
|
10 |
headers = {"Authorization": f"Bearer {api_key}"}
|
11 |
|
12 |
+
# Define custom Exception class for better error handling
|
13 |
class QueryError(Exception):
|
14 |
pass
|
15 |
|
16 |
def query(payload):
|
17 |
try:
|
18 |
+
# Make sure we have valid JSON data before sending the request
|
19 |
+
assert type(payload) == dict
|
20 |
+
|
21 |
+
# Send the POST request to the API URL
|
22 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
23 |
+
|
24 |
+
# Check if the status code indicates success (HTTP Status Code 2xx)
|
25 |
+
if not str(response.status_code).startswith("2"):
|
26 |
+
raise QueryError(f"Query failed! Response status code was '{response.status_code}'")
|
27 |
|
28 |
+
else:
|
29 |
+
# Return the raw bytes from the response object
|
30 |
+
return response.content
|
31 |
+
|
32 |
except AssertionError:
|
33 |
print("Invalid Payload Error: Please provide a dictionary.")
|
34 |
+
except RequestException as e:
|
35 |
print("Request Failed: ", e)
|
36 |
+
except ConnectionError as ce:
|
37 |
+
print("Connection Error: Unable to connect to the API.", ce)
|
38 |
+
except Timeout as t:
|
39 |
+
print("Timeout Error: Request timed out while trying to reach the API.", t)
|
40 |
+
except TooManyRedirects as tmr:
|
41 |
+
print("Too Many Redirects Error: Exceeded maximum number of redirects.", tmr)
|
42 |
+
except HTTPError as he:
|
43 |
+
print("HTTP Error: Invalid HTTP response.", he)
|
44 |
except QueryError as qe:
|
45 |
print(qe)
|
46 |
except Exception as ex:
|
47 |
print("Unknown Error occurred: ", ex)
|
48 |
|
49 |
+
def generate_image_from_prompt(prompt_text):
|
50 |
+
image_bytes = query({"inputs": prompt_text})
|
51 |
+
img = BytesIO(image_bytes) # Convert to BytesIO stream
|
52 |
+
pil_img = Image.open(img) # Open the image using PIL library
|
53 |
+
return pil_img # Return the converted PIL image
|
54 |
+
|
55 |
+
title = "KVIImager 2.0 Demo 🎨"
|
56 |
+
description = "This app uses Hugging Face AI model to generate an image based on the provided text prompt 🖼."
|
57 |
+
|
58 |
+
input_prompt = gr.Textbox(label="Enter Prompt 📝", placeholder="E.g. 'A peaceful garden with a small cottage'")
|
59 |
+
output_generated_image = gr.Image(label="Generated Image")
|
60 |
+
|
61 |
+
iface = gr.Interface(
|
62 |
+
fn=generate_image_from_prompt,
|
63 |
+
inputs=input_prompt,
|
64 |
+
outputs=output_generated_image,
|
65 |
+
title=title,
|
66 |
+
description=description,
|
67 |
+
theme="soft"
|
68 |
+
)
|
69 |
+
iface.launch()
|