Make Your Own AI Image Generator with Bria 2.3 Model

Gourav Lohar Last Updated : 30 Oct, 2024
8 min read

Bria AI is a generative AI platform for the production of professional-grade visual content, mainly for enterprises. Established in 2020, they have the tools there, including text-to-image generation, editing with inpainting, background removal, and more. They design their models with responsible AI use in mind, utilizing licensed data to ensure compliance and ethical practices. They even made a partnership with Getty Images to boost the production of visual content for businesses with AI-driven tools. In this article , we will learn to make AI image generator with Bria 2.3.

Make Your Own AI Image Generator with Bria 2.3 Model
Source: Bria 2.3

Learning Outcomes

  • Understand the capabilities of Bria AI for automating high-quality visual content production in various industries.
  • Learn how to leverage Bria AI’s features, such as text-to-image generation, background removal, and inpainting, for enhanced creativity.
  • Explore the ethical use of AI in visual content creation and the importance of licensed data in Bria AI’s operations.
  • Discover how Bria 2.3 model improves image generation speed and quality, making it suitable for enterprise-level applications.
  • Gain insights into the integration options for Bria AI, including API access and platforms like Hugging Face for developers.

This article was published as a part of the Data Science Blogathon.

What is Bria AI?

Bria AI is an enterprise-focused platform employing generative AI in order to automate high quality visual content. With the constant demand for images and videos from businesses and creative professionals, Bria has streamlined the process to produce high-level content at scale. At its core is the democratization of creativity for huge companies and small startups, alike, and makes it easy and accessible.

What sets Bria apart is a very robust commitment toward responsible AI. They train their models exclusively on licensed data and ensure that all contributors are fairly compensated for their work. This approach keeps them far away from controversies linked to certain generative AI systems based on unlicensed or copyrighted content.

Bria AI has a different set of tools to suit every distinct creative need. It offers extreme flexibility in terms of customization and allows users to generate as well as modify images using text prompts, swapping backgrounds, and removing undesired elements from an image-known as inpainting. Bria AI thus provides more value by providing businesses that heavily rely on media.

Key Features of Bria AI

Bria AI’s platform offers a wealth of features that simplify and enhance the creative process for businesses. Some of the standout functionalities include:

  • Text-to-Image Generation: One of the core features offered by Bria is text-to-image generation. The user can provide a textual description to the AI, and it will create an image accordingly. This is very helpful for marketing teams or for content creators who want to have certain visuals created but don’t have the time or resources to commission custom photography or design.
  • Background Removal and Replacement: Bria’s AI can remove backgrounds from images with precision, making it easier to isolate subjects or create different versions of the same image with various backgrounds.
  • Inpainting: This feature lets you edit existing images by masking parts of an image with some trivial information. Remove unwanted objects or even make elements in the picture look better without breaking the artistic integrity of creating from scratch.
  • ControlNet: A powerful feature for users who need fine-grained control over image generation, ControlNet allows users to guide the image generation process, giving more specific instructions to the AI.

Exploring Bria 2.3 Model

Bria 2.3 is the most recent model released by Bria AI, and it brings a significant leap forward in the capabilities of visual generative AI. Bria 2.3 incorporates a range of features designed to deliver higher-quality, more detailed, and faster image generation. The newest and probably the coolest applications of AI are in text-to-image generation, and Bria 2.3 shines here. Whether you need marketing materials, social media posts, or product images, Bria 2.3 enables you to easily generate customized images tailored to your needs.

Use Cases for Bria AI and Bria 2.3

Bria AI offers versatile tools and models, including Bria 2.3, that you can apply across various industries and scenarios. Here are some examples of how businesses and professionals are using Bria’s technology:

  • Marketing and Advertising: Bria AI creates campaign-oriented tailored visuals for marketing teams. In this regard, teams can create unique visuals for advertisements, social media, and email marketing through images produced based on text prompts.
  • E-commerce: In online retail, product images have to be high quality; so far, using Bria 2.3 has made it easy for me to generate professional images, remove or replace backgrounds, and even multiple versions of product images, where possible, to enhance their use better.

How to Access Bria 2.3 Model

  • Official Bria AI Platform: Bria provides its tools and models in its platform, You could sign up on their services so you can access their text-to-image generation, background editing, and other features directly through their website.
  • Hugging Face Integration: Bria has integrated its models, including Bria 2.3, on Hugging Face, a popular AI model-sharing platform. You can interact with and use these models via their API or directly through Hugging Face’s user interface. Search for Bria models on Hugging Face by visiting their model hub and looking for Bria AI’s contributions.
How to Access Bria 2.3 Model
image bria
  • API Access: Bria provides access to developers for application integrations or workflow incorporation via APIs. They provide documentation on how to use their APIs in image generation, removing the background of images, and inpainting; therefore, they can easily integrate into websites, apps, or custom tools. To start any of the APIs you have to sign up for API access on their developer portal.
  • NVIDIA: You can access Bria 2.3 Model API using NVIDIA NIM

Let’s See How to Make Image Generation Webapp with Bria 2.3 Model:

  • Get a Bria 2.3 Model API from NVIDIA NIM
  • Install requirements.txt

Get the Full Code in the GitHub Repo.

Step1: Import Required Library

To get started, we need to import the essential libraries that will facilitate our API requests, environment variable management, and the web app interface. The libraries include requests for handling HTTP requests, base64 for decoding image data, dotenv for loading environment variables, os for interacting with the operating system, time for measuring execution duration, and streamlit for creating the web application interface.

import requests
import base64
from dotenv import load_dotenv
import os
import time
import streamlit as st

Step2: Load your API key from .env File

In this step, we load the API key stored in a .env file using the load_dotenv function. This API key is crucial as it allows us to authenticate our requests to the NVIDIA Bria AI 2.3 model. We then set up the base URL for the API endpoint and prepare the necessary headers for our HTTP requests, ensuring that we include our authorization token.

load_dotenv()

invoke_url = "https://ai.api.nvidia.com/v1/genai/briaai/bria-2.3"

api_key = os.getenv('NVIDIA_API_KEY')

headers = {
    "Authorization": f"Bearer {api_key}",
    "Accept": "application/json",
}

The code sets up the base URL and API key to be used for making authenticated calls to Bria AI 2.3 model API through NVIDIA NIM.

Step3: Streamlit Setup

Now, we’ll set up the Streamlit interface for our image generation app. This includes defining the app’s title, creating an input field for users to enter their image prompts, and allowing them to select an aspect ratio. When users click the “Generate Image” button, we will prepare the payload containing the necessary parameters for the API call, including the prompt, aspect ratio, and other configuration settings.

st.title("Bria Image Generation App")

prompt = st.text_input("Enter Your Image Prompt Here:")
aspect_ratio = st.selectbox("Aspect Ratio", ["1:1", "16:9", "4:3"])

if st.button("Generate Image"):
    payload = {
        "prompt": prompt,
        "cfg_scale": 5,
        "aspect_ratio": aspect_ratio,
        "seed": 0,
        "steps": 30,
        "negative_prompt": ""
    }
    
    start_time = time.time()

    response = requests.post(invoke_url, headers=headers, json=payload)

    end_time = time.time()

This is a simple interface of the Webapp. After entering a text prompt, aspect ratio, and selecting “Generate Image”, an image is generated. This payload consists of the prompt, configuration settings, an aspect ratio, a fixed seed for consistency, number of generation steps, and an optional negative prompt. All these parameters are sent to the Bria API to generate the image according to the user’s input and the response time is calculated after user give the prompt.

Step4: Decoding base64 Image

After sending the API request, this step focuses on handling the response. We check for any errors, decode the base64-encoded image data received from the API, and save it as a PNG file. If the image is successfully generated, it is displayed on the Streamlit interface with a success message. Additionally, we calculate and display the response time for the image generation process to provide users with feedback on the app’s performance.

response.raise_for_status()
    response_body = response.json()
    image_data = response_body.get('image')

    if image_data:
        image_bytes = base64.b64decode(image_data)
        with open('generated_image.png', 'wb') as image_file:
            image_file.write(image_bytes)
        st.image('generated_image.png', caption='Generated Image')
        st.success("Image saved as 'generated_image.png'")
    else:
        st.error("No image data found in the response")

    response_time = end_time - start_time
    st.write(f"Response time: {response_time} seconds")

This code reads the response from the image generation API, saves, and displays the image created. It then looks for errors, decodes any base64 image data present, saves it under generated_image.png, and presents it in Streamlit as a success message. It will display an error otherwise if no image data has been found. The response time of the API will be calculated and shown finally.

Full Code

Incorporating all the steps we’ve discussed, the complete code integrates the libraries, loads the API key, sets up the user interface, and processes the API response to generate and display an image based on user input. This structured approach allows for a seamless experience in generating images using the Bria AI model.

import requests
import base64
from dotenv import load_dotenv
import os
import time
import streamlit as st

load_dotenv()

invoke_url = "https://ai.api.nvidia.com/v1/genai/briaai/bria-2.3"

api_key = os.getenv('NVIDIA_API_KEY')

headers = {
    "Authorization": f"Bearer {api_key}",
    "Accept": "application/json",
}

st.title("Bria Image Generation App")

prompt = st.text_input("Enter Your Image Prompt Here:")
aspect_ratio = st.selectbox("Aspect Ratio", ["1:1", "16:9", "4:3"])

if st.button("Generate Image"):
    payload = {
        "prompt": prompt,
        "cfg_scale": 5,
        "aspect_ratio": aspect_ratio,
        "seed": 0,
        "steps": 30,
        "negative_prompt": ""
    }

    start_time = time.time()

    response = requests.post(invoke_url, headers=headers, json=payload)

    end_time = time.time()

    response.raise_for_status()
    response_body = response.json()
    image_data = response_body.get('image')

    if image_data:
        image_bytes = base64.b64decode(image_data)
        with open('generated_image.png', 'wb') as image_file:
            image_file.write(image_bytes)
        st.image('generated_image.png', caption='Generated Image')
        st.success("Image saved as 'generated_image.png'")
    else:
        st.error("No image data found in the response")

    response_time = end_time - start_time
    st.write(f"Response time: {response_time} seconds")
bria image generator

Prompt

A cozy café scene with a close-up of a steaming coffee cup on a rustic wooden table, surrounded by coffee beans, a croissant, and a soft, warm light filtering through a window, conveying comfort and quality

Output

Output

Output Response time: 3.992785426879541 seconds

Conclusion

Bria AI, through its model Bria 2.3, is transforming visual content for businesses and creators. It has established itself as a leading brand in enterprise-level image generation using generative AI. Bria AI emphasizes responsible use of AI, highly customized features, and fast processing. Whether in marketing, e-commerce, content creation, or design, Bria AI offers options and capabilities to create stunning visuals tailored to your specific needs.

Key Takeaways

  • Import necessary libraries for API requests, environment management, and web app development to facilitate image generation.
  • Load your API key securely from a .env file to authenticate requests to the NVIDIA Bria AI model.
  • Create an intuitive Streamlit interface for users to enter image prompts and select aspect ratios seamlessly.
  • Implement error checking and base64 decoding to save and display generated images while measuring API response time.
  • Combine all components into a cohesive app that efficiently generates and showcases images based on user input.

Frequently Asked Questions

Q1. What is Bria 2.3, and how does it differ from other image generation models?

A. Bria 2.3 is an advanced text-to-image AI model focusing on high-quality, customizable visuals for businesses. It stands out with features like ControlNet and ethical data practices.

Q2. Is Bria 2.3 suitable for large-scale enterprise use?

A. Yes, Bria 2.3 is designed specifically for enterprise applications, featuring API access and bulk processing options. Its rapid generation speeds make it ideal for businesses that require high volumes of visuals.

Q3. How can I access Bria 2.3 if I want to integrate it into my application?

A. You can access Bria 2.3 through their website, API documentation, NVIDIA NIM, or via Hugging Face. This flexibility allows developers to seamlessly incorporate Bria’s tools into custom applications.

Q4. How does ControlNet enhance image customization in Bria 2.3?

A. ControlNet enables precise control over output images by managing details like layout and lighting.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Hi I'm Gourav, a Data Science Enthusiast with a medium foundation in statistical analysis, machine learning, and data visualization. My journey into the world of data began with a curiosity to unravel insights from datasets.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details