File size: 2,101 Bytes
cb691b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import base64
from PIL import Image
import io
import os

API_KEY = os.environ.get("API_KEY")
API_URL = "https://api.4llm.com/v1/images/generations"

def generate_image(prompt, size="1024x1024"):
    headers = {
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    }
    
    data = {
        "model": "4llm-image",
        "prompt": prompt,
        "n": 1,
        "size": size
    }
    
    try:
        response = requests.post(API_URL, headers=headers, json=data)
        response.raise_for_status()
        result = response.json()
        
        # Get the base64 image data
        image_data = result["data"][0]["url"].split(",")[1]
        image_bytes = base64.b64decode(image_data)
        
        # Convert to PIL Image
        image = Image.open(io.BytesIO(image_bytes))
        return image, result["data"][0]["revised_prompt"]
    except Exception as e:
        return None, f"Error: {str(e)}"

# Create the Gradio interface
with gr.Blocks(title="4LLM Image Generation") as demo:
    gr.Markdown("# 4LLM Image Generation Demo")
    gr.Markdown("Generate images using the 4LLM API with no rate limits!")
    
    with gr.Row():
        with gr.Column():
            prompt = gr.Textbox(
                label="Prompt",
                placeholder="Describe the image you want to generate...",
                lines=3
            )
            size = gr.Dropdown(
                choices=["1024x1024", "512x512", "768x768"],
                value="1024x1024",
                label="Image Size"
            )
            generate_btn = gr.Button("Generate Image")
        
        with gr.Column():
            output_image = gr.Image(label="Generated Image")
            revised_prompt = gr.Textbox(
                label="Revised Prompt",
                lines=3,
                interactive=False
            )
    
    generate_btn.click(
        fn=generate_image,
        inputs=[prompt, size],
        outputs=[output_image, revised_prompt]
    )

if __name__ == "__main__":
    demo.launch(share=False)