Spaces:
Running
Running
Upload vision/image_generator.py with huggingface_hub
Browse files- vision/image_generator.py +39 -17
vision/image_generator.py
CHANGED
|
@@ -23,11 +23,11 @@ def generate_image(
|
|
| 23 |
width: int = 1024,
|
| 24 |
height: int = 1024,
|
| 25 |
seed: Optional[int] = None,
|
| 26 |
-
model: str = "
|
| 27 |
) -> Dict[str, Any]:
|
| 28 |
"""
|
| 29 |
-
Generate an image from a prompt using Stable Diffusion
|
| 30 |
-
|
| 31 |
"""
|
| 32 |
if not output_filename:
|
| 33 |
timestamp = int(time.time())
|
|
@@ -37,28 +37,50 @@ def generate_image(
|
|
| 37 |
|
| 38 |
out_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 39 |
|
| 40 |
-
clean_prompt = prompt + ", masterpiece,
|
| 41 |
encoded_prompt = urllib.parse.quote(clean_prompt)
|
| 42 |
seed_param = f"&seed={seed}" if seed is not None else ""
|
| 43 |
|
| 44 |
-
models_to_try = [model, "turbo", "sdxl"] if model != "turbo" else ["turbo", "sdxl"]
|
| 45 |
data = None
|
| 46 |
image_url = ""
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
req = urllib.request.Request(
|
| 52 |
-
try_url,
|
| 53 |
-
headers={"User-Agent": "KoraVision/1.0 (Kontyra AI)"}
|
| 54 |
-
)
|
| 55 |
try:
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
data = resp.read()
|
| 58 |
-
image_url =
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
try:
|
| 64 |
with open(out_path, "wb") as f:
|
|
|
|
| 23 |
width: int = 1024,
|
| 24 |
height: int = 1024,
|
| 25 |
seed: Optional[int] = None,
|
| 26 |
+
model: str = "sdxl"
|
| 27 |
) -> Dict[str, Any]:
|
| 28 |
"""
|
| 29 |
+
Generate an image from a prompt using real Stable Diffusion XL (SDXL 1.0 / SDXL Turbo).
|
| 30 |
+
Uses HF Inference API if HF_TOKEN is present, with fallback to high-speed SDXL endpoints.
|
| 31 |
"""
|
| 32 |
if not output_filename:
|
| 33 |
timestamp = int(time.time())
|
|
|
|
| 37 |
|
| 38 |
out_path = os.path.join(OUTPUT_DIR, output_filename)
|
| 39 |
|
| 40 |
+
clean_prompt = prompt + ", masterpiece, highly detailed, photorealistic, sharp focus, 8k resolution, no watermark, no logo, no text"
|
| 41 |
encoded_prompt = urllib.parse.quote(clean_prompt)
|
| 42 |
seed_param = f"&seed={seed}" if seed is not None else ""
|
| 43 |
|
|
|
|
| 44 |
data = None
|
| 45 |
image_url = ""
|
| 46 |
|
| 47 |
+
# Attempt 1: Hugging Face Inference API (if HF_TOKEN is set)
|
| 48 |
+
hf_token = os.environ.get("HF_TOKEN", "").strip()
|
| 49 |
+
if hf_token:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
+
hf_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 52 |
+
print(f"🎨 Generating image with Hugging Face SDXL 1.0 for prompt: '{prompt}'...", flush=True)
|
| 53 |
+
req = urllib.request.Request(
|
| 54 |
+
hf_url,
|
| 55 |
+
data=json.dumps({"inputs": clean_prompt}).encode("utf-8"),
|
| 56 |
+
headers={
|
| 57 |
+
"Authorization": f"Bearer {hf_token}",
|
| 58 |
+
"Content-Type": "application/json"
|
| 59 |
+
}
|
| 60 |
+
)
|
| 61 |
+
with urllib.request.urlopen(req, timeout=35) as resp:
|
| 62 |
data = resp.read()
|
| 63 |
+
image_url = hf_url
|
| 64 |
+
except Exception as hf_err:
|
| 65 |
+
print(f"ℹ️ HF Inference API notice ({hf_err}), falling back to SDXL Turbo...", flush=True)
|
| 66 |
+
|
| 67 |
+
# Attempt 2 & 3: Stable Diffusion XL 1.0 / SDXL Turbo
|
| 68 |
+
if not data:
|
| 69 |
+
models_to_try = [model, "sdxl", "turbo"] if model != "sdxl" else ["sdxl", "turbo"]
|
| 70 |
+
for m in models_to_try:
|
| 71 |
+
try_url = f"https://image.pollinations.ai/prompt/{encoded_prompt}?width={width}&height={height}&model={m}&nologo=true&nofeed=true{seed_param}"
|
| 72 |
+
print(f"🎨 Generating image with Stable Diffusion XL [{m}] for prompt: '{prompt}'...", flush=True)
|
| 73 |
+
req = urllib.request.Request(
|
| 74 |
+
try_url,
|
| 75 |
+
headers={"User-Agent": "KoraVision/1.0 (Kontyra AI)"}
|
| 76 |
+
)
|
| 77 |
+
try:
|
| 78 |
+
with urllib.request.urlopen(req, timeout=30) as resp:
|
| 79 |
+
data = resp.read()
|
| 80 |
+
image_url = try_url
|
| 81 |
+
break
|
| 82 |
+
except Exception as err:
|
| 83 |
+
print(f"⚠️ Model {m} failed ({err}), retrying next SD model...", flush=True)
|
| 84 |
|
| 85 |
try:
|
| 86 |
with open(out_path, "wb") as f:
|