Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,44 +4,126 @@ import requests
|
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if randomize:
|
| 11 |
-
seed = random.randint(0,
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
if
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
else:
|
| 27 |
-
return "
|
| 28 |
|
| 29 |
-
|
| 30 |
-
description = "Here you can use the FLUX-Pro as much as you want without limits"
|
| 31 |
|
| 32 |
iface = gr.Interface(
|
| 33 |
fn=generate_image,
|
| 34 |
inputs=[
|
| 35 |
-
gr.Textbox(label="Prompt", placeholder="Enter your image prompt
|
| 36 |
-
gr.Slider(
|
| 37 |
-
gr.Slider(
|
| 38 |
gr.Number(label="Seed", value=0),
|
| 39 |
-
gr.Checkbox(label="Randomize Seed", value=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
],
|
| 41 |
outputs=gr.Image(type="pil"),
|
| 42 |
-
title=
|
| 43 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
| 46 |
if __name__ == "__main__":
|
| 47 |
-
iface.launch()
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
+
from gradio_client import Client
|
| 8 |
|
| 9 |
+
# more servers coming soon...
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
SERVER_NAMES = {
|
| 13 |
+
"google_us": "Google US Server",
|
| 14 |
+
"azure_lite": "Azure Lite Supercomputer Server",
|
| 15 |
+
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
SERVER_SOCKETS = {
|
| 20 |
+
"google_us": None,
|
| 21 |
+
"azure_lite": "FLUX-Pro-SERVER1",
|
| 22 |
+
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 26 |
+
FLUX_URL = os.environ.get("FLUX_URL")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _open_image_from_str(s: str):
|
| 31 |
+
# base64 decoding
|
| 32 |
+
if s.startswith("http"):
|
| 33 |
+
r = requests.get(s); return Image.open(BytesIO(r.content))
|
| 34 |
+
if os.path.exists(s):
|
| 35 |
+
return Image.open(s)
|
| 36 |
+
# try base64 blob
|
| 37 |
+
try:
|
| 38 |
+
import base64
|
| 39 |
+
_, b64 = s.split(",", 1)
|
| 40 |
+
data = base64.b64decode(b64)
|
| 41 |
+
return Image.open(BytesIO(data))
|
| 42 |
+
except:
|
| 43 |
+
raise ValueError(f"Can't parse image string: {s[:30]}…")
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def generate_image(prompt, width, height, seed, randomize, server_choice):
|
| 48 |
+
|
| 49 |
+
print("\n\n\n\n"+prompt+"\n\n\n\n")
|
| 50 |
+
|
| 51 |
if randomize:
|
| 52 |
+
seed = random.randint(0, 9_999_999)
|
| 53 |
+
|
| 54 |
+
# dict key for this display name
|
| 55 |
+
key = next(k for k, v in SERVER_NAMES.items() if v == server_choice)
|
| 56 |
+
socket = SERVER_SOCKETS.get(key)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if socket is None:
|
| 60 |
+
if not FLUX_URL:
|
| 61 |
+
return "Error: FLUX_URL not set."
|
| 62 |
+
url = (
|
| 63 |
+
FLUX_URL
|
| 64 |
+
.replace("[prompt]", prompt)
|
| 65 |
+
.replace("[w]", str(width))
|
| 66 |
+
.replace("[h]", str(height))
|
| 67 |
+
.replace("[seed]", str(seed))
|
| 68 |
+
)
|
| 69 |
+
r = requests.get(url)
|
| 70 |
+
return Image.open(BytesIO(r.content)) if r.ok else f"FLUX-Pro failed ({r.status_code})"
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
space_id = f"NihalGazi/{socket}"
|
| 74 |
+
client = Client(space_id, hf_token=HF_TOKEN)
|
| 75 |
+
res = client.predict(
|
| 76 |
+
prompt=prompt,
|
| 77 |
+
width=width,
|
| 78 |
+
height=height,
|
| 79 |
+
seed=seed,
|
| 80 |
+
randomize=randomize,
|
| 81 |
+
api_name="/predict"
|
| 82 |
)
|
| 83 |
+
|
| 84 |
+
# handle dict or str
|
| 85 |
+
if isinstance(res, dict):
|
| 86 |
+
if res.get("path"):
|
| 87 |
+
return Image.open(res["path"])
|
| 88 |
+
if res.get("url"):
|
| 89 |
+
return _open_image_from_str(res["url"])
|
| 90 |
+
return "No image found in response."
|
| 91 |
+
elif isinstance(res, str):
|
| 92 |
+
return _open_image_from_str(res)
|
| 93 |
else:
|
| 94 |
+
return f"Unexpected response type: {type(res)}"
|
| 95 |
|
| 96 |
+
# ─── GRADIO INTERFACE ─────────────────────────────────────────────────────
|
|
|
|
| 97 |
|
| 98 |
iface = gr.Interface(
|
| 99 |
fn=generate_image,
|
| 100 |
inputs=[
|
| 101 |
+
gr.Textbox(label="Prompt", placeholder="Enter your image prompt…"),
|
| 102 |
+
gr.Slider(512, 1280, step=16, value=1280, label="Width"),
|
| 103 |
+
gr.Slider(512, 1280, step=16, value=1280, label="Height"),
|
| 104 |
gr.Number(label="Seed", value=0),
|
| 105 |
+
gr.Checkbox(label="Randomize Seed", value=True),
|
| 106 |
+
gr.Dropdown(
|
| 107 |
+
label="Server",
|
| 108 |
+
choices=list(SERVER_NAMES.values()),
|
| 109 |
+
value=SERVER_NAMES["google_us"]
|
| 110 |
+
),
|
| 111 |
],
|
| 112 |
outputs=gr.Image(type="pil"),
|
| 113 |
+
title="Unlimited FLUX‑Pro",
|
| 114 |
+
description="""**Enter a prompt and tweak your settings:**
|
| 115 |
+
- **Server** – switch between servers *if one is slow or fails*:
|
| 116 |
+
1. **Google US Server**
|
| 117 |
+
2. **Azure Lite Supercomputer Server**
|
| 118 |
+
- **Width & Height** – choose your canvas size
|
| 119 |
+
- **Seed** – pick a number or check **Randomize Seed**
|
| 120 |
+
|
| 121 |
+
Click **Generate** and enjoy unlimited AI art!
|
| 122 |
+
❤️ **Like & follow** for more AI projects:
|
| 123 |
+
• Instagram: [@nihal_gazi_io](https://www.instagram.com/nihal_gazi_io/)
|
| 124 |
+
• Discord: nihal_gazi_io
|
| 125 |
+
"""
|
| 126 |
)
|
| 127 |
|
| 128 |
if __name__ == "__main__":
|
| 129 |
+
iface.launch()
|