Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
Gradio Space: Text → Image using FLUX.1 (Hugging Face Inference API)
|
| 3 |
-
Attractive interface with custom styling and footer label: "designed by Mehak Mazhar"
|
| 4 |
-
|
| 5 |
-
How to use:
|
| 6 |
-
1. Install dependencies: pip install gradio requests pillow
|
| 7 |
-
2. Get a Hugging Face API token (if you want to use the hosted FLUX.1 models) and either set it as an env var HF_TOKEN or paste it into the 'HF Token' field in the UI.
|
| 8 |
-
3. Run: python gradio_flux_text2img.py
|
| 9 |
-
|
| 10 |
-
Notes: this script calls the Hugging Face Inference API for the model 'black-forest-labs/FLUX.1-schnell' by default.
|
| 11 |
-
You can change the MODEL variable to any compatible image generation model hosted on Hugging Face or point to your own inference server.
|
| 12 |
-
|
| 13 |
-
"""
|
| 14 |
|
| 15 |
import os
|
| 16 |
import io
|
|
@@ -24,7 +12,7 @@ import gradio as gr
|
|
| 24 |
MODEL = os.environ.get("FLUX_MODEL", "black-forest-labs/FLUX.1-schnell")
|
| 25 |
HF_API_URL = f"https://api-inference.huggingface.co/models/{MODEL}"
|
| 26 |
|
| 27 |
-
#
|
| 28 |
def call_hf_image_api(prompt, token, width, height, guidance_scale, steps, seed, negative_prompt=None):
|
| 29 |
headers = {"Authorization": f"Bearer {token}"} if token else {}
|
| 30 |
payload = {
|
|
@@ -41,30 +29,25 @@ def call_hf_image_api(prompt, token, width, height, guidance_scale, steps, seed,
|
|
| 41 |
if negative_prompt:
|
| 42 |
payload["parameters"]["negative_prompt"] = negative_prompt
|
| 43 |
|
| 44 |
-
# Many HF image models return binary image bytes directly. Some return JSON with base64.
|
| 45 |
resp = requests.post(HF_API_URL, headers=headers, json=payload, stream=True, timeout=120)
|
| 46 |
resp.raise_for_status()
|
| 47 |
|
| 48 |
content_type = resp.headers.get("content-type", "")
|
| 49 |
if "application/json" in content_type:
|
| 50 |
data = resp.json()
|
| 51 |
-
# attempt to find a base64 image in JSON response
|
| 52 |
-
# common key patterns: 'image', 'images', 'generated_images'
|
| 53 |
if isinstance(data, dict):
|
| 54 |
for k in ("image", "images", "generated_images", "artifacts"):
|
| 55 |
if k in data:
|
| 56 |
imgs = data[k]
|
| 57 |
-
if isinstance(imgs, list) and
|
| 58 |
b64 = imgs[0].get("data") if isinstance(imgs[0], dict) else imgs[0]
|
| 59 |
if isinstance(b64, str):
|
| 60 |
return Image.open(io.BytesIO(base64.b64decode(b64)))
|
| 61 |
-
# fallback: try to find base64 strings in the JSON
|
| 62 |
for v in data.values():
|
| 63 |
-
if isinstance(v, str) and v.strip().startswith("iVBOR"):
|
| 64 |
return Image.open(io.BytesIO(base64.b64decode(v)))
|
| 65 |
raise ValueError("Could not parse image from JSON response")
|
| 66 |
else:
|
| 67 |
-
# assume raw image bytes
|
| 68 |
return Image.open(io.BytesIO(resp.content))
|
| 69 |
|
| 70 |
# --- Gradio UI ---
|
|
@@ -82,7 +65,14 @@ body { background: linear-gradient(135deg, #fff7e6 0%, #fffaf0 50%, #fff7fd 100%
|
|
| 82 |
with gr.Blocks(css=css, title="Flux Text→Image — designed by Mehak Mazhar") as demo:
|
| 83 |
with gr.Row(elem_id="top-row"):
|
| 84 |
with gr.Column(scale=1):
|
| 85 |
-
gr.HTML(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column(scale=1, min_width=360):
|
|
@@ -104,8 +94,7 @@ with gr.Blocks(css=css, title="Flux Text→Image — designed by Mehak Mazhar")
|
|
| 104 |
gallery = gr.Gallery(label="Generated images", show_label=True, elem_id="gallery").style(grid=[2], height="640px")
|
| 105 |
out_log = gr.Textbox(label="Status / Debug log", lines=4, interactive=False)
|
| 106 |
|
| 107 |
-
|
| 108 |
-
gr.HTML("<div class='footer'>\n<p><strong>designed by Mehak Mazhar</strong></p>\n</div>")
|
| 109 |
|
| 110 |
def generate_image(prompt_text, negative_text, hf_token_text, width_v, height_v, steps_v, guidance_v, seed_v):
|
| 111 |
token = hf_token_text.strip() or os.environ.get("HF_TOKEN")
|
|
@@ -123,6 +112,5 @@ with gr.Blocks(css=css, title="Flux Text→Image — designed by Mehak Mazhar")
|
|
| 123 |
|
| 124 |
gen_btn.click(fn=generate_image, inputs=[prompt, negative, hf_token, width, height, steps, guidance, seed], outputs=[gallery, out_log])
|
| 125 |
|
| 126 |
-
# Run the app when executed directly
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch(server_name="0.0.0.0", share=False)
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
import io
|
|
|
|
| 12 |
MODEL = os.environ.get("FLUX_MODEL", "black-forest-labs/FLUX.1-schnell")
|
| 13 |
HF_API_URL = f"https://api-inference.huggingface.co/models/{MODEL}"
|
| 14 |
|
| 15 |
+
# --- Helper to call Hugging Face API ---
|
| 16 |
def call_hf_image_api(prompt, token, width, height, guidance_scale, steps, seed, negative_prompt=None):
|
| 17 |
headers = {"Authorization": f"Bearer {token}"} if token else {}
|
| 18 |
payload = {
|
|
|
|
| 29 |
if negative_prompt:
|
| 30 |
payload["parameters"]["negative_prompt"] = negative_prompt
|
| 31 |
|
|
|
|
| 32 |
resp = requests.post(HF_API_URL, headers=headers, json=payload, stream=True, timeout=120)
|
| 33 |
resp.raise_for_status()
|
| 34 |
|
| 35 |
content_type = resp.headers.get("content-type", "")
|
| 36 |
if "application/json" in content_type:
|
| 37 |
data = resp.json()
|
|
|
|
|
|
|
| 38 |
if isinstance(data, dict):
|
| 39 |
for k in ("image", "images", "generated_images", "artifacts"):
|
| 40 |
if k in data:
|
| 41 |
imgs = data[k]
|
| 42 |
+
if isinstance(imgs, list) and imgs:
|
| 43 |
b64 = imgs[0].get("data") if isinstance(imgs[0], dict) else imgs[0]
|
| 44 |
if isinstance(b64, str):
|
| 45 |
return Image.open(io.BytesIO(base64.b64decode(b64)))
|
|
|
|
| 46 |
for v in data.values():
|
| 47 |
+
if isinstance(v, str) and v.strip().startswith("iVBOR"):
|
| 48 |
return Image.open(io.BytesIO(base64.b64decode(v)))
|
| 49 |
raise ValueError("Could not parse image from JSON response")
|
| 50 |
else:
|
|
|
|
| 51 |
return Image.open(io.BytesIO(resp.content))
|
| 52 |
|
| 53 |
# --- Gradio UI ---
|
|
|
|
| 65 |
with gr.Blocks(css=css, title="Flux Text→Image — designed by Mehak Mazhar") as demo:
|
| 66 |
with gr.Row(elem_id="top-row"):
|
| 67 |
with gr.Column(scale=1):
|
| 68 |
+
gr.HTML(
|
| 69 |
+
"<div class='header'>"
|
| 70 |
+
"<img class='logo' src='https://raw.githubusercontent.com/black-forest-labs/flux/main/logo.png' "
|
| 71 |
+
"alt='Flux logo' onerror=\"this.style.display='none'\"> "
|
| 72 |
+
"<div><h2 style='margin:0'>FLUX.1 Text → Image</h2>"
|
| 73 |
+
"<p style='margin:0;color:#555;'>Generate high-quality images from text (Hugging Face inference API)</p></div>"
|
| 74 |
+
"</div>"
|
| 75 |
+
)
|
| 76 |
|
| 77 |
with gr.Row():
|
| 78 |
with gr.Column(scale=1, min_width=360):
|
|
|
|
| 94 |
gallery = gr.Gallery(label="Generated images", show_label=True, elem_id="gallery").style(grid=[2], height="640px")
|
| 95 |
out_log = gr.Textbox(label="Status / Debug log", lines=4, interactive=False)
|
| 96 |
|
| 97 |
+
gr.HTML("<div class='footer'><p><strong>designed by Mehak Mazhar</strong></p></div>")
|
|
|
|
| 98 |
|
| 99 |
def generate_image(prompt_text, negative_text, hf_token_text, width_v, height_v, steps_v, guidance_v, seed_v):
|
| 100 |
token = hf_token_text.strip() or os.environ.get("HF_TOKEN")
|
|
|
|
| 112 |
|
| 113 |
gen_btn.click(fn=generate_image, inputs=[prompt, negative, hf_token, width, height, steps, guidance, seed], outputs=[gallery, out_log])
|
| 114 |
|
|
|
|
| 115 |
if __name__ == "__main__":
|
| 116 |
demo.launch(server_name="0.0.0.0", share=False)
|