ProfessorCEO commited on
Commit
c97bb05
·
verified ·
1 Parent(s): 906ed96

Upload vision/image_generator.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. 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 = "turbo"
27
  ) -> Dict[str, Any]:
28
  """
29
- Generate an image from a prompt using Stable Diffusion Turbo / SDXL with zero watermarks.
30
- Automatically retries fallback models if any model rate limits.
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, pristine quality, high resolution, vector asset, 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
- models_to_try = [model, "turbo", "sdxl"] if model != "turbo" else ["turbo", "sdxl"]
45
  data = None
46
  image_url = ""
47
 
48
- for m in models_to_try:
49
- try_url = f"https://image.pollinations.ai/prompt/{encoded_prompt}?width={width}&height={height}&model={m}&nologo=true&nofeed=true{seed_param}"
50
- print(f"🎨 Generating image with Stable Diffusion [{m}] for prompt: '{prompt}'...", flush=True)
51
- req = urllib.request.Request(
52
- try_url,
53
- headers={"User-Agent": "KoraVision/1.0 (Kontyra AI)"}
54
- )
55
  try:
56
- with urllib.request.urlopen(req, timeout=30) as resp:
 
 
 
 
 
 
 
 
 
 
57
  data = resp.read()
58
- image_url = try_url
59
- break
60
- except Exception as err:
61
- print(f"⚠️ Model {m} failed ({err}), retrying next model...", flush=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: