Image Generation

Module

End-to-end image generation module: receives the API request, generates the images with Gemini, writes the record to MongoDB and the files to Supabase Storage, and returns the URLs.

On this page

    Source: src/image_generation/module.py. This page's original notebook was empty; the content was written from the code.

    #Overview

    The file defines two classes. RequestProcessor validates what arrives with the request (JSON configuration and uploaded images). ImageGenerate runs the image generation flow using ImageGeneratorService and PayloadBuilder, and persists the result.

    The generation endpoint in src/web_services_network/routes/davinci.py uses both classes.

    #RequestProcessor

    Receives the configuration as JSON text (config) and the list of files (files), both optional. The async method process() returns a dictionary with the keys config (dictionary) and image_bytes (list of bytes).

    StepRuleError
    _process_configParses the config text with json.loads. Without config, nothing is done.HTTPException with status 400 when the JSON is invalid.
    _process_filesAccepts only image/jpeg and image/png, with a 10 MB limit (max_mb=10), through FilesPayloadBuilder.RuntimeError with the message "Erro ao carregar as imagens".

    #ImageGenerate

    The constructor takes user_input (required), instructions, config and image_bytes. The methods are:

    MethodDescription
    generateCreates an ImageGeneratorService, builds the parts and the configuration, calls the model and parses the responses.
    build_payloadsUses PayloadBuilder with a jobId generated by IDGenerator.timestamp(prefix="job_") and returns mongo_payload, storage_payload and response_payload.
    save_to_mongoDBWrites the mongo_payload to DATABASE_NAME / COLLECTION_NAME with DocumentStore.
    save_to_supabaseUploads each image in the storage_payload to the bucket with StorageRepository.upload_to_supabase, using the image id as the file name.
    save_results_localSaves the generated images to the local img_test folder, for testing. It is not part of the runner.
    runnerRuns the full flow and returns the response_payload.

    #Runner flow

    1. generate: generates the images and aggregates the responses.
    2. build_payloads: splits the result into three payloads.
    3. save_to_mongoDB: persists the record (without the image bytes).
    4. save_to_supabase: persists the image files.
    5. Returns the response_payload, with text_response and the list of URLs in images.

    #Usage example

    from src.image_generation.module import ImageGenerate
    
    # Create the generator with the prompt and the optional configuration
    generator = ImageGenerate(
        user_input="Uma borboleta no estilo de Da Vinci",
        instructions=None,
        config={"aspect_ratio": "16:9", "number_of_images": 2},
        image_bytes=None,
    )
    
    # Run the full flow and receive the URLs
    response = generator.runner()
    print(response["images"])

    #Notes

    Generic errors. build_payloads, save_to_mongoDB and save_to_supabase catch any exception and raise a RuntimeError with a fixed message, without chaining the original cause.

    The supporting modules are documented in Image Generator Service, Payload Builder and Config.

    Source: src/image_generation/module.py

    Esc
    ↑↓navigate Enteropen