Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ from PIL import Image
|
|
| 9 |
from gradio_client import Client
|
| 10 |
|
| 11 |
# ==========================================
|
| 12 |
-
# 1. API Keys
|
| 13 |
# ==========================================
|
| 14 |
KEYS = {
|
| 15 |
"deepai": "16e69995-00d4-4ffd-993c-a026632f6de6",
|
|
@@ -27,181 +27,147 @@ KEYS = {
|
|
| 27 |
}
|
| 28 |
|
| 29 |
# ==========================================
|
| 30 |
-
# 2. API Functions (
|
| 31 |
# ==========================================
|
| 32 |
def get_image_from_url(url):
|
| 33 |
-
|
| 34 |
-
return Image.open(BytesIO(response.content))
|
| 35 |
|
| 36 |
-
def generate_stability(
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
headers={"Authorization": f"Bearer {KEYS['stability']}", "Accept": "image/*"},
|
| 40 |
-
files={"none": ''},
|
| 41 |
-
data={"prompt": prompt, "output_format": "jpeg", "aspect_ratio": ar}
|
| 42 |
-
)
|
| 43 |
-
if response.status_code == 200: return Image.open(BytesIO(response.content))
|
| 44 |
raise Exception("Error")
|
| 45 |
|
| 46 |
-
def generate_getimg(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
headers={"Authorization": f"Bearer {KEYS['getimg']}"},
|
| 50 |
-
json={"prompt": prompt, "width": w, "height": h}
|
| 51 |
-
)
|
| 52 |
-
if response.status_code == 200: return Image.open(BytesIO(base64.b64decode(response.json()["image"])))
|
| 53 |
raise Exception("Error")
|
| 54 |
|
| 55 |
-
def generate_segmind(
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
headers={"x-api-key": KEYS['segmind']},
|
| 59 |
-
json={"prompt": prompt, "samples": 1, "width": w, "height": h}
|
| 60 |
-
)
|
| 61 |
-
if response.status_code == 200: return Image.open(BytesIO(response.content))
|
| 62 |
raise Exception("Error")
|
| 63 |
|
| 64 |
-
def generate_fireworks(
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
headers={"Authorization": f"Bearer {KEYS['fireworks']}"},
|
| 68 |
-
json={"text_prompts": [{"text": prompt}], "width": w, "height": h}
|
| 69 |
-
)
|
| 70 |
-
if response.status_code == 200: return Image.open(BytesIO(base64.b64decode(response.json()[0]["base64"])))
|
| 71 |
raise Exception("Error")
|
| 72 |
|
| 73 |
-
def
|
| 74 |
-
|
| 75 |
-
if
|
| 76 |
raise Exception("Error")
|
| 77 |
|
| 78 |
-
def
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
json={"instances": [{"prompt": prompt}]}
|
| 82 |
-
)
|
| 83 |
-
if response.status_code == 200: return Image.open(BytesIO(base64.b64decode(response.json()['predictions'][0]['bytesBase64Encoded'])))
|
| 84 |
raise Exception("Error")
|
| 85 |
|
| 86 |
-
def generate_edenai(
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
headers={"Authorization": f"Bearer {KEYS['edenai']}"},
|
| 90 |
-
json={"providers": "openai", "text": prompt, "resolution": "1024x1024"}
|
| 91 |
-
)
|
| 92 |
-
if response.status_code == 200: return get_image_from_url(response.json()['openai']['items'][0]['image_resource_url'])
|
| 93 |
raise Exception("Error")
|
| 94 |
|
| 95 |
-
def generate_modelslab(
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
json={"key": KEYS['modelslab'], "prompt": prompt, "width": str(w), "height": str(h)}
|
| 99 |
-
)
|
| 100 |
-
if response.status_code == 200 and "output" in response.json(): return get_image_from_url(response.json()["output"][0])
|
| 101 |
raise Exception("Error")
|
| 102 |
|
| 103 |
-
def generate_openrouter(
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
)
|
| 109 |
-
if response.status_code == 200:
|
| 110 |
-
content = response.json()['choices'][0]['message']['content']
|
| 111 |
-
if "http" in content:
|
| 112 |
-
url = content.split("(")[-1].split(")")[0] if "(" in content else content.strip()
|
| 113 |
-
return get_image_from_url(url)
|
| 114 |
raise Exception("Error")
|
| 115 |
|
| 116 |
-
def
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
json={"prompt": prompt, "params": {"n": 1, "width": 512, "height": 512}}
|
| 121 |
-
)
|
| 122 |
-
if response.status_code == 202:
|
| 123 |
-
job_id = response.json()['id']
|
| 124 |
-
for _ in range(15):
|
| 125 |
-
time.sleep(2)
|
| 126 |
-
check = requests.get(f"https://stablehorde.net/api/v2/generate/status/{job_id}")
|
| 127 |
-
if check.status_code == 200 and check.json().get("done", False):
|
| 128 |
-
return get_image_from_url(check.json()['generations'][0]['img'])
|
| 129 |
raise Exception("Error")
|
| 130 |
|
| 131 |
# ==========================================
|
| 132 |
-
# 3.
|
| 133 |
# ==========================================
|
| 134 |
-
def
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
| 141 |
raise Exception("Error")
|
| 142 |
|
| 143 |
-
def
|
| 144 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
url = f"https://api-inference.huggingface.co/models/{model_id}"
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
raise Exception("Error")
|
| 150 |
|
| 151 |
-
# HF Public Spaces (আগেরগুলো)
|
| 152 |
-
def generate_hf_flux(prompt, w, h, ar):
|
| 153 |
-
client = Client("black-forest-labs/FLUX.1-schnell")
|
| 154 |
-
result = client.predict(prompt=prompt, seed=0, randomize_seed=True, width=1024, height=1024, num_inference_steps=4, api_name="/infer")
|
| 155 |
-
return Image.open(result[0])
|
| 156 |
-
|
| 157 |
-
def generate_hf_sdxl_lightning(prompt, w, h, ar):
|
| 158 |
-
client = Client("ByteDance/SDXL-Lightning")
|
| 159 |
-
result = client.predict(prompt, "4-Step", api_name="/generate_image")
|
| 160 |
-
return Image.open(result)
|
| 161 |
-
|
| 162 |
-
def generate_hf_sd3(prompt, w, h, ar):
|
| 163 |
-
client = Client("stabilityai/stable-diffusion-3.5-large")
|
| 164 |
-
result = client.predict(prompt=prompt, negative_prompt="", seed=0, randomize_seed=True, width=1024, height=1024, guidance_scale=4.5, num_inference_steps=40, api_name="/infer")
|
| 165 |
-
return Image.open(result[0])
|
| 166 |
-
|
| 167 |
# ==========================================
|
| 168 |
-
# 4.
|
| 169 |
# ==========================================
|
| 170 |
-
|
| 171 |
-
"Pollinations AI (Unlimited)": generate_pollinations, # আনলিমিটেড ফ্রি
|
| 172 |
-
"Stability AI": generate_stability,
|
| 173 |
"Google Imagen": generate_google_imagen,
|
|
|
|
| 174 |
"GetImg AI": generate_getimg,
|
| 175 |
"Segmind": generate_segmind,
|
| 176 |
"Fireworks": generate_fireworks,
|
|
|
|
| 177 |
"OpenRouter": generate_openrouter,
|
| 178 |
"DeepAI": generate_deepai,
|
| 179 |
"Eden AI": generate_edenai,
|
| 180 |
-
"ModelsLab": generate_modelslab
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
"HF API: Playground v2.5": lambda p, w, h, ar: generate_hf_api(p, "playgroundai/playground-v2.5-1024px-aesthetic"),
|
| 185 |
"Stable Horde (Crowdsourced)": generate_stable_horde,
|
| 186 |
-
"HF Space: FLUX.1 (
|
| 187 |
-
"HF Space: SDXL Light (
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
}
|
| 190 |
|
|
|
|
|
|
|
|
|
|
| 191 |
# ==========================================
|
| 192 |
-
# 5. Generation Logic (
|
| 193 |
# ==========================================
|
| 194 |
-
def generate_image_logic(prompt, style, size, selected_api):
|
| 195 |
if not prompt.strip():
|
| 196 |
yield None, "⚠️ **দয়া করে একটি প্রম্পট লিখুন!**"
|
| 197 |
return
|
| 198 |
|
| 199 |
-
if size == "ল্যান্ডস্কেপ (16:9) - YouTube":
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
w, h, ar = 576, 1024, "9:16"
|
| 203 |
-
else:
|
| 204 |
-
w, h, ar = 1024, 1024, "1:1"
|
| 205 |
|
| 206 |
style_modifiers = {
|
| 207 |
"ফটোরিয়ালিস্টিক (Photorealistic)": ", ultra realistic, highly detailed, photorealistic, 8k resolution, raw photo, masterpiece, soft cinematic lighting",
|
|
@@ -211,43 +177,42 @@ def generate_image_logic(prompt, style, size, selected_api):
|
|
| 211 |
}
|
| 212 |
enhanced_prompt = f"{prompt.strip()}{style_modifiers.get(style, '')}, aspect ratio {ar}"
|
| 213 |
|
| 214 |
-
# অটোমেটিক মোড
|
| 215 |
if selected_api == "অটোমেটিক (Auto Fallback)":
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
| 219 |
try:
|
| 220 |
image = api_func(enhanced_prompt, w, h, ar)
|
| 221 |
if image:
|
| 222 |
-
yield image, f"✅ **সফল!**
|
| 223 |
return
|
| 224 |
except Exception as e:
|
| 225 |
-
yield None, f"❌ **{api_name}**
|
| 226 |
continue
|
| 227 |
-
yield None, "🚫 **দুঃখিত! সবগুলো সার্ভার ব্যস্ত
|
| 228 |
|
| 229 |
-
# ম্যানুয়াল
|
| 230 |
else:
|
| 231 |
-
yield None, f"⏳ শুধুমাত্র **{selected_api}**-এ
|
| 232 |
-
api_func =
|
| 233 |
try:
|
| 234 |
image = api_func(enhanced_prompt, w, h, ar)
|
| 235 |
-
if image:
|
| 236 |
-
|
| 237 |
-
else:
|
| 238 |
-
yield None, f"❌ **দুঃখিত!** {selected_api} বর্তমানে ব্যস্ত বা ডাউন। অন্য একটি সিলেক্ট করুন।"
|
| 239 |
except Exception as e:
|
| 240 |
-
yield None, f"❌ **দুঃখিত!** {selected_api} এর সার্ভার ফুল অথবা লিমিট শেষ। অন্য একটি
|
| 241 |
|
| 242 |
# ==========================================
|
| 243 |
# 6. Gradio Web UI
|
| 244 |
# ==========================================
|
| 245 |
-
api_choices = ["অ
|
| 246 |
|
| 247 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 248 |
-
gr.Markdown("<h1 style='text-align: center;'>🎨 Pro AI Image Generator (
|
| 249 |
-
|
| 250 |
-
|
| 251 |
with gr.Row():
|
| 252 |
with gr.Column(scale=1):
|
| 253 |
prompt_input = gr.Textbox(label="ইমেজের বর্ণনা লিখুন (Prompt)", placeholder="A highly detailed futuristic city...", lines=3)
|
|
@@ -255,21 +220,21 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 255 |
with gr.Row():
|
| 256 |
style_input = gr.Dropdown(
|
| 257 |
choices=["ফটোরিয়ালিস্টিক (Photorealistic)", "এনিমে (Anime)", "থ্রিডি আর্ট (3D)", "ডিফল্ট (Default)"],
|
| 258 |
-
label="ছবির স্টাইল (Style)",
|
| 259 |
-
value="ফটোরিয়ালিস্টিক (Photorealistic)"
|
| 260 |
)
|
| 261 |
-
|
| 262 |
size_input = gr.Dropdown(
|
| 263 |
choices=["স্কয়ার (1:1) - Instagram", "ল্যান্ডস্কেপ (16:9) - YouTube", "পোর্ট্রেট (9:16) - TikTok"],
|
| 264 |
-
label="ছবির সাইজ (Size)",
|
| 265 |
-
value="স্কয়ার (1:1) - Instagram"
|
| 266 |
)
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
api_selector = gr.Dropdown(
|
| 269 |
-
choices=api_choices,
|
| 270 |
-
label="সার্ভার / API সিলেক্ট করুন",
|
| 271 |
-
value="অটোমেটিক (Auto Fallback)",
|
| 272 |
-
interactive=True
|
| 273 |
)
|
| 274 |
|
| 275 |
generate_btn = gr.Button("Generate Image", variant="primary")
|
|
@@ -280,7 +245,7 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 280 |
|
| 281 |
generate_btn.click(
|
| 282 |
fn=generate_image_logic,
|
| 283 |
-
inputs=[prompt_input, style_input, size_input, api_selector],
|
| 284 |
outputs=[image_output, log_box]
|
| 285 |
)
|
| 286 |
|
|
|
|
| 9 |
from gradio_client import Client
|
| 10 |
|
| 11 |
# ==========================================
|
| 12 |
+
# 1. API Keys
|
| 13 |
# ==========================================
|
| 14 |
KEYS = {
|
| 15 |
"deepai": "16e69995-00d4-4ffd-993c-a026632f6de6",
|
|
|
|
| 27 |
}
|
| 28 |
|
| 29 |
# ==========================================
|
| 30 |
+
# 2. Private API Functions (FAST)
|
| 31 |
# ==========================================
|
| 32 |
def get_image_from_url(url):
|
| 33 |
+
return Image.open(BytesIO(requests.get(url).content))
|
|
|
|
| 34 |
|
| 35 |
+
def generate_stability(p, w, h, ar):
|
| 36 |
+
res = requests.post("https://api.stability.ai/v2beta/stable-image/generate/core", headers={"Authorization": f"Bearer {KEYS['stability']}", "Accept": "image/*"}, files={"none": ''}, data={"prompt": p, "output_format": "jpeg", "aspect_ratio": ar})
|
| 37 |
+
if res.status_code == 200: return Image.open(BytesIO(res.content))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
raise Exception("Error")
|
| 39 |
|
| 40 |
+
def generate_getimg(p, w, h, ar):
|
| 41 |
+
res = requests.post("https://api.getimg.ai/v1/essential/text-to-image", headers={"Authorization": f"Bearer {KEYS['getimg']}"}, json={"prompt": p, "width": w, "height": h})
|
| 42 |
+
if res.status_code == 200: return Image.open(BytesIO(base64.b64decode(res.json()["image"])))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
raise Exception("Error")
|
| 44 |
|
| 45 |
+
def generate_segmind(p, w, h, ar):
|
| 46 |
+
res = requests.post("https://api.segmind.com/v1/sdxl1.0-txt2img", headers={"x-api-key": KEYS['segmind']}, json={"prompt": p, "samples": 1, "width": w, "height": h})
|
| 47 |
+
if res.status_code == 200: return Image.open(BytesIO(res.content))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
raise Exception("Error")
|
| 49 |
|
| 50 |
+
def generate_fireworks(p, w, h, ar):
|
| 51 |
+
res = requests.post("https://api.fireworks.ai/inference/v1/image_generation/accounts/fireworks/models/stable-diffusion-xl-1024-v1-0", headers={"Authorization": f"Bearer {KEYS['fireworks']}"}, json={"text_prompts": [{"text": p}], "width": w, "height": h})
|
| 52 |
+
if res.status_code == 200: return Image.open(BytesIO(base64.b64decode(res.json()[0]["base64"])))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
raise Exception("Error")
|
| 54 |
|
| 55 |
+
def generate_google_imagen(p, w, h, ar):
|
| 56 |
+
res = requests.post(f"https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate-001:predict?key={KEYS['google']}", json={"instances": [{"prompt": p}]})
|
| 57 |
+
if res.status_code == 200: return Image.open(BytesIO(base64.b64decode(res.json()['predictions'][0]['bytesBase64Encoded'])))
|
| 58 |
raise Exception("Error")
|
| 59 |
|
| 60 |
+
def generate_deepai(p, w, h, ar):
|
| 61 |
+
res = requests.post("https://api.deepai.org/api/text2img", data={'text': p}, headers={'api-key': KEYS['deepai']})
|
| 62 |
+
if res.status_code == 200: return get_image_from_url(res.json()['output_url'])
|
|
|
|
|
|
|
|
|
|
| 63 |
raise Exception("Error")
|
| 64 |
|
| 65 |
+
def generate_edenai(p, w, h, ar):
|
| 66 |
+
res = requests.post("https://api.edenai.run/v2/image/generation", headers={"Authorization": f"Bearer {KEYS['edenai']}"}, json={"providers": "openai", "text": p, "resolution": "1024x1024"})
|
| 67 |
+
if res.status_code == 200: return get_image_from_url(res.json()['openai']['items'][0]['image_resource_url'])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
raise Exception("Error")
|
| 69 |
|
| 70 |
+
def generate_modelslab(p, w, h, ar):
|
| 71 |
+
res = requests.post("https://modelslab.com/api/v6/images/text2img", json={"key": KEYS['modelslab'], "prompt": p, "width": str(w), "height": str(h)})
|
| 72 |
+
if res.status_code == 200 and "output" in res.json(): return get_image_from_url(res.json()["output"][0])
|
|
|
|
|
|
|
|
|
|
| 73 |
raise Exception("Error")
|
| 74 |
|
| 75 |
+
def generate_openrouter(p, w, h, ar):
|
| 76 |
+
res = requests.post("https://openrouter.ai/api/v1/chat/completions", headers={"Authorization": f"Bearer {KEYS['openrouter']}"}, json={"model": "google/imagen-3-fast-generate", "messages": [{"role": "user", "content": p}]})
|
| 77 |
+
if res.status_code == 200:
|
| 78 |
+
content = res.json()['choices'][0]['message']['content']
|
| 79 |
+
if "http" in content: return get_image_from_url(content.split("(")[-1].split(")")[0] if "(" in content else content.strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
raise Exception("Error")
|
| 81 |
|
| 82 |
+
def generate_pollinations(p, w, h, ar):
|
| 83 |
+
encoded = urllib.parse.quote(p)
|
| 84 |
+
res = requests.get(f"https://image.pollinations.ai/prompt/{encoded}?width={w}&height={h}&nologo=true")
|
| 85 |
+
if res.status_code == 200: return Image.open(BytesIO(res.content))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
raise Exception("Error")
|
| 87 |
|
| 88 |
# ==========================================
|
| 89 |
+
# 3. HF & Slow API Functions (SLOW)
|
| 90 |
# ==========================================
|
| 91 |
+
def generate_stable_horde(p, w, h, ar):
|
| 92 |
+
res = requests.post("https://stablehorde.net/api/v2/generate/async", headers={"apikey": KEYS['stablehorde']}, json={"prompt": p, "params": {"n": 1, "width": 512, "height": 512}})
|
| 93 |
+
if res.status_code == 202:
|
| 94 |
+
job_id = res.json()['id']
|
| 95 |
+
for _ in range(15):
|
| 96 |
+
time.sleep(2)
|
| 97 |
+
check = requests.get(f"https://stablehorde.net/api/v2/generate/status/{job_id}")
|
| 98 |
+
if check.status_code == 200 and check.json().get("done", False): return get_image_from_url(check.json()['generations'][0]['img'])
|
| 99 |
raise Exception("Error")
|
| 100 |
|
| 101 |
+
def generate_hf_space(client_name, api_name, p):
|
| 102 |
+
try:
|
| 103 |
+
client = Client(client_name)
|
| 104 |
+
result = client.predict(p, api_name=api_name)
|
| 105 |
+
return Image.open(result[0] if isinstance(result, (list, tuple)) else result)
|
| 106 |
+
except:
|
| 107 |
+
raise Exception("Error")
|
| 108 |
+
|
| 109 |
+
def generate_hf_api(prompt, model_id, w, h):
|
| 110 |
+
# এই ফাংশনে সাইজ (width, height) ফিক্স করে দেওয়া হয়েছে!
|
| 111 |
url = f"https://api-inference.huggingface.co/models/{model_id}"
|
| 112 |
+
payload = {"inputs": prompt, "parameters": {"width": w, "height": h}}
|
| 113 |
+
res = requests.post(url, json=payload)
|
| 114 |
+
if res.status_code == 200: return Image.open(BytesIO(res.content))
|
| 115 |
raise Exception("Error")
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
# ==========================================
|
| 118 |
+
# 4. API Categorization
|
| 119 |
# ==========================================
|
| 120 |
+
FAST_APIS = {
|
|
|
|
|
|
|
| 121 |
"Google Imagen": generate_google_imagen,
|
| 122 |
+
"Stability AI": generate_stability,
|
| 123 |
"GetImg AI": generate_getimg,
|
| 124 |
"Segmind": generate_segmind,
|
| 125 |
"Fireworks": generate_fireworks,
|
| 126 |
+
"Pollinations AI (Unlimited)": generate_pollinations,
|
| 127 |
"OpenRouter": generate_openrouter,
|
| 128 |
"DeepAI": generate_deepai,
|
| 129 |
"Eden AI": generate_edenai,
|
| 130 |
+
"ModelsLab": generate_modelslab
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
SLOW_APIS = {
|
|
|
|
| 134 |
"Stable Horde (Crowdsourced)": generate_stable_horde,
|
| 135 |
+
"HF Space: FLUX.1": lambda p, w, h, ar: generate_hf_space("black-forest-labs/FLUX.1-schnell", "/infer", p),
|
| 136 |
+
"HF Space: SDXL Light": lambda p, w, h, ar: generate_hf_space("ByteDance/SDXL-Lightning", "/generate_image", p),
|
| 137 |
+
|
| 138 |
+
# আগের ৪টি HF API
|
| 139 |
+
"HF API: OpenJourney": lambda p, w, h, ar: generate_hf_api(p, "prompthero/openjourney", w, h),
|
| 140 |
+
"HF API: RealVisXL": lambda p, w, h, ar: generate_hf_api(p, "SG161222/RealVisXL_V4.0", w, h),
|
| 141 |
+
"HF API: Animagine XL": lambda p, w, h, ar: generate_hf_api(p, "cagliostrolab/animagine-xl-3.1", w, h),
|
| 142 |
+
"HF API: Playground v2.5": lambda p, w, h, ar: generate_hf_api(p, "playgroundai/playground-v2.5-1024px-aesthetic", w, h),
|
| 143 |
+
|
| 144 |
+
# তোমার চাওয়া নতুন ১০টি HF API!
|
| 145 |
+
"HF API: DreamShaper XL": lambda p, w, h, ar: generate_hf_api(p, "Lykon/dreamshaper-xl-1.0", w, h),
|
| 146 |
+
"HF API: OpenDalle V1.1": lambda p, w, h, ar: generate_hf_api(p, "dataautogpt3/OpenDalleV1.1", w, h),
|
| 147 |
+
"HF API: Anything V5 (Anime)": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/anything-v5", w, h),
|
| 148 |
+
"HF API: Disney Pixar Cartoon": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/disney-pixar-cartoon", w, h),
|
| 149 |
+
"HF API: CyberRealistic": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/cyberrealistic", w, h),
|
| 150 |
+
"HF API: Absolute Reality": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/absolute-reality-v181", w, h),
|
| 151 |
+
"HF API: Juggernaut XL": lambda p, w, h, ar: generate_hf_api(p, "stablediffusionapi/juggernaut-xl-v5", w, h),
|
| 152 |
+
"HF API: SDXL Base 1.0": lambda p, w, h, ar: generate_hf_api(p, "stabilityai/stable-diffusion-xl-base-1.0", w, h),
|
| 153 |
+
"HF API: Realistic Vision V5": lambda p, w, h, ar: generate_hf_api(p, "SG161222/Realistic_Vision_V5.1_noVAE", w, h),
|
| 154 |
+
"HF API: Stable Diffusion 1.5": lambda p, w, h, ar: generate_hf_api(p, "runwayml/stable-diffusion-v1-5", w, h)
|
| 155 |
}
|
| 156 |
|
| 157 |
+
# সব মিলিয়ে একটি মাস্টার ডিকশনারি (ম্যানুয়াল সিলেকশনের জন্য)
|
| 158 |
+
ALL_APIS = {**FAST_APIS, **SLOW_APIS}
|
| 159 |
+
|
| 160 |
# ==========================================
|
| 161 |
+
# 5. Generation Logic (Fast/Slow + Manual)
|
| 162 |
# ==========================================
|
| 163 |
+
def generate_image_logic(prompt, style, size, speed_mode, selected_api):
|
| 164 |
if not prompt.strip():
|
| 165 |
yield None, "⚠️ **দয়া করে একটি প্রম্পট লিখুন!**"
|
| 166 |
return
|
| 167 |
|
| 168 |
+
if size == "ল্যান্ডস্কেপ (16:9) - YouTube": w, h, ar = 1024, 576, "16:9"
|
| 169 |
+
elif size == "পোর্ট্রেট (9:16) - TikTok": w, h, ar = 576, 1024, "9:16"
|
| 170 |
+
else: w, h, ar = 1024, 1024, "1:1"
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
style_modifiers = {
|
| 173 |
"ফটোরিয়ালিস্টিক (Photorealistic)": ", ultra realistic, highly detailed, photorealistic, 8k resolution, raw photo, masterpiece, soft cinematic lighting",
|
|
|
|
| 177 |
}
|
| 178 |
enhanced_prompt = f"{prompt.strip()}{style_modifiers.get(style, '')}, aspect ratio {ar}"
|
| 179 |
|
| 180 |
+
# লজিক ১: অটোমেটিক (Fast বা Slow মোড অনুযায়ী)
|
| 181 |
if selected_api == "অটোমেটিক (Auto Fallback)":
|
| 182 |
+
target_apis = FAST_APIS if speed_mode == "Fast (ফাস্ট)" else SLOW_APIS
|
| 183 |
+
yield None, f"⏳ {speed_mode} মোডে শুরু হচ্ছে..."
|
| 184 |
+
|
| 185 |
+
for api_name, api_func in target_apis.items():
|
| 186 |
+
yield None, f"⏳ **{api_name}**-এ কল করা হচ্ছে..."
|
| 187 |
try:
|
| 188 |
image = api_func(enhanced_prompt, w, h, ar)
|
| 189 |
if image:
|
| 190 |
+
yield image, f"✅ **সফল!** অটোমেটিক {api_name} থেকে ছবি তৈরি হয়েছে।"
|
| 191 |
return
|
| 192 |
except Exception as e:
|
| 193 |
+
yield None, f"❌ **{api_name}** ব্যস্ত বা লিমিট শেষ। পরেরটিতে যাচ্ছে..."
|
| 194 |
continue
|
| 195 |
+
yield None, "🚫 **দুঃখিত! এই মোডের সবগুলো সার্ভার বর্তমানে ব্যস্ত।**"
|
| 196 |
|
| 197 |
+
# লজিক ২: ম্যানুয়ালি নির্দিষ্ট API সিলেক্ট করলে
|
| 198 |
else:
|
| 199 |
+
yield None, f"⏳ শুধুমাত্র **{selected_api}**-এ কল করা হচ্ছে..."
|
| 200 |
+
api_func = ALL_APIS[selected_api]
|
| 201 |
try:
|
| 202 |
image = api_func(enhanced_prompt, w, h, ar)
|
| 203 |
+
if image: yield image, f"✅ **সফল!** আপনি ম্যানুয়ালি {selected_api} ব্যবহার করেছেন।"
|
| 204 |
+
else: yield None, f"❌ **দুঃখিত!** {selected_api} বর্তমানে ডাউন।"
|
|
|
|
|
|
|
| 205 |
except Exception as e:
|
| 206 |
+
yield None, f"❌ **দুঃখিত!** {selected_api} এর সার্ভার ফুল অথবা লিমিট শেষ। অন্য একটি ট্রাই করুন।"
|
| 207 |
|
| 208 |
# ==========================================
|
| 209 |
# 6. Gradio Web UI
|
| 210 |
# ==========================================
|
| 211 |
+
api_choices = ["অটোমেটিক (Auto Fallback)"] + list(ALL_APIS.keys())
|
| 212 |
|
| 213 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 214 |
+
gr.Markdown("<h1 style='text-align: center;'>🎨 Pro AI Image Generator (28 Servers)</h1>")
|
| 215 |
+
|
|
|
|
| 216 |
with gr.Row():
|
| 217 |
with gr.Column(scale=1):
|
| 218 |
prompt_input = gr.Textbox(label="ইমেজের বর্ণনা লিখুন (Prompt)", placeholder="A highly detailed futuristic city...", lines=3)
|
|
|
|
| 220 |
with gr.Row():
|
| 221 |
style_input = gr.Dropdown(
|
| 222 |
choices=["ফটোরিয়ালিস্টিক (Photorealistic)", "এনিমে (Anime)", "থ্রিডি আর্ট (3D)", "ডিফল্ট (Default)"],
|
| 223 |
+
label="ছবির স্টাইল (Style)", value="ফটোরিয়ালিস্টিক (Photorealistic)"
|
|
|
|
| 224 |
)
|
|
|
|
| 225 |
size_input = gr.Dropdown(
|
| 226 |
choices=["স্কয়ার (1:1) - Instagram", "ল্যান্ডস্কেপ (16:9) - YouTube", "পোর্ট্রেট (9:16) - TikTok"],
|
| 227 |
+
label="ছবির সাইজ (Size)", value="স্কয়ার (1:1) - Instagram"
|
|
|
|
| 228 |
)
|
| 229 |
|
| 230 |
+
# নতুন Speed Mode অপশন
|
| 231 |
+
speed_mode = gr.Radio(
|
| 232 |
+
choices=["Fast (ফাস্ট)", "Slow (স্লো) - High Quality"],
|
| 233 |
+
label="স্পিড মোড (অটোমেটিকের জন্য)", value="Fast (ফাস্ট)"
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
api_selector = gr.Dropdown(
|
| 237 |
+
choices=api_choices, label="সার্ভার ম্যানুয়ালি সিলেক্ট করুন", value="অটোমেটিক (Auto Fallback)", interactive=True
|
|
|
|
|
|
|
|
|
|
| 238 |
)
|
| 239 |
|
| 240 |
generate_btn = gr.Button("Generate Image", variant="primary")
|
|
|
|
| 245 |
|
| 246 |
generate_btn.click(
|
| 247 |
fn=generate_image_logic,
|
| 248 |
+
inputs=[prompt_input, style_input, size_input, speed_mode, api_selector],
|
| 249 |
outputs=[image_output, log_box]
|
| 250 |
)
|
| 251 |
|