dsdanielpark / Gemini-API

The unofficial python package that returns response of Google Gemini through cookie values.
https://pypi.org/project/python-gemini-api/
MIT License
160 stars 14 forks source link

The response text is a empty URL, which cannot be seen. {'metadata': ['c_cd648c1c943cb00f', 'r_f093111e78855c35'], 'candidates': {'rcid': 'rc_97ec8a9d7ddbed62', 'text': 'http://googleusercontent.com/card_content/0', 'web_images': [], 'generated_images': []}} #14

Open LuxerIThink opened 7 months ago

LuxerIThink commented 7 months ago

Describe the bug The response text is returned in a URL format that cannot be seen.

Version OS: Windows 11 Python: 3.12.2 Gemini API: 1.0.4 Using proxy: No Region: Poland

Code

from gemini import Gemini

cookies = {
    "__Secure-1PSID" : "fullfilled",
    "__Secure-1PSIDTS" : "fullfilled",
    "__Secure-1PSIDCC" : "fullfilled", # I use this because in BardCookies it works
    "NID" : "fullfilled",
  }

GeminiClient = Gemini(cookies=cookies)

prompt = "Hello, Gemini. What's the weather like in Seoul today?"
response = GeminiClient.generate_content(prompt)
print(response)

Error

{'metadata': ['c_cd648c1c943cb00f', 'r_f093111e78855c35'], 'candidates': {'rcid': 'rc_97ec8a9d7ddbed62', 'text': 'http://googleusercontent.com/card_content/0', 'web_images': [], 'generated_images': []}}
dsdanielpark commented 7 months ago

Hello, LuxerIThink

This has already been developed and will be updated today.

It will be provided as asynchronous code that can save to a bytetype image or path. Thank you for your valuable feedback!

opnoice commented 6 months ago

Getting same error

dsdanielpark commented 6 months ago

Getting same error

Please, try to this code.

from gemini import Gemini, GeminiImage

response = GeminiClient.generate_content("Create illustrations of Seoul, South Korea.")
generated_images = response.generated_images # Check generated images [Dict]

GeminiImage.save_sync(generated_images, save_path="cached")

# You can use byte type image dict for printing images as follow:
bytes_images_dict = GeminiImage.fetch_images_dict_sync(generated_images, cookies) # Get bytes images dict
from IPython.display import display, Image
import io

for image_name, image_bytes in bytes_images_dict.items():
    print(image_name)
    image = Image(data=image_bytes)
    display(image)

# GeminiImage.save_images_sync(bytes_images_dict, path="cached") # Save to path
LuxerIThink commented 6 months ago

Getting same error

Please, try to this code.

from gemini import Gemini, GeminiImage

response = GeminiClient.generate_content("Create illustrations of Seoul, South Korea.")
generated_images = response.generated_images # Check generated images [Dict]

GeminiImage.save_sync(generated_images, save_path="cached")

# You can use byte type image dict for printing images as follow:
bytes_images_dict = GeminiImage.fetch_images_dict_sync(generated_images, cookies) # Get bytes images dict
from IPython.display import display, Image
import io

for image_name, image_bytes in bytes_images_dict.items():
    print(image_name)
    image = Image(data=image_bytes)
    display(image)

# GeminiImage.save_images_sync(bytes_images_dict, path="cached") # Save to path

Code you give us in here can't work (because cookies are not defined), but this is problem with texts not with images (I don't tested them).

dsdanielpark commented 6 months ago

LuxerIThink, opnoice

Possible Solutions

Most of all, try rerunning the generate_content method without redeclaring the Gemini object.

  1. Initial Empty Response: Check if the first response is empty due to initial data fetching by the application. Retry after a short wait.

    • The initial response to your request might be empty, possibly due to the Gemini application fetching initial data. This seems to involve some operation by internal middleware or routers, indicated by fetching values from URLs starting with 'execute', leading to the first response being empty.
  2. Premature Response: Wait for a minute after the first attempt if the server settings might not be complete. Avoid redeclaring the Gemini object.

    • This might occur if Gemini's web UI hasn't completed server settings, fetching a premature response. Wait about a minute after the first attempt before running the generate_content method again. Ensure not to redeclare the Gemini object, as repeated object declarations or exporting cookies can lead to 'undefined' cookie values and errors. It's suspected that internal interfaces might reset cookies upon detection, possibly through a RotateCookie process.
  3. Logout and Restart: Log out from all synchronized Chrome accounts, cancel synchronization, and restart the browser.

    • Log out of all synchronized accounts in Chrome, cancel synchronization, and restart your browser. Synced accounts might prevent proper operation.
  4. Cookie Sync Issues: If the same cookies work on one PC but not another, try a fresh browser environment or delete all cookies before accessing Gemini again.

    • Cookie values might sync with the browser's metadata. The same cookies worked on one PC but not another, despite identical inputs. One PC could use the same cookie for over a month. Try executing your code on a completely fresh Chrome environment or manually delete all cookies before accessing Gemini. If issues persist, consider reinstalling the browser.
  5. New Account or Region: Create a new Google account or set a new default country in the account settings.

  6. Valid Cookies in Different Environments: Try using a different Google account in a new browser if valid cookies do not work.

  7. Few Valid Cookies: Input a small number of valid cookie values as suggested on the authentication section, may require country-specific experimentation.

  8. Other User Solutions: Look at solutions provided by other users on the issue page.

If you've found a suitable solution to this issue, please share your experience on this issue page. Thank you.

dsdanielpark commented 6 months ago

Hey, guys!

Make sure you're using version 2.4.2 or later and manually pass the complete cookie (avoid using auto_cookie).

Please test this script with the complete set of cookies collected manually: https://github.com/dsdanielpark/Gemini-API/blob/main/scripts/sample.ipynb

dsdanielpark commented 5 months ago

Using VPN or public IP, facing temporary blocks like 429 errors (due to repeated requests or authentication attempts) can disrupt the SSL/TLS handshake, resulting in an empty response. This issue isn't limited to specific accounts and might require IP-based circumvention.

fredmo commented 4 months ago

Still with the return message :

{'metadata': ['c_c4d1c4aed0cb5eXX', 'r_3214ea893c673dXX'], 'prompt_class': None, 'prompt_candidates': [], 'candidates': [{'rcid': 'rc_5fbed4cbaf9e71XX', 'text': 'http://googleusercontent.com/card_content/0', 'code': {}, 'web_images': [], 'generated_images': []}]}