File size: 2,684 Bytes
c7f3c86
864f159
 
 
 
 
 
 
c7f3c86
 
864f159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import aiohttp
import asyncio
import random
import io
from PIL import Image
import os
from concurrent.futures import ThreadPoolExecutor


markdown_link = "For more generations, visit [Imagine Art](https://www.imagine.art/)."

TOKEN = os.getenv("TOKEN")
URL = os.getenv("URL")
print(f"URL: {URL}, Type: {type(URL)}")

if not URL:
    raise ValueError("URL environment variable is not set.")

async def async_generation_request(request_data, is_user_premium):
    seed = request_data.get("seed", random.randint(1, 10000000))

    headers = {
        "bearer": TOKEN,
        "Style-Id": request_data["model_id"]
    }
    if is_user_premium:
        headers["Premium"] = "True"

    for key, value in headers.items():
        if value is None:
            raise ValueError(f"Header value is missing for key: {key}")

    async with aiohttp.ClientSession() as session:
        for i in range(3):
            data = aiohttp.FormData()
            data.add_field('prompt', request_data["prompt"], content_type='multipart/form-data')
            data.add_field('style_id', request_data["model_id"])
            data.add_field('seed', str(seed))
            data.add_field('aspect_ratio', request_data["aspect_ratio"])
            data.add_field('negative_prompt', request_data["negative_prompt"])
            data.add_field('cfg', str(request_data["cfg_scale"]))

            async with session.post(URL, data=data, headers=headers) as response:
                if response.status == 200:
                    return await response.read()
                print(f"Failed API Call: {response.status} >>> {await response.text()}")

    return None

def run_async_in_thread(func, *args):
    with ThreadPoolExecutor() as executor:
        return executor.submit(asyncio.run, func(*args)).result()

def gradio_generation_request(prompt, aspect_ratio, negative_prompt, cfg_scale):
    request_data = {
        "prompt": prompt,
        "model_id": "32",
        "aspect_ratio": aspect_ratio,
        "negative_prompt": negative_prompt,
        "cfg_scale": cfg_scale,
        "seed": random.randint(1, 10000000)
    }

    output = run_async_in_thread(async_generation_request, request_data, True)  

    if output:
        return Image.open(io.BytesIO(output))
    else:
        return None

interface = gr.Interface(
    fn=gradio_generation_request,
    inputs=[
        gr.Textbox(label="Prompt"),
        gr.Textbox(label="Aspect Ratio"),
        gr.Textbox(label="Negative Prompt"),
        gr.Slider(minimum=1, maximum=15, value=7.5, label="CFG Scale") 
    ],
    outputs="image",
    title='IMAGINE V4.1 GENERATION',
    description=markdown_link
)

interface.launch()