Mohammed Baashar
Mohammed Baashar

Reputation: 555

Google generative ai: gemini not working on android: 403 error

I send my text to "google gemini" using this code in python:

import google.generativeai as genai

# Create your views here.
# add here to your generated API key
genai.configure(api_key="*****")

def ask_question(request):
    if request.method == "POST":
        text = request.POST.get("text")
        try:
            model = genai.GenerativeModel("gemini-1.5-flash")
        except Exception as e:
           print(e)
        chat = model.start_chat()
        try:
            response = chat.send_message(text)
        except Exception as e:
           print(e)
        try:
            response = chat.send_message(text)
        except Exception as e:
           print(e)
        user = request.user
        ChatBot.objects.create(text_input=text, gemini_output=response.text, user=user)
        # Extract necessary data from response
        response_data = {
            "text": response.text,  # Assuming response.text contains the relevant response data
            # Add other relevant data from response if needed
        }
        return JsonResponse({"data": response_data})
    else:
        return JsonResponse({"data": 'I apologise but there is an issue'})

It works perfectly on desktop, but on android phone it throws "403 PERMISSION_DENIED":

https://ai.google.dev/gemini-api/docs/troubleshooting?lang=python

I tried updating generativeai via pip install but it didn't work Any suggestions?

Upvotes: 1

Views: 491

Answers (1)

sam
sam

Reputation: 1896

I have tested the crux of your code. The code seem to be working fine in a fresh environment. That said, a few code recommendations.

# Pip Package info - https://pypi.org/project/google-generativeai/
import google.generativeai as genai


# To generate a key - https://aistudio.google.com/app/apikey
genai.configure(api_key=API_KEY)

model = genai.GenerativeModel("gemini-1.5-flash")
chat = model.start_chat()
response = chat.send_message("how are you")
print(response)

Code recommendations:

  1. Chat sessions are great if you want to continue the conversation on a topic. For one time content generation, use model.generate_content.

  2. Instead of gemini-1.5-flash use gemini-1.5-flash-001 or gemini-1.5-flash-002. This brings clarity for model response, cost and efficiency. If you are building an application, preferable store model name in configuration that is easy to change.

  3. Like Gemini Model names, keep your API Keys in a secure location & configurable location.

About 403 Error:

This error means you are not authorised. As mentioned earlier, I have tested code in a fresh environment. Code is fine and Gemini Services are also working fine. So, the questionable part is your runtime environment and I have the following recommendation for you.

  1. Firewalls or network restrictions on your Android phone or Wi-Fi network could be blocking access to the Gemini API.

  2. The Google Gemini client library you're using might not be fully compatible with the Android platform.

  3. Examine the API Restriction for your API keys

  4. Examine the full 403 error message for any additional details or clues about the cause.

Upvotes: 1

Related Questions