Spaces:
Runtime error
Runtime error
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -73,19 +73,22 @@ def query_databank(filename: str) -> str:
|
|
| 73 |
|
| 74 |
@mcp.tool()
|
| 75 |
def generate_image(prompt: str) -> str:
|
| 76 |
-
"""Generate an image with multiple model fallbacks
|
| 77 |
try:
|
| 78 |
from huggingface_hub import InferenceClient
|
| 79 |
-
hf_client = InferenceClient(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 87 |
models = [
|
| 88 |
-
"black-forest-labs/FLUX.1-schnell",
|
| 89 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 90 |
"runwayml/stable-diffusion-v1-5",
|
| 91 |
"CompVis/stable-diffusion-v1-4"
|
|
@@ -98,18 +101,18 @@ def generate_image(prompt: str) -> str:
|
|
| 98 |
try:
|
| 99 |
image = hf_client.text_to_image(full_prompt, model=model_id)
|
| 100 |
model_used = model_id
|
| 101 |
-
break
|
| 102 |
except Exception as e:
|
| 103 |
print(f"Model {model_id} failed: {str(e)}")
|
| 104 |
-
continue
|
| 105 |
|
| 106 |
if not image:
|
| 107 |
-
return "Error: All image generation models failed
|
| 108 |
|
| 109 |
os.makedirs("exports/images", exist_ok=True)
|
| 110 |
image_path = f"exports/images/{abs(hash(prompt))}.png"
|
| 111 |
image.save(image_path)
|
| 112 |
-
return f"Branded Image Generated using {model_used}: {image_path}"
|
| 113 |
except Exception as e:
|
| 114 |
return f"System Error during image generation: {str(e)}"
|
| 115 |
|
|
@@ -213,10 +216,14 @@ def aussie_router(user_input, history):
|
|
| 213 |
)
|
| 214 |
return response.choices[0].message.content
|
| 215 |
except Exception as e:
|
| 216 |
-
# Fallback to Free Llama 3.1 on Hugging Face
|
| 217 |
try:
|
| 218 |
from huggingface_hub import InferenceClient
|
| 219 |
-
hf_client = InferenceClient(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
response = hf_client.chat_completion(
|
| 221 |
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 222 |
messages=messages,
|
|
|
|
| 73 |
|
| 74 |
@mcp.tool()
|
| 75 |
def generate_image(prompt: str) -> str:
|
| 76 |
+
"""Generate an image with multiple model fallbacks using the free hf-inference provider."""
|
| 77 |
try:
|
| 78 |
from huggingface_hub import InferenceClient
|
| 79 |
+
hf_client = InferenceClient(
|
| 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 |
+
# Truly free models on hf-inference
|
| 91 |
models = [
|
|
|
|
| 92 |
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 93 |
"runwayml/stable-diffusion-v1-5",
|
| 94 |
"CompVis/stable-diffusion-v1-4"
|
|
|
|
| 101 |
try:
|
| 102 |
image = hf_client.text_to_image(full_prompt, model=model_id)
|
| 103 |
model_used = model_id
|
| 104 |
+
break
|
| 105 |
except Exception as e:
|
| 106 |
print(f"Model {model_id} failed: {str(e)}")
|
| 107 |
+
continue
|
| 108 |
|
| 109 |
if not image:
|
| 110 |
+
return "Error: All free image generation models failed. The serverless API may be overloaded."
|
| 111 |
|
| 112 |
os.makedirs("exports/images", exist_ok=True)
|
| 113 |
image_path = f"exports/images/{abs(hash(prompt))}.png"
|
| 114 |
image.save(image_path)
|
| 115 |
+
return f"Branded Image Generated using {model_used} (Free Tier): {image_path}"
|
| 116 |
except Exception as e:
|
| 117 |
return f"System Error during image generation: {str(e)}"
|
| 118 |
|
|
|
|
| 216 |
)
|
| 217 |
return response.choices[0].message.content
|
| 218 |
except Exception as e:
|
| 219 |
+
# Fallback to Free Llama 3.1 on Hugging Face (FORCED FREE TIER)
|
| 220 |
try:
|
| 221 |
from huggingface_hub import InferenceClient
|
| 222 |
+
hf_client = InferenceClient(
|
| 223 |
+
provider="hf-inference",
|
| 224 |
+
token=HF_TOKEN,
|
| 225 |
+
headers={"x-wait-for-model": "true"}
|
| 226 |
+
)
|
| 227 |
response = hf_client.chat_completion(
|
| 228 |
model="meta-llama/Meta-Llama-3.1-8B-Instruct",
|
| 229 |
messages=messages,
|