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.
This article was published as a part of the Data Science Blogathon.
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.
Bria AI’s platform offers a wealth of features that simplify and enhance the creative process for businesses. Some of the standout functionalities include:
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.
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:
Let’s See How to Make Image Generation Webapp with Bria 2.3 Model:
Get the Full Code in the GitHub Repo.
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
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.
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.
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.
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")
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 Response time: 3.992785426879541 seconds
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.
.env
file to authenticate requests to the NVIDIA Bria AI model.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.
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.
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.
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.