Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -73,46 +73,57 @@ def query_databank(filename: str) -> str:
|
|
| 73 |
|
| 74 |
@mcp.tool()
|
| 75 |
def generate_image(prompt: str) -> str:
|
| 76 |
-
"""Generate an image
|
| 77 |
try:
|
| 78 |
-
from
|
| 79 |
-
|
| 80 |
-
provider="hf-inference",
|
| 81 |
-
token=HF_TOKEN,
|
| 82 |
-
headers={"x-wait-for-model": "true"}
|
| 83 |
-
)
|
| 84 |
|
| 85 |
business_name = os.environ.get("BUSINESS_NAME", "Fair Dinkum Publishing")
|
| 86 |
owner = os.environ.get("BUSINESS_OWNER", "BRETT SJOBERG")
|
| 87 |
brand_context = f"Professional brand asset for {business_name} (Owner: {owner}). Style: Modern, clean, high-quality. "
|
| 88 |
full_prompt = brand_context + prompt
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
"
|
| 93 |
-
"
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
except Exception as e:
|
| 117 |
return f"System Error during image generation: {str(e)}"
|
| 118 |
|
|
|
|
| 73 |
|
| 74 |
@mcp.tool()
|
| 75 |
def generate_image(prompt: str) -> str:
|
| 76 |
+
"""Generate an image using a free ZeroGPU Space via gradio_client, with fallbacks."""
|
| 77 |
try:
|
| 78 |
+
from gradio_client import Client
|
| 79 |
+
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
business_name = os.environ.get("BUSINESS_NAME", "Fair Dinkum Publishing")
|
| 82 |
owner = os.environ.get("BUSINESS_OWNER", "BRETT SJOBERG")
|
| 83 |
brand_context = f"Professional brand asset for {business_name} (Owner: {owner}). Style: Modern, clean, high-quality. "
|
| 84 |
full_prompt = brand_context + prompt
|
| 85 |
|
| 86 |
+
try:
|
| 87 |
+
# Method 1: Gradio Client (ZeroGPU Space - Truly Free)
|
| 88 |
+
print(f"Attempting free generation via Gradio Client...")
|
| 89 |
+
client = Client("black-forest-labs/FLUX.1-schnell", hf_token=HF_TOKEN)
|
| 90 |
+
result = client.predict(
|
| 91 |
+
prompt=full_prompt,
|
| 92 |
+
seed=0,
|
| 93 |
+
randomize_seed=True,
|
| 94 |
+
width=1024,
|
| 95 |
+
height=1024,
|
| 96 |
+
num_inference_steps=4,
|
| 97 |
+
api_name="/infer"
|
| 98 |
+
)
|
| 99 |
+
# Result is a tuple where the first element is the image path
|
| 100 |
+
temp_image_path = result[0] if isinstance(result, (list, tuple)) else result
|
| 101 |
+
|
| 102 |
+
os.makedirs("exports/images", exist_ok=True)
|
| 103 |
+
final_path = f"exports/images/{abs(hash(prompt))}.png"
|
| 104 |
+
shutil.copy(temp_image_path, final_path)
|
| 105 |
+
return f"Branded Image Generated using FLUX.1 (Free ZeroGPU Space): {final_path}"
|
| 106 |
+
|
| 107 |
+
except Exception as g_e:
|
| 108 |
+
print(f"Gradio Client method failed: {str(g_e)}. Falling back to classic serverless...")
|
| 109 |
+
|
| 110 |
+
# Fallback to Classic Serverless (Method 2)
|
| 111 |
+
from huggingface_hub import InferenceClient
|
| 112 |
+
hf_client = InferenceClient(provider="hf-inference", token=HF_TOKEN, headers={"x-wait-for-model": "true"})
|
| 113 |
+
|
| 114 |
+
models = ["runwayml/stable-diffusion-v1-5", "stabilityai/stable-diffusion-2-1"]
|
| 115 |
+
for model_id in models:
|
| 116 |
+
try:
|
| 117 |
+
image = hf_client.text_to_image(full_prompt, model=model_id)
|
| 118 |
+
os.makedirs("exports/images", exist_ok=True)
|
| 119 |
+
image_path = f"exports/images/{abs(hash(prompt))}.png"
|
| 120 |
+
image.save(image_path)
|
| 121 |
+
return f"Branded Image Generated using {model_id} (Free Serverless Tier): {image_path}"
|
| 122 |
+
except Exception as e:
|
| 123 |
+
continue
|
| 124 |
+
|
| 125 |
+
return f"Error: All free generation methods failed. (Gradio Error: {str(g_e)})"
|
| 126 |
+
|
| 127 |
except Exception as e:
|
| 128 |
return f"System Error during image generation: {str(e)}"
|
| 129 |
|