Brettapps commited on
Commit
0468e56
·
verified ·
1 Parent(s): 6341d61

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +44 -33
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 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
- "runwayml/stable-diffusion-v1-5",
93
- "stabilityai/stable-diffusion-2-1",
94
- "black-forest-labs/FLUX.1-schnell"
95
- ]
96
-
97
- image = None
98
- model_used = None
99
-
100
- for model_id in models:
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
 
 
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