Update Modules/Generate_Image.py
Browse files- Modules/Generate_Image.py +6 -15
Modules/Generate_Image.py
CHANGED
|
@@ -8,12 +8,12 @@ from typing import Annotated
|
|
| 8 |
import gradio as gr
|
| 9 |
from PIL import Image
|
| 10 |
from huggingface_hub import InferenceClient
|
| 11 |
-
from .
|
| 12 |
|
| 13 |
from app import _log_call_end, _log_call_start, _truncate_for_log
|
| 14 |
from ._docstrings import autodoc
|
| 15 |
|
| 16 |
-
HF_API_TOKEN =
|
| 17 |
|
| 18 |
# Single source of truth for the LLM-facing tool description
|
| 19 |
TOOL_SUMMARY = (
|
|
@@ -54,9 +54,8 @@ def Generate_Image(
|
|
| 54 |
_log_call_end("Generate_Image", "error=empty prompt")
|
| 55 |
raise gr.Error("Please provide a non-empty prompt.")
|
| 56 |
enhanced_prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
| 57 |
-
providers = ["auto", "replicate", "fal-ai"]
|
| 58 |
last_error: Exception | None = None
|
| 59 |
-
for provider in
|
| 60 |
try:
|
| 61 |
client = InferenceClient(api_key=HF_API_TOKEN, provider=provider)
|
| 62 |
image = client.text_to_image(
|
|
@@ -76,21 +75,13 @@ def Generate_Image(
|
|
| 76 |
|
| 77 |
_log_call_end("Generate_Image", f"provider={provider} size={image.size} saved_to={filename}")
|
| 78 |
return output_path
|
| 79 |
-
except Exception as exc:
|
| 80 |
last_error = exc
|
| 81 |
continue
|
|
|
|
| 82 |
msg = str(last_error) if last_error else "Unknown error"
|
| 83 |
-
lowered = msg.lower()
|
| 84 |
-
if "404" in msg:
|
| 85 |
-
raise gr.Error(f"Model not found or unavailable: {model_id}. Check the id and your HF token access.")
|
| 86 |
-
if "503" in msg:
|
| 87 |
-
raise gr.Error("The model is warming up. Please try again shortly.")
|
| 88 |
-
if "401" in msg or "403" in msg:
|
| 89 |
-
raise gr.Error("Please duplicate the space and provide a `HF_READ_TOKEN` to enable Image and Video Generation.")
|
| 90 |
-
if ("api_key" in lowered) or ("hf auth login" in lowered) or ("unauthorized" in lowered) or ("forbidden" in lowered):
|
| 91 |
-
raise gr.Error("Please duplicate the space and provide a `HF_READ_TOKEN` to enable Image and Video Generation.")
|
| 92 |
_log_call_end("Generate_Image", f"error={_truncate_for_log(msg, 200)}")
|
| 93 |
-
|
| 94 |
|
| 95 |
|
| 96 |
def build_interface() -> gr.Interface:
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
from PIL import Image
|
| 10 |
from huggingface_hub import InferenceClient
|
| 11 |
+
from ._core import ROOT_DIR, get_hf_token, DEFAULT_PROVIDERS, handle_hf_error
|
| 12 |
|
| 13 |
from app import _log_call_end, _log_call_start, _truncate_for_log
|
| 14 |
from ._docstrings import autodoc
|
| 15 |
|
| 16 |
+
HF_API_TOKEN = get_hf_token()
|
| 17 |
|
| 18 |
# Single source of truth for the LLM-facing tool description
|
| 19 |
TOOL_SUMMARY = (
|
|
|
|
| 54 |
_log_call_end("Generate_Image", "error=empty prompt")
|
| 55 |
raise gr.Error("Please provide a non-empty prompt.")
|
| 56 |
enhanced_prompt = f"{prompt} | ultra detail, ultra elaboration, ultra quality, perfect."
|
|
|
|
| 57 |
last_error: Exception | None = None
|
| 58 |
+
for provider in DEFAULT_PROVIDERS:
|
| 59 |
try:
|
| 60 |
client = InferenceClient(api_key=HF_API_TOKEN, provider=provider)
|
| 61 |
image = client.text_to_image(
|
|
|
|
| 75 |
|
| 76 |
_log_call_end("Generate_Image", f"provider={provider} size={image.size} saved_to={filename}")
|
| 77 |
return output_path
|
| 78 |
+
except Exception as exc:
|
| 79 |
last_error = exc
|
| 80 |
continue
|
| 81 |
+
|
| 82 |
msg = str(last_error) if last_error else "Unknown error"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
_log_call_end("Generate_Image", f"error={_truncate_for_log(msg, 200)}")
|
| 84 |
+
handle_hf_error(msg, model_id, context="Image generation")
|
| 85 |
|
| 86 |
|
| 87 |
def build_interface() -> gr.Interface:
|