File size: 2,668 Bytes
1a952dd
93a61c7
1a952dd
1fead8b
047e1b6
1a952dd
0a66490
36fe03a
1a952dd
fca1b98
 
1a952dd
f265fa8
 
 
fca1b98
f265fa8
1a952dd
 
 
 
 
 
 
 
 
f265fa8
1a952dd
 
 
 
f265fa8
 
1a952dd
f265fa8
1a952dd
 
 
 
 
 
 
 
f265fa8
 
 
 
fca1b98
1a952dd
f1c4820
1a952dd
d3b342b
 
 
1a952dd
2b5e2b1
2151b1c
1a952dd
2151b1c
1a952dd
2151b1c
d1061a8
 
2151b1c
 
1a952dd
 
 
 
 
 
 
2151b1c
 
1a952dd
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import gradio as gr
import requests
import io
from io import BytesIO
import os
from PIL import Image

API_URL = "https://api-inference.huggingface.co/models/Kvikontent/Bulatnaya-V1"
api_key = os.environ.get('API_KEY')
headers = {"Authorization": f"Bearer {api_key}"}

# Define custom Exception class for better error handling
class QueryError(Exception):
    pass

def query(payload):
    try:
      # Make sure we have valid JSON data before sending the request
      assert type(payload) == dict
      
      # Send the POST request to the API URL
      response = requests.post(API_URL, headers=headers, json=payload)
    
      # Check if the status code indicates success (HTTP Status Code 2xx)
      if not str(response.status_code).startswith("2"):
          raise QueryError(f"Query failed! Response status code was '{response.status_code}'")
        
      else:
           # Return the raw bytes from the response object
            return response.content
            
    except AssertionError:
        print("Invalid Payload Error: Please provide a dictionary.")
    except RequestException as e:
        print("Request Failed: ", e)
    except ConnectionError as ce:
        print("Connection Error: Unable to connect to the API.", ce)
    except Timeout as t:
        print("Timeout Error: Request timed out while trying to reach the API.", t)
    except TooManyRedirects as tmr:
        print("Too Many Redirects Error: Exceeded maximum number of redirects.", tmr)
    except HTTPError as he:
        print("HTTP Error: Invalid HTTP response.", he)
    except QueryError as qe:
        print(qe)
    except Exception as ex:
        print("Unknown Error occurred: ", ex)

def generate_image_from_prompt(prompt_text):
    gr.Info("Image generation started")
    image_bytes = query({"inputs": prompt_text})
    img = BytesIO(image_bytes)   
    pil_img = Image.open(img)   
    return pil_img          

title = "BUlatnaya V1 Demo 🎨"
description = "This app uses Bulatnaya V1 model to generate an image based on the provided text prompt 🖼. Tag 'Bulatnaya' in prompt is required. No nudes accepted!"

input_prompt = gr.Textbox(label="Enter Prompt 📝", placeholder="E.g. 'Bulatnaya in red suit'")
output_generated_image = gr.Image(label="Generated Image")
examples = [
    "Bulatnaya in swimsuit on the beach",
    "Bulatnaya eating chicken in gold restaurant",
    "Bulatnaya screaming at plate"
]

iface = gr.Interface(
    fn=generate_image_from_prompt,
    inputs=input_prompt, 
    outputs=output_generated_image, 
    title=title,
    description=description,
    examples=examples,
    cache_examples=False,
    theme="soft"
)
iface.launch()