File size: 3,012 Bytes
5e5fd54
72398d0
 
 
9c51543
72398d0
d4bdcdb
31520f0
d4bdcdb
 
e55e709
683915d
f94dc93
c95b55d
9c51543
314a6d7
9c51543
72398d0
c95b55d
72398d0
 
314a6d7
 
 
 
 
 
 
 
 
72398d0
314a6d7
72398d0
 
 
 
febdcdd
c95b55d
d4bdcdb
 
 
9c51543
89dc556
9c51543
314a6d7
9c51543
 
d4bdcdb
febdcdd
9c51543
d4bdcdb
9c51543
 
d4bdcdb
89dc556
d4bdcdb
 
9c51543
d4bdcdb
9c51543
 
 
 
 
 
 
 
 
d4bdcdb
f94dc93
314a6d7
9c51543
 
c95b55d
 
 
 
 
9c51543
c95b55d
4c8d63a
c95b55d
153920b
9c51543
 
72398d0
5e5fd54
9c51543
314a6d7
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
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import requests
from io import BytesIO
from PIL import Image
import os

TOKEN = os.getenv("TOKEN1")
API_URL = os.getenv("API_URL")
token_id = 1
tokens_tried = 0
no_of_accounts = 6
model_id = os.getenv("MODEL_ID")

def get_image_from_url(url):
    """
    Fetches and returns an image from a given URL, converting to PNG if needed.
    """
    try:
        response = requests.get(url, stream=True)
        response.raise_for_status()
        image = Image.open(BytesIO(response.content))
        
        # Convert to PNG if the image is WebP
        if image.format == "WEBP":
            image = image.convert("RGBA")  # Convert to RGBA to handle transparency
            output_buffer = BytesIO()
            image.save(output_buffer, format="PNG")
            output_buffer.seek(0)  # Reset buffer position for reading
            image = Image.open(output_buffer) # Re-open image from buffer
        
        return image

    except requests.exceptions.RequestException as e:
        return f"Error fetching image: {e}"
    except Exception as e:
        return f"Error processing image: {e}"

def generate_image(prompt, aspect_ratio, realism):
    global token_id
    global TOKEN
    global tokens_tried
    global no_of_accounts
    global model_id
    payload = {
        "id": model_id,  
        "inputs": [prompt, aspect_ratio, str(realism).lower()],
    }
    headers = {"Authorization": f"Bearer {TOKEN}"}

    try:
        response_data = requests.post(API_URL, json=payload, headers=headers).json()
        if "error" in response_data:
            if 'error 429' in response_data['error']:
                if tokens_tried < no_of_accounts:
                    token_id = (token_id + 1) % (no_of_accounts+1)
                    tokens_tried += 1
                    TOKEN = os.getenv(f"TOKEN{token_id}")
                    response_data = generate_image(prompt, aspect_ratio, realism)
                    tokens_tried = 0
                    return response_data
                return "No credits available"
            return response_data
        elif "output" in response_data:
            url = response_data['output']
            return get_image_from_url(url)  # Return the image directly
        else:
            return "Error: Unexpected response from server"
    except Exception as e:
        return f"Error"


interface = gr.Interface(
    fn=generate_image,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="Describe the image you want to generate"),
        gr.Radio(
            choices=["1:1", "3:4", "4:3", "9:16", "16:9", "9:21", "21:9"], 
            label="Aspect Ratio", 
            value="16:9"  # Default value
        ),
        gr.Checkbox(label="Realism", value=False),  # Checkbox for realism (True/False)
    ],
    outputs=gr.Image(type="pil"),  # Output image
    title="Image Generator",
    description="Provide a prompt, select an aspect ratio, and set realism to generate an image.",
)

# Launch the interface
interface.launch()