Nymbo commited on
Commit
6ae649b
·
verified ·
1 Parent(s): 3f16305

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -2
app.py CHANGED
@@ -82,6 +82,47 @@ def infer(
82
  ):
83
  yield img, seed
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  examples = [
86
  [
87
  "a tiny astronaut hatching from an egg on mars",
@@ -197,13 +238,21 @@ with gr.Blocks() as demo:
197
  cache_examples=False
198
  )
199
 
 
200
  gr.on(
201
  triggers=[run_button.click, prompt.submit],
202
  fn = infer,
203
  inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
204
  outputs = [result, seed],
205
- api_name="Generate_Image",
206
- api_description=TOOL_SUMMARY,
 
 
 
 
 
 
 
207
  )
208
 
209
  if __name__ == "__main__":
 
82
  ):
83
  yield img, seed
84
 
85
+
86
+ @spaces.GPU(duration=25)
87
+ def infer_mcp(
88
+ prompt: Annotated[str, "Text description of the desired image."],
89
+ seed: Annotated[int, "Random seed for reproducibility. Use 0 to randomize per call."] = 42,
90
+ randomize_seed: Annotated[bool, "If true, ignore seed and pick a new random value per call."] = True,
91
+ width: Annotated[int, "Image width in pixels. Upper bound enforced by MAX_IMAGE_SIZE."] = 768,
92
+ height: Annotated[int, "Image height in pixels. Upper bound enforced by MAX_IMAGE_SIZE."] = 768,
93
+ guidance_scale: Annotated[float, "Classifier-free guidance strength."] = 4.5,
94
+ num_inference_steps: Annotated[int, "Number of denoising iterations to perform."] = 24,
95
+ ) -> Tuple[Image.Image, int]:
96
+ """
97
+ Generate an image from a text prompt via an uncensored model.
98
+
99
+ Returns the generated image and the seed used. Return the generated media
100
+ to the user in this format: `![Alt text](URL)`.
101
+ """
102
+ if not prompt or not prompt.strip():
103
+ raise gr.Error("Please provide a non-empty prompt.")
104
+ if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
105
+ raise gr.Error(f"Width and height must be <= {MAX_IMAGE_SIZE}.")
106
+
107
+ if randomize_seed or seed == 0:
108
+ seed = random.randint(0, MAX_SEED)
109
+ generator = torch.Generator().manual_seed(seed)
110
+
111
+ final_img = None
112
+ for img in pipe.flux_pipe_call_that_returns_an_iterable_of_images(
113
+ prompt=prompt,
114
+ guidance_scale=guidance_scale,
115
+ num_inference_steps=num_inference_steps,
116
+ width=width,
117
+ height=height,
118
+ generator=generator,
119
+ output_type="pil",
120
+ good_vae=good_vae,
121
+ ):
122
+ final_img = img
123
+
124
+ return final_img, seed
125
+
126
  examples = [
127
  [
128
  "a tiny astronaut hatching from an egg on mars",
 
238
  cache_examples=False
239
  )
240
 
241
+ # UI event: uses the generator for live preview (no api_name = not exposed to MCP)
242
  gr.on(
243
  triggers=[run_button.click, prompt.submit],
244
  fn = infer,
245
  inputs = [prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
246
  outputs = [result, seed],
247
+ )
248
+
249
+ # MCP tool: uses non-generator function to avoid middleware issues
250
+ mcp_button = gr.Button("Generate (MCP)", visible=False)
251
+ mcp_button.click(
252
+ fn=infer_mcp,
253
+ inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
254
+ outputs=[result, seed],
255
+ api_name="Fun_Img_infer",
256
  )
257
 
258
  if __name__ == "__main__":