Spaces:
Running
Running
Deploy: hf-inference-proxy — via Anushka AI
Browse files- README.md +9 -7
- app.py +126 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,13 +1,15 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
python_version: '3.13'
|
| 9 |
app_file: app.py
|
| 10 |
pinned: false
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: hf-inference-proxy
|
| 3 |
+
emoji: 🖼️
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.31.0
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# HuggingFace Inference Image Generator
|
| 13 |
+
FLUX.1 · SDXL · Realistic Vision · Dreamshaper · Anything v5
|
| 14 |
+
|
| 15 |
+
Built for Anushka AI — AJ's autonomous assistant.
|
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
HF Inference Proxy — Routes image generation requests to HF Inference API
|
| 3 |
+
Deployed as a HF Space so it runs on HF's own servers (bypasses Oracle firewall)
|
| 4 |
+
The Oracle server calls THIS space, which calls HF Inference internally.
|
| 5 |
+
|
| 6 |
+
Supports: SDXL, FLUX.1, Realistic Vision, Dreamshaper, Anything v5
|
| 7 |
+
"""
|
| 8 |
+
import os, requests, base64, time
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import gradio as gr
|
| 12 |
+
|
| 13 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 14 |
+
|
| 15 |
+
MODELS = {
|
| 16 |
+
"FLUX.1 Schnell (Fast)": "black-forest-labs/FLUX.1-schnell",
|
| 17 |
+
"FLUX.1 Dev (Best Quality)": "black-forest-labs/FLUX.1-dev",
|
| 18 |
+
"Stable Diffusion XL": "stabilityai/stable-diffusion-xl-base-1.0",
|
| 19 |
+
"Realistic Vision v6": "SG161222/Realistic_Vision_V6.0_B1_noVAE",
|
| 20 |
+
"Dreamshaper XL": "Lykon/dreamshaper-xl-lightning",
|
| 21 |
+
"Anything v5 (Anime)": "stablediffusionapi/anything-v5",
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def generate(prompt: str, model_name: str, width: int = 512,
|
| 26 |
+
height: int = 512, steps: int = 20) -> tuple:
|
| 27 |
+
if not prompt.strip():
|
| 28 |
+
return None, "Enter a prompt"
|
| 29 |
+
if not HF_TOKEN:
|
| 30 |
+
return None, "HF_TOKEN secret not configured on this Space"
|
| 31 |
+
|
| 32 |
+
model_id = MODELS.get(model_name, "black-forest-labs/FLUX.1-schnell")
|
| 33 |
+
t0 = time.time()
|
| 34 |
+
|
| 35 |
+
# FLUX models use different parameter names
|
| 36 |
+
is_flux = "FLUX" in model_name
|
| 37 |
+
payload = {"inputs": prompt}
|
| 38 |
+
if not is_flux:
|
| 39 |
+
payload["parameters"] = {
|
| 40 |
+
"num_inference_steps": steps,
|
| 41 |
+
"width": width,
|
| 42 |
+
"height": height,
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
r = requests.post(
|
| 47 |
+
f"https://api-inference.huggingface.co/models/{model_id}",
|
| 48 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 49 |
+
json=payload,
|
| 50 |
+
timeout=120,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if r.status_code == 503:
|
| 54 |
+
# Model loading, wait and retry
|
| 55 |
+
time.sleep(25)
|
| 56 |
+
r = requests.post(
|
| 57 |
+
f"https://api-inference.huggingface.co/models/{model_id}",
|
| 58 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 59 |
+
json=payload,
|
| 60 |
+
timeout=120,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
if r.status_code == 200 and "image" in r.headers.get("Content-Type", ""):
|
| 64 |
+
img = Image.open(BytesIO(r.content))
|
| 65 |
+
elapsed = time.time() - t0
|
| 66 |
+
return img, f"✅ {model_name} | {img.size[0]}×{img.size[1]} | {elapsed:.1f}s"
|
| 67 |
+
|
| 68 |
+
err = ""
|
| 69 |
+
try:
|
| 70 |
+
err = r.json().get("error", r.text[:200])
|
| 71 |
+
except Exception:
|
| 72 |
+
err = r.text[:200]
|
| 73 |
+
return None, f"❌ HTTP {r.status_code}: {err}"
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return None, f"❌ Error: {str(e)[:150]}"
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def generate_api(prompt: str, model_name: str = "FLUX.1 Schnell (Fast)",
|
| 80 |
+
width: int = 512, height: int = 512) -> dict:
|
| 81 |
+
"""
|
| 82 |
+
API endpoint — called by Anushka's server via HTTP.
|
| 83 |
+
Returns base64-encoded image or error.
|
| 84 |
+
"""
|
| 85 |
+
img, status = generate(prompt, model_name, width, height)
|
| 86 |
+
if img:
|
| 87 |
+
buf = BytesIO()
|
| 88 |
+
img.save(buf, format="PNG")
|
| 89 |
+
b64 = base64.b64encode(buf.getvalue()).decode()
|
| 90 |
+
return {"success": True, "image_b64": b64, "status": status}
|
| 91 |
+
return {"success": False, "error": status}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
# Gradio UI
|
| 95 |
+
with gr.Blocks(title="HF Inference Proxy", theme=gr.themes.Soft()) as demo:
|
| 96 |
+
gr.Markdown("""
|
| 97 |
+
# 🖼️ HuggingFace Inference API — Image Generator
|
| 98 |
+
High quality AI images via FLUX.1, SDXL, Realistic Vision, and more.
|
| 99 |
+
*Note: First generation may take 30-60s while model loads.*
|
| 100 |
+
""")
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
with gr.Column():
|
| 104 |
+
prompt = gr.Textbox(label="Prompt", lines=3,
|
| 105 |
+
placeholder="Describe your image in detail...")
|
| 106 |
+
neg = gr.Textbox(label="Negative Prompt", lines=2,
|
| 107 |
+
value="ugly, blurry, low quality, watermark",
|
| 108 |
+
placeholder="What to avoid...")
|
| 109 |
+
model = gr.Dropdown(list(MODELS.keys()),
|
| 110 |
+
value="FLUX.1 Schnell (Fast)", label="Model")
|
| 111 |
+
with gr.Row():
|
| 112 |
+
w = gr.Slider(256, 1024, 512, step=64, label="Width")
|
| 113 |
+
h = gr.Slider(256, 1024, 768, step=64, label="Height")
|
| 114 |
+
steps = gr.Slider(10, 50, 20, step=5, label="Steps")
|
| 115 |
+
btn = gr.Button("🎨 Generate", variant="primary")
|
| 116 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 117 |
+
|
| 118 |
+
with gr.Column():
|
| 119 |
+
output = gr.Image(label="Result", type="pil", height=550)
|
| 120 |
+
|
| 121 |
+
btn.click(generate, inputs=[prompt, model, w, h, steps],
|
| 122 |
+
outputs=[output, status])
|
| 123 |
+
prompt.submit(generate, inputs=[prompt, model, w, h, steps],
|
| 124 |
+
outputs=[output, status])
|
| 125 |
+
|
| 126 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
Pillow
|