Spaces:
Sleeping
Sleeping
Kvikontent
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,11 @@
|
|
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 |
class QueryError(Exception):
|
@@ -14,7 +13,7 @@ class QueryError(Exception):
|
|
14 |
|
15 |
def query(payload):
|
16 |
try:
|
17 |
-
assert
|
18 |
response = requests.post(API_URL, headers=headers, json=payload)
|
19 |
|
20 |
if not str(response.status_code).startswith("2"):
|
@@ -24,16 +23,8 @@ def query(payload):
|
|
24 |
|
25 |
except AssertionError:
|
26 |
print("Invalid Payload Error: Please provide a dictionary.")
|
27 |
-
except RequestException as e:
|
28 |
print("Request Failed: ", e)
|
29 |
-
except ConnectionError as ce:
|
30 |
-
print("Connection Error: Unable to connect to the API.", ce)
|
31 |
-
except Timeout as t:
|
32 |
-
print("Timeout Error: Request timed out while trying to reach the API.", t)
|
33 |
-
except TooManyRedirects as tmr:
|
34 |
-
print("Too Many Redirects Error: Exceeded maximum number of redirects.", tmr)
|
35 |
-
except HTTPError as he:
|
36 |
-
print("HTTP Error: Invalid HTTP response.", he)
|
37 |
except QueryError as qe:
|
38 |
print(qe)
|
39 |
except Exception as ex:
|
@@ -43,25 +34,6 @@ def generate_images_from_prompt(prompt_text, num_images):
|
|
43 |
images = []
|
44 |
for _ in range(num_images):
|
45 |
image_bytes = query({"inputs": prompt_text})
|
46 |
-
img = BytesIO(image_bytes)
|
47 |
-
|
48 |
-
|
49 |
-
return images
|
50 |
-
|
51 |
-
title = "KVIImager 2.0 Demo 🎨"
|
52 |
-
description = "This app uses Hugging Face AI model to generate images based on the provided text prompt 🖼."
|
53 |
-
|
54 |
-
input_prompt = gr.Textbox(label="Enter Prompt 📝", placeholder="E.g. 'A peaceful garden with a small cottage'")
|
55 |
-
input_slider = gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Number of Images per Prompt")
|
56 |
-
|
57 |
-
output_generated_images = gr.Image(label="Generated Image", type="pil")
|
58 |
-
|
59 |
-
iface = gr.Interface(
|
60 |
-
fn=generate_images_from_prompt,
|
61 |
-
inputs=[input_prompt, input_slider],
|
62 |
-
outputs=output_generated_images,
|
63 |
-
title=title,
|
64 |
-
description=description,
|
65 |
-
theme="soft"
|
66 |
-
)
|
67 |
-
iface.launch()
|
|
|
|
|
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', 'YOUR_API_KEY_HERE')
|
9 |
headers = {"Authorization": f"Bearer {api_key}"}
|
10 |
|
11 |
class QueryError(Exception):
|
|
|
13 |
|
14 |
def query(payload):
|
15 |
try:
|
16 |
+
assert isinstance(payload, dict)
|
17 |
response = requests.post(API_URL, headers=headers, json=payload)
|
18 |
|
19 |
if not str(response.status_code).startswith("2"):
|
|
|
23 |
|
24 |
except AssertionError:
|
25 |
print("Invalid Payload Error: Please provide a dictionary.")
|
26 |
+
except requests.exceptions.RequestException as e:
|
27 |
print("Request Failed: ", e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
except QueryError as qe:
|
29 |
print(qe)
|
30 |
except Exception as ex:
|
|
|
34 |
images = []
|
35 |
for _ in range(num_images):
|
36 |
image_bytes = query({"inputs": prompt_text})
|
37 |
+
img = Image.open(BytesIO(image_bytes))
|
38 |
+
images.append(img)
|
39 |
+
return images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|