How to Disable Safety Settings in Gemini Vision Pro Model Using API?

Written by- Aionlinecourse458 times views

How to Disable Safety Settings in Gemini Vision Pro Model Using API?

Gemini Vision Pro is a powerful computer vision model developed by Google Deepminnd. Gemini Vision Pro has a lot of uses and real-time applications in recent times. This vision model provides various safety settings to prevent misuse. However, sometimes developers need to disable these safety settings because of some specific application. 


Solution 1:

We can modify the safety setting of the Gemini Vision Pro model as described below:

safe = [
    {
        "category": "HARM_CATEGORY_HARASSMENT",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_HATE_SPEECH",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
        "threshold": "BLOCK_NONE",
    },
    {
        "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
        "threshold": "BLOCK_NONE",
    },
]

Solution 2:

We may have to try few times in a loop because sometimes Gemini through errors:

def generate_content_with_gemini_from_text(prompt):
model = genai.GenerativeModel(        
    model_name="gemini-1.5-flash",
    generation_config=generation_config,
    safety_settings = safe
)
e = None  # Initialize e before the try-except block
for attempt in range(4):
    try:
        response = model.generate_content(prompt)            
        return response.text
    except Exception as caught_exception:
        e = caught_exception  # Update e with the caught exception
        print(f"Error on attempt {attempt + 1}: {e}")
        time.sleep(1)
if e:
    print(f"Failed after 4 retries. Error: {e}")
else:
    print("Failed after 4 retries due to an unknown error.")
return None
We need to send correctly formatted post requests through Gemini Vision Pro API to disable the safety settings.  We must check that the request includes the necessary headers and payload.  By handling the response properly, we can ensure that the safety settings are disabled. These approaches help us to disable the safety settings of the Gemini Vision Pro model using its API.


Thank you for reading the article.