Skip to main content

The python package that returns Response of Google Gemini through API.

Project description

Development Status :: 3 - Alpha

Gemini Icon Gemini API

A Python wrapper, python-gemini-api, interacts with Google Gemini via reverse engineering.


What is Gemini?

Gemini is a family of generative AI models developed by Google DeepMind that is designed for multimodal use cases. The Gemini API gives you access to the Gemini Pro and Gemini Pro Vision models. In February 2024, Google"s Gemini service was changed to Gemini.
Paper, Official Web, Open-source Gemma-7b

Installation

pip install python-gemini-api
pip install git+https://github.com/dsdanielpark/Gemini-API.git

Authentication

Warning DO NOT expose your cookies.

  • Depending on the region and Google account status, multiple cookies may be required, including __Secure-1PSIDTS, __Secure-1PSIDCC, __Secure-1PSID, NID, or more.
  • You must appropriately set the cookies_dict parameter to Gemini class.
  • Cookie values can be changed frequently, thus it is recommended to automatically update them through the browser_cookie3 package.

Usage

After Bard's update to Gemini, multiple cookies, often updated, are needed based on region or Google account. Thus, automatic cookie renewal logic is crucial.

Innitiallization

from gemini import Gemini

cookies = {
    "__Secure-1PSID": "value",
    "__Secure-1PSIDTS": "value",
    "__Secure-1PSIDCC": "value",
    "NID": "value",
}

GeminiClient = Gemini(cookies=cookies)

Or update cookies automatically using broser_cookie3.

from gemini import Gemini

GeminiClient = Gemini(auto_cookies=True)

However, auto_cookies feature is incomplete, and you may need to periodically update cookie values in a .env file or json file. You will need to develop a logic that suits you for automatically updating cookies.

Before proceeding, ensure that the GeminiClient object is defined without any errors.

Text generation

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

Image generation

prompt = "Hello, Gemini. Give me a beautiful photo of Seoul's scenery."
response = GeminiClient.generate_content(prompt)

print("\n".join(response.images)) # Print images

for i, image in enumerate(response.images): # Save images
    image.save(path="folder_path/", filename=f"seoul_{i}.png")

Generate content about image

It may not work as it is only available for certain accounts, regions, and other restrictions. As an experimental feature, it is possible to ask questions with an image. However, this functionality is only available for accounts with image upload capability in Gemini"s web UI.

prompt = "What is in the image?"
image = open("folder_path/image.jpg", "rb").read() # (jpeg, png, webp) are supported.

response = GeminiClient.generate_content(prompt, image)

Text To Speech(TTS) from Gemini

Business users and high traffic volume may be subject to account restrictions according to Google"s policies. Please use the Official Google Cloud API for any other purpose. The user is solely responsible for all code, and it is imperative to consult Google"s official services and policies. Furthermore, the code in this repository is provided under the MIT license, and it disclaims any liability, including explicit or implied legal responsibilities.

text = "Hello, I'm developer in seoul" # Gemini will speak this sentence
response = GeminiClient.generate_content(prompt, image)
audio = GeminiClient.speech(text)
with open("speech.ogg", "wb") as f:
    f.write(bytes(audio["audio"]))

Further

Behind a proxy

If you are working behind a proxy, use the following.

proxies = {
    "http": "http://proxy.example.com:8080",
    "https": "https://proxy.example.com:8080"
}

GeminiClient = Gemini(cookies=cookies, proxies=proxies, timeout=30)
GeminiClient.generate_content("Hello, Gemini. Give me a beautiful photo of Seoul"s scenery.")

Use rotating proxies

If you want to avoid blocked requests and bans, then use Smart Proxy by Crawlbase. It forwards your connection requests to a randomly rotating IP address in a pool of proxies before reaching the target website. The combination of AI and ML make it more effective to avoid CAPTCHAs and blocks.

# Get your proxy url at crawlbase https://crawlbase.com/docs/smart-proxy/get/
proxy_url = "http://xxxxx:@smartproxy.crawlbase.com:8012" 
proxies = {"http": proxy_url, "https": proxy_url}

GeminiClient = Gemini(cookies=cookies, proxies=proxies, timeout=30)
GeminiClient.generate_content("Hello, Gemini. Give me a beautiful photo of Seoul's scenery.")

Reusable session object

You can continue the conversation using a reusable session. However, this feature is limited, and it is difficult for a package-level feature to perfectly maintain conversation_id and context. You can try to maintain the consistency of conversations same way as other LLM services, such as passing some sort of summary of past conversations to the DB.

from gemini import Gemini, SESSION_HEADERS
import requests

cookies = {
    "__Secure-1PSID": "value",
    "__Secure-1PSIDTS": "value",
    "__Secure-1PSIDCC": "value",
    "NID": "value",
}

session = requests.Session()
session.headers = SESSION_HEADERS
session.cookies.update(cookies)

GeminiClient = Gemini(session=session, timeout=30)
response = GeminiClient.generate_content("Hello, Gemini. What's the weather like in Seoul today?")["content"]

# Continued conversation without set new session
response = GeminiClient.generate_content("What was my last prompt?")

More features


FAQ

You can find most help on the FAQ and Issue pages. Alternatively, utilize the official Gemini API at Google AI Studio.

Issues

Sincerely grateful for any reports on new features or bugs. Your valuable feedback on the code is highly appreciated. Frequent errors may occur due to changes in Google"s service API interface. Both Issue reports and Pull requests contributing to improvements are always welcome. We strive to maintain an active and courteous open community.

Contributors

We would like to express my sincere gratitude to all the contributors.

Contacts

License

MIT license, 2024, Minwoo(Daniel) Park. We hereby strongly disclaim any explicit or implicit legal liability related to our works. Users are required to use this package responsibly and at their own risk.

References

[1] Github acheong08/Gemini
[2] Github GoogleCloudPlatform/generative-ai
[3] Github HanaokaYuzu/Gemini-API
[4] Google AI Studio

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-gemini-api-0.1.1.tar.gz (26.3 kB view details)

Uploaded Source

Built Distribution

python_gemini_api-0.1.1-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

Details for the file python-gemini-api-0.1.1.tar.gz.

File metadata

  • Download URL: python-gemini-api-0.1.1.tar.gz
  • Upload date:
  • Size: 26.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for python-gemini-api-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2819baf16281695827ba010ca9650cac25bd66ca9d1baae3a63351736ec775f2
MD5 24afd8f55df573e4eb81009ef63a4ab5
BLAKE2b-256 7266825bf68356d419ce067b8673da588a424c3ce3de870295910d33b6e3e3d8

See more details on using hashes here.

File details

Details for the file python_gemini_api-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_gemini_api-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5266eb11e9fb2dcd36399105e97e8a7382cf08b07aaff3beebdfaa8d8d7eac7d
MD5 c6db276abb5d3cac1baaf1bee82d8b6c
BLAKE2b-256 995867cb0ac2154db05092a3d65295781c372938171bc19e48ea85b0e16250ad

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page