File size: 7,599 Bytes
d8f4b35
8254cea
 
 
 
8210209
a61e6ef
7975a1a
d8f4b35
8210209
7975a1a
8210209
 
 
 
 
7975a1a
8210209
 
 
7975a1a
8210209
 
7975a1a
8210209
 
 
 
 
 
 
 
7975a1a
8210209
 
 
 
 
 
 
a61e6ef
8210209
 
d8f4b35
8210209
a61e6ef
8210209
 
 
 
7975a1a
 
a61e6ef
8210209
a61e6ef
8210209
a61e6ef
8210209
 
 
 
7975a1a
8210209
7975a1a
8210209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7975a1a
8210209
 
 
 
 
 
7975a1a
8210209
7975a1a
8210209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a61e6ef
8210209
ab5cbcb
8210209
 
 
 
a61e6ef
ab5cbcb
8210209
 
 
 
7975a1a
8210209
d8f4b35
8210209
 
 
 
 
 
 
8254cea
d8f4b35
8210209
 
 
 
 
 
 
 
8254cea
ab5cbcb
 
8210209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8254cea
8210209
 
 
 
 
 
 
 
 
 
 
7975a1a
8210209
 
 
 
 
d8f4b35
8210209
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import gradio as gr
import requests
from PIL import Image
from io import BytesIO
import urllib.parse
import base64
import json
import time

# ==================== WORKING FREE APIs ====================

def craiyon_generate(prompt, width=1024, height=1024, seed=0):
    """
    Craiyon (formerly DALL-E mini) - Unlimited, no signup required
    Community-maintained API endpoint
    """
    try:
        # Craiyon API endpoint (unofficial but working)
        url = "https://backend.craiyon.com/generate"
        
        payload = {
            "prompt": prompt,
            "model": "photo"  # Options: art, photo, drawing, none
        }
        
        headers = {
            "Content-Type": "application/json",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
        }
        
        response = requests.post(url, json=payload, headers=headers, timeout=60)
        
        if response.status_code == 200:
            data = response.json()
            if "images" in data and len(data["images"]) > 0:
                # Craiyon returns base64 images
                img_data = base64.b64decode(data["images"][0])
                return Image.open(BytesIO(img_data)), "Craiyon (Unlimited)"
            else:
                return None, "Craiyon: No images in response"
        else:
            return None, f"Craiyon: HTTP {response.status_code}"
            
    except Exception as e:
        return None, f"Craiyon Failed: {str(e)}"

def pollinations_generate(prompt, width=1024, height=1024, seed=0):
    """
    Pollinations.ai - Free, no signup, but occasionally down
    """
    try:
        encoded_prompt = urllib.parse.quote(prompt)
        url = f"https://image.pollinations.ai/prompt/{encoded_prompt}?width={width}&height={height}&nologo=true&seed={seed if seed > 0 else int(time.time())}"
        
        response = requests.get(url, timeout=30)
        
        if response.status_code == 200:
            return Image.open(BytesIO(response.content)), "Pollinations.ai"
        else:
            return None, f"Pollinations: HTTP {response.status_code}"
            
    except Exception as e:
        return None, f"Pollinations Failed: {str(e)}"

def deepai_generate(prompt, width=1024, height=1024, seed=0):
    """
    DeepAI - No signup, stable, but lower quality
    """
    try:
        url = "https://api.deepai.org/api/text2img"
        
        # DeepAI allows a demo key for limited free use
        headers = {
            'api-key': 'tryit-demo-key',
        }
        
        data = {
            'text': prompt,
        }
        
        response = requests.post(url, headers=headers, data=data, timeout=40)
        
        if response.status_code == 200:
            result = response.json()
            if 'output_url' in result:
                img_response = requests.get(result['output_url'], timeout=20)
                if img_response.status_code == 200:
                    return Image.open(BytesIO(img_response.content)), "DeepAI"
        return None, f"DeepAI: HTTP {response.status_code}"
        
    except Exception as e:
        return None, f"DeepAI Failed: {str(e)}"

# ==================== MAIN GENERATION LOGIC ====================

def generate_with_fallback(prompt, width=1024, height=1024, seed=0, primary="Craiyon (Unlimited)"):
    """
    Try multiple free services in order
    """
    if not prompt:
        return None, "⚠️ Please enter a prompt!"
    
    services = {
        "Craiyon (Unlimited)": craiyon_generate,
        "Pollinations.ai": pollinations_generate,
        "DeepAI (Stable)": deepai_generate,
    }
    
    errors = []
    
    # Try primary first
    if primary in services:
        try:
            print(f"πŸ”„ Trying {primary}...")
            img, status = services[primary](prompt, width, height, seed)
            if img:
                return img, f"βœ… Success! ({status})"
            else:
                errors.append(f"{primary}: {status}")
        except Exception as e:
            errors.append(f"{primary}: {str(e)}")
    
    # Try others as fallback
    for name, func in services.items():
        if name == primary:
            continue
        try:
            print(f"πŸ”„ Trying fallback: {name}...")
            img, status = func(prompt, width, height, seed)
            if img:
                return img, f"βœ… Success! ({status} - Fallback)"
            else:
                errors.append(f"{name}: {status}")
        except Exception as e:
            errors.append(f"{name}: {str(e)}")
    
    # All failed
    error_msg = "❌ All services failed:\\n" + "\\n".join(errors)
    return None, error_msg

# ==================== GRADIO UI ====================

with gr.Blocks(title="Free Image Generator") as demo:
    gr.Markdown("""
    # 🎨 Free Image Generator (Working Edition)
    **No API Keys β€’ Multiple Providers β€’ Craiyon + Pollinations + DeepAI**
    
    Made with ❀️ by Srijan Gajurel
    """)
    
    with gr.Row():
        with gr.Column(scale=1):
            prompt = gr.Textbox(
                label="πŸ“ Prompt",
                lines=3,
                placeholder="Describe your image...",
                value="A beautiful sunset over mountains, cinematic lighting"
            )
            
            with gr.Row():
                width = gr.Slider(512, 1024, 1024, step=64, label="Width")
                height = gr.Slider(512, 1024, 1024, step=64, label="Height")
            
            seed = gr.Number(0, label="🎲 Seed (0 for random)", precision=0)
            
            primary_service = gr.Radio(
                choices=["Craiyon (Unlimited)", "Pollinations.ai", "DeepAI (Stable)"],
                value="Craiyon (Unlimited)",
                label="πŸš€ Primary Service"
            )
            
            with gr.Accordion("ℹ️ Service Info", open=False):
                gr.Markdown("""
                **Craiyon (Recommended)**
                - βœ… Unlimited generations
                - βœ… No signup required
                - ⚠️ 1024x1024 only (other sizes ignored)
                - ⚠️ Lower quality than premium models
                
                **Pollinations.ai**
                - βœ… High quality
                - βœ… Custom sizes supported
                - ⚠️ Occasionally down for maintenance
                
                **DeepAI**
                - βœ… Stable and reliable
                - ⚠️ Lower quality
                - ⚠️ Slower generation
                """)
            
            btn = gr.Button("🎨 Generate Image", variant="primary")
            status = gr.Textbox(label="Status", value="Ready")
        
        with gr.Column(scale=1):
            output_img = gr.Image(label="Generated Image", type="pil", height=600)
    
    examples = gr.Examples(
        examples=[
            ["A futuristic cyberpunk city at night, neon lights", 1024, 1024, 0, "Craiyon (Unlimited)"],
            ["Portrait of a cat wearing a crown, oil painting", 1024, 1024, 42, "Pollinations.ai"],
            ["Space nebula with vibrant colors and stars", 1024, 1024, 0, "DeepAI (Stable)"],
        ],
        inputs=[prompt, width, height, seed, primary_service]
    )
    
    btn.click(
        fn=generate_with_fallback,
        inputs=[prompt, width, height, seed, primary_service],
        outputs=[output_img, status]
    )

if __name__ == "__main__":
    print("πŸš€ Starting Free Image Generator...")
    print("πŸ› οΈ Services: Craiyon (Primary), Pollinations (Backup), DeepAI (Backup)")
    demo.launch()