Spaces:
Running
Running
Update app.py
Browse files629 unmodified lines63063163263363463563628 unmodified lines6656666676686696706716726736746756766776787 unmodified lines6866876886896906916926932 unmodified lines69669769869970070124 unmodified lines7267277287297307317328 unmodified lines7417427437447457467471 unmodified line74975075175275375475575675775813 unmodified lines772773774775776777778779780781782783784785786787788789790791792793794795796797798799629 unmodified lines}/* Default: add subtitle under titles */.gradio-container h1::after {content: "Fetch Webpage | Search DuckDuckGo | Code Interpreter | Kokoro TTS | Flux Krea (Serverless)";display: block;font-size: 1rem;font-weight: 500;28 unmodified lines)# ==========================# Flux Krea (Serverless)# ==========================HF_API_TOKEN = os.getenv("HF_READ_TOKEN")def Flux_Krea_Generate( # <-- MCP tool #5 (Flux Krea - Serverless)prompt: Annotated[str, "Text description of the image to generate."],negative_prompt: Annotated[str, "What should NOT appear in the image." ] = ("(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, ""missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, "7 unmodified linesheight: Annotated[int, "Output height in pixels (64–1216, multiple of 32 recommended)."] = 1024,) -> Image.Image:"""Generate a single image from a text prompt using the black-forest-labs/FLUX.1-Krea-devmodel via Hugging Face Inference (serverless). Returns a PIL image.Notes (MCP):- Per the latest Gradio MCP docs, images returned from tools are handled by the server and2 unmodified linesArgs:prompt (str): Text description of the image to generate.negative_prompt (str): What should NOT appear in the image.steps (int): Number of denoising steps (1–100). Higher can improve quality.cfg_scale (float): Guidance scale (1–20). Higher = follow the prompt more closely.24 unmodified linesimage = client.text_to_image(prompt=enhanced_prompt,negative_prompt=negative_prompt,model="black-forest-labs/FLUX.1-Krea-dev",width=width,height=height,num_inference_steps=steps,8 unmodified lines# If we reach here, all providers failedmsg = str(last_error) if last_error else "Unknown error"if "404" in msg:raise gr.Error("Model not found. Ensure 'FLUX.1-Krea-dev' is accessible with your HF token.")if "503" in msg:raise gr.Error("The model is warming up. Please try again shortly.")if "401" in msg or "403" in msg:1 unmodified lineraise gr.Error(f"Image generation failed: {msg}")flux_krea_interface = gr.Interface(fn=Flux_Krea_Generate,inputs=[gr.Textbox(label="Prompt", placeholder="Enter a prompt", lines=2),gr.Textbox(label="Negative Prompt",value=(13 unmodified linesgr.Slider(minimum=64, maximum=1216, value=1024, step=32, label="Height"),],outputs=gr.Image(label="Generated Image"),title="Flux Krea (Serverless)",description=("<div style=\"text-align:center\">Generate images with FLUX.1-Krea-dev via Hugging Face Inference. ""No input image required. 'Strength' is omitted. Set HF_READ_TOKEN as needed.</div>"),api_description=("Generate a single image from a text prompt using black-forest-labs/FLUX.1-Krea-dev on Hugging Face Inference. ""Parameters: prompt (str), negative_prompt (str), steps (int, 1–100), cfg_scale (float, 1–20), ""sampler (str, label only), seed (int, -1=random), width/height (int, 64–1216). Returns a PIL.Image."),allow_flagging="never",)# Build tabbed app including Flux Kreademo = gr.TabbedInterface(interface_list=[fetch_interface, concise_interface, code_interface, kokoro_interface, flux_krea_interface],tab_names=["Fetch Webpage","DuckDuckGo Search","Python Code Executor","Kokoro TTS","Flux Krea (Serverless)",],title="Tools MCP",theme="Nymbo/Nymbo_Theme",629 unmodified lines63063163263363463563628 unmodified lines6656666676686696706716726736746756766776786797 unmodified lines6876886896906916926936946952 unmodified lines69869970070170270370470524 unmodified lines7307317327337347357368 unmodified lines7457467477487497507511 unmodified line75375475575675775875976076176276313 unmodified lines777778779780781782783784785786787788789790791792793794795796797798799800801802803804629 unmodified lines}/* Default: add subtitle under titles */.gradio-container h1::after {content: "Fetch Webpage | Search DuckDuckGo | Code Interpreter | Kokoro TTS | Image Generation";display: block;font-size: 1rem;font-weight: 500;28 unmodified lines)# ==========================# Image Generation (Serverless)# ==========================HF_API_TOKEN = os.getenv("HF_READ_TOKEN")def Image_Generation( # <-- MCP tool #5 (Image Generation)prompt: Annotated[str, "Text description of the image to generate."],model_id: Annotated[str, "Hugging Face model id in the form 'creator/model-name' (e.g., black-forest-labs/FLUX.1-Krea-dev)."] = "black-forest-labs/FLUX.1-Krea-dev",negative_prompt: Annotated[str, "What should NOT appear in the image." ] = ("(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, ""missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, "7 unmodified linesheight: Annotated[int, "Output height in pixels (64–1216, multiple of 32 recommended)."] = 1024,) -> Image.Image:"""Generate a single image from a text prompt using a Hugging Face model viaserverless Inference. Returns a PIL image. By default, the model isblack-forest-labs/FLUX.1-Krea-dev.Notes (MCP):- Per the latest Gradio MCP docs, images returned from tools are handled by the server and2 unmodified linesArgs:prompt (str): Text description of the image to generate.model_id (str): The Hugging Face model id (creator/model-name). Defaults to"black-forest-labs/FLUX.1-Krea-dev".negative_prompt (str): What should NOT appear in the image.steps (int): Number of denoising steps (1–100). Higher can improve quality.cfg_scale (float): Guidance scale (1–20). Higher = follow the prompt more closely.24 unmodified linesimage = client.text_to_image(prompt=enhanced_prompt,negative_prompt=negative_prompt,model=model_id,width=width,height=height,num_inference_steps=steps,8 unmodified lines# If we reach here, all providers failedmsg = str(last_error) if last_error else "Unknown error"if "404" in msg:raise gr.Error(f"Model not found or unavailable: {model_id}. Check the id and your HF token access.")if "503" in msg:raise gr.Error("The model is warming up. Please try again shortly.")if "401" in msg or "403" in msg:1 unmodified lineraise gr.Error(f"Image generation failed: {msg}")image_generation_interface = gr.Interface(fn=Image_Generation,inputs=[gr.Textbox(label="Prompt", placeholder="Enter a prompt", lines=2),gr.Textbox(label="Model", value="black-forest-labs/FLUX.1-Krea-dev", placeholder="creator/model-name"),gr.Textbox(label="Negative Prompt",value=(13 unmodified linesgr.Slider(minimum=64, maximum=1216, value=1024, step=32, label="Height"),],outputs=gr.Image(label="Generated Image"),title="Image Generation",description=("<div style=\"text-align:center\">Generate images via Hugging Face Inference. ""Default model: black-forest-labs/FLUX.1-Krea-dev. Set HF_READ_TOKEN as needed.</div>"),api_description=("Generate a single image from a text prompt using a Hugging Face model (serverless Inference). ""Parameters: prompt (str), model_id (str, creator/model-name), negative_prompt (str), steps (int, 1–100), cfg_scale (float, 1–20), ""sampler (str, label only), seed (int, -1=random), width/height (int, 64–1216). Returns a PIL.Image."),allow_flagging="never",)# Build tabbed app including Image Generationdemo = gr.TabbedInterface(interface_list=[fetch_interface, concise_interface, code_interface, kokoro_interface, image_generation_interface],tab_names=["Fetch Webpage","DuckDuckGo Search","Python Code Executor","Kokoro TTS","Image Generation",],title="Tools MCP",theme="Nymbo/Nymbo_Theme",
app.py+22-17