Git Product home page Git Product logo

Comments (41)

irfanbd2 avatar irfanbd2 commented on June 2, 2024 10

1.Change in core_cookies.py "https://bard.google.com/" TO "https://gemini.google.com/app" and change in core.py bard.google to gemini.google

2.Make sure to pass four cookie __Secure-1PSID, __Secure-1PSIDTS,NID and GOOGLE_ABUSE_EXEMPTION This four are main cookie

and change _get_session to

def _get_session(self, session: Optional[requests.Session]) -> requests.Session:
    """
    Get the requests Session object.

    Args:
        session (requests.Session): Requests session object.

    Returns:
        requests.Session: The Session object.
    """

    if session is None:
        new_session = requests.Session()
        cookie = ""
        for k, v in self.cookie_dict.items():
            cookie += f"{k}={v}; "
        new_session.headers.update({"Cookie": cookie})
        return new_session
    else:
        return session

That's it then It will work perfectly

from bard-api.

dsdanielpark avatar dsdanielpark commented on June 2, 2024 9

We are currently working on modifying the previous bardapi to make it usable on Gemini. Both this repository and the gemini-api repository will be synced for a while to minimize confusion for users.

Once the work is complete, we will release bardapi version 0.1.40 and python-gemini-api version 0.1.2. Thank you.

from bard-api.

bluemods avatar bluemods commented on June 2, 2024 4

Change all references of bard.google.com to gemini.google.com in your code and you should be back in business.

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024 4

@FriedaMarvin

Please find the updated files
Updated files.zip

Unzip and paste the files in the appropriate folder.

For any assistance, don't hesitate to comment here.

from bard-api.

sambitweeting avatar sambitweeting commented on June 2, 2024 3

When we can expect a update from developer? So that we update the current API.

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024 3

@irfanbd2

Thank you for your solution. Only one thing to update when you create the session headers, I mean user agent, you have to change the host from bard.google.com to gemini.google.com. I did it and is working for me. Thank you once again.
Screenshot 2024-02-09 200555

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024 3

@Goodday777

I have found the solution for this.

But I think this only works with Firefox browser.

Here are the steps to use the latest Gemini AI through Bardapi with some kind of stability.

Install Firefox.

Sign in to Gemini (formerly Bard) with your google account credentials.

Unzip the updated files which are attached here and place it in the appropriate folders

Then you will be able to use bardapi seamlessly with the following code. Every time you run this code, it will extract the new cookies from firefox and send it to bardapi.
Make sure to pip install browser_cookie3.

import requests
from bardapi import SESSION_HEADERS, BardCookies
import browser_cookie3

domain = '.google.com'

firefox_cookies = browser_cookie3.firefox(domain_name=domain)

psid = '__Secure-1PSID'
dts = '__Secure-1PSIDTS'
dcc = '__Secure-1PSIDCC'
nid = 'NID'

psid_value = None
dts_value = None
dcc_value = None
nid_value = None

for cookie in firefox_cookies:
    if cookie.name == psid:
        psid_value = cookie.value
    elif cookie.name == dts:
        dts_value = cookie.value
    elif cookie.name == dcc:
        dcc_value = cookie.value
    elif cookie.name == nid:
        nid_value = cookie.value


cookie_dict = {
    "__Secure-1PSID": psid_value,
    "__Secure-1PSIDTS": dts_value,
    "__Secure-1PSIDCC": dcc_value,
}
session = requests.Session()
session.cookies.set("__Secure-1PSID", psid_value)
session.cookies.set( "__Secure-1PSIDCC", dcc_value)
session.cookies.set("__Secure-1PSIDTS", dts_value)
session.cookies.set("NID",nid_value)
session.headers = SESSION_HEADERS

bard = BardCookies(session=session,cookie_dict=cookie_dict)

while True:

    question = input("Enter your prompt to Gemini: ")
    response = bard.get_answer(question)['content']
    data = str(response).replace("*", "")
    print(data)
    

Updated files.zip

from bard-api.

emanuelaelli avatar emanuelaelli commented on June 2, 2024 2

can you provide me with instructions on how to use bardapi with the gemini update? I tried editing the files and putting the code in firefox but it doesn't work

is there a repository already updated to this version of gemini?

I can't use the API because it is not yet accessible in Italy and I don't have a VPN available

from bard-api.

J-Fo-S avatar J-Fo-S commented on June 2, 2024 1

Here's a new wrinkle. I now obtain a Secure-1PSID that does not end with a dot. When I place the string in my code I get the following error:

Exception: __Secure-1PSID value must end with a single dot. Enter correct __Secure-1PSID value

If I add the dot, it of course does not work. I do notice a dot now appears after the first character of the string, in my case it starts of like:

g.a000

Not sure if this is random or the new placement of the dot.

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024 1

@J-Fo-S I tried using Edge but it does not work. It works seamlessly through Firefox.

from bard-api.

sambitweeting avatar sambitweeting commented on June 2, 2024 1

We are currently working on modifying the previous bardapi to make it usable on Gemini. Both this repository and the gemini-api repository will be synced for a while to minimize confusion for users.

Once the work is complete, we will release bardapi version 0.1.40 and python-gemini-api version 0.1.2. Thank you.

Let us know once done.

from bard-api.

dsdanielpark avatar dsdanielpark commented on June 2, 2024 1

@Goodday777

I have found the solution for this.

But I think this only works with Firefox browser.

Here are the steps to use the latest Gemini AI through Bardapi with some kind of stability.

Install Firefox.

Sign in to Gemini (formerly Bard) with your google account credentials.

Unzip the updated files which are attached here and place it in the appropriate folders

Then you will be able to use bardapi seamlessly with the following code. Every time you run this code, it will extract the new cookies from firefox and send it to bardapi. Make sure to pip install browser_cookie3.

import requests
from bardapi import SESSION_HEADERS, BardCookies
import browser_cookie3

domain = '.google.com'

firefox_cookies = browser_cookie3.firefox(domain_name=domain)

psid = '__Secure-1PSID'
dts = '__Secure-1PSIDTS'
dcc = '__Secure-1PSIDCC'
nid = 'NID'

psid_value = None
dts_value = None
dcc_value = None
nid_value = None

for cookie in firefox_cookies:
    if cookie.name == psid:
        psid_value = cookie.value
    elif cookie.name == dts:
        dts_value = cookie.value
    elif cookie.name == dcc:
        dcc_value = cookie.value
    elif cookie.name == nid:
        nid_value = cookie.value


cookie_dict = {
    "__Secure-1PSID": psid_value,
    "__Secure-1PSIDTS": dts_value,
    "__Secure-1PSIDCC": dcc_value,
}
session = requests.Session()
session.cookies.set("__Secure-1PSID", psid_value)
session.cookies.set( "__Secure-1PSIDCC", dcc_value)
session.cookies.set("__Secure-1PSIDTS", dts_value)
session.cookies.set("NID",nid_value)
session.headers = SESSION_HEADERS

bard = BardCookies(session=session,cookie_dict=cookie_dict)

while True:

    question = input("Enter your prompt to Gemini: ")
    response = bard.get_answer(question)['content']
    data = str(response).replace("*", "")
    print(data)
    

Updated files.zip

When setting four cookie values as session cookie values, it does not execute properly. Could it be that someone succeeded by adding additional cookie values in the Firefox browser? If anyone has succeeded with different cookie values, please let me know. If you have any experimental code that works correctly, please share it. It would greatly help with the update.

Alternatively, if needed, anyone can submit a pull request at any time.

Thank you.

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024 1

from bard-api.

sambitweeting avatar sambitweeting commented on June 2, 2024 1

Error even after using updated file for gemini

        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\bardapi\core.py", line 332, in get_answer
    resp_dict = json.loads(resp.content.splitlines()[-5])[0][2]
                           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

Do you use NID cookie? If yes, remove it. It worked for me.

Yes I use NID Cookie. I will try it removing NID

from bard-api.

OlegRuban-ai avatar OlegRuban-ai commented on June 2, 2024

as for me, this doesn't work now, according to the code all requests go to bard.google.com and are not sent to gemini.google.com

from bard-api.

FriedaMarvin avatar FriedaMarvin commented on June 2, 2024

Just tried it out, got this error
Response status code is not 200. Response Status is 429

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

I am also receiving errors.

from bard-api.

zabeiua avatar zabeiua commented on June 2, 2024

Change all references of bard.google.com to gemini.google.com in your code and you should be back in business.

This should do the developer, i suppose. We, users, are waiting to pip install -U bardapi

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

\

from bard-api.

FriedaMarvin avatar FriedaMarvin commented on June 2, 2024

@irfanbd2 I changed the two files, also changed _get_session in the core.py file. But can you help me with this:
2.Make sure to pass four cookie __Secure-1PSID, __Secure-1PSIDTS,NID and GOOGLE_ABUSE_EXEMPTION This four are main cookie

Which line should i change in core.py file for this?

If possible can you share the updated core.py and core_cookies.py files.

Thanks

from bard-api.

jacktse0225 avatar jacktse0225 commented on June 2, 2024

@aniruddhkrish
I put the files in the folder already. But I still got the error: requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

from bard-api.

eienmojiki206 avatar eienmojiki206 commented on June 2, 2024

@aniruddhkrish I put the files in the folder already. But I still got the error: requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

You can try changing "/app" to "/" in Referer of the sessions. It works good for me

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

@aniruddhkrish I put the files in the folder already. But I still got the error: requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

Did you change the session headers?

from bard-api.

Goodday777 avatar Goodday777 commented on June 2, 2024

Previously, if there was only __Secure-1PSID,
If authentication was possible
For now, if you don't have __Secure-1PSIDTS (expiring or changing soon)
It doesn't give at.
Also, in some environments
If GOOGLE_ABUSE_EXEMPTION is not present, access itself is denied.

As we moved from Bard to Gemini, it became difficult to use this function with just the __Secure-1PSID value.

I am of the opinion that it will become difficult in the future if there is no function to automatically update the __Secure-1PSIDTS (expiring or changing soon) value.

from bard-api.

nicolaslr75 avatar nicolaslr75 commented on June 2, 2024

@aniruddhkrish

File "XXXXXX\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\requests\sessions.py", line 166, in resolve_redirects
raise TooManyRedirects('Exceeded {} redirects.'.format(self.max_redirects), response=resp)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

@J-Fo-S

Use this code,

from bardapi import SESSION_HEADERS, BardCookies
import browser_cookie3
import requests
domain = '.google.com'

firefox_cookies = browser_cookie3.firefox(domain_name=domain)

psid = '__Secure-1PSID'
dts = '__Secure-1PSIDTS'
dcc = '__Secure-1PSIDCC'
nid = 'NID'

psid_value = None
dts_value = None
dcc_value = None
nid_value = None

for cookie in firefox_cookies:
    if cookie.name == psid:
        psid_value = cookie.value
    elif cookie.name == dts:
        dts_value = cookie.value
    elif cookie.name == dcc:
        dcc_value = cookie.value
    elif cookie.name == nid:
        nid_value = cookie.value


cookie_dict = {
    "__Secure-1PSID": psid_value,
    "__Secure-1PSIDTS": dts_value,
    "__Secure-1PSIDCC": dcc_value,
}
session = requests.Session()
session.cookies.set("__Secure-1PSID", psid_value)
session.cookies.set( "__Secure-1PSIDCC", dcc_value)
session.cookies.set("__Secure-1PSIDTS", dts_value)
session.cookies.set("NID",nid_value)
session.headers = SESSION_HEADERS

bard = BardCookies(session=session,cookie_dict=cookie_dict)

while True:

    question = input("Enter your prompt to Gemini: ")
    response = bard.get_answer(question)['content']
    data = str(response).replace("*", "")
    print(data)

Pass the 4 cookies. It will work. Do it as done above. Make sure you have installed Firefox and signed in there.

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

@nicolaslr75

Did you unzip the updated files and replaced the original ones in the appropriate folder with them?

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

@nicolaslr75
Attached is the zipped folder of the updated files (core.py, constant, core_cookies.py). Replace the original ones with the ones in the folder of updated files.

Then run the code given above by me in my last comment. Make sure to use a reusable session with session headers.

from bard-api.

nicolaslr75 avatar nicolaslr75 commented on June 2, 2024

Thanks.

Exception: Response status code is not 200. Response Status is 429

from bard-api.

J-Fo-S avatar J-Fo-S commented on June 2, 2024

@J-Fo-S

Use this code,

from bardapi import SESSION_HEADERS, BardCookies
import browser_cookie3
import requests
domain = '.google.com'

firefox_cookies = browser_cookie3.firefox(domain_name=domain)

psid = '__Secure-1PSID'
dts = '__Secure-1PSIDTS'
dcc = '__Secure-1PSIDCC'
nid = 'NID'

psid_value = None
dts_value = None
dcc_value = None
nid_value = None

for cookie in firefox_cookies:
    if cookie.name == psid:
        psid_value = cookie.value
    elif cookie.name == dts:
        dts_value = cookie.value
    elif cookie.name == dcc:
        dcc_value = cookie.value
    elif cookie.name == nid:
        nid_value = cookie.value


cookie_dict = {
    "__Secure-1PSID": psid_value,
    "__Secure-1PSIDTS": dts_value,
    "__Secure-1PSIDCC": dcc_value,
}
session = requests.Session()
session.cookies.set("__Secure-1PSID", psid_value)
session.cookies.set( "__Secure-1PSIDCC", dcc_value)
session.cookies.set("__Secure-1PSIDTS", dts_value)
session.cookies.set("NID",nid_value)
session.headers = SESSION_HEADERS

bard = BardCookies(session=session,cookie_dict=cookie_dict)

while True:

    question = input("Enter your prompt to Gemini: ")
    response = bard.get_answer(question)['content']
    data = str(response).replace("*", "")
    print(data)

Pass the 4 cookies. It will work. Do it as done above. Make sure you have installed Firefox and signed in there.

@aniruddhkrish While this doesn't quite address the error I described while operating on Chrome, I can confirm this solution works on FireFox IF you also manually set the headers as you describe here

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

@J-Fo-S

You can also try using edge by removing firefox.

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

@J-Fo-S While operating chrome, you get that errors because chrome does not permit you to access cookies through browser_cookie3 but firefox does.

from bard-api.

sapycola avatar sapycola commented on June 2, 2024

from bardapi.constants import (
ALLOWED_LANGUAGES,
SESSION_HEADERS,
TEXT_GENERATION_WEB_SERVER_PARAM,
Tool,
POST_ENDPOINT,
)

exception after copy file.zip

ImportError: cannot import name 'POST_ENDPOINT' from 'bardapi.constants' (C:\Python312\Lib\site-packages\bardapi\constants.py)
python-BaseException

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

@sapycola its working for me. I imported POST_ENDPOINT

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

Click that and try changing the POST_ENDPOINT POST_ENDPOINT = "https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate"
to "POST_ENDPOINT = "https://gemini.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate"

from bard-api.

sambitweeting avatar sambitweeting commented on June 2, 2024

Error even after using updated file for gemini

        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\bardapi\core.py", line 332, in get_answer
    resp_dict = json.loads(resp.content.splitlines()[-5])[0][2]
                           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

@dsdanielpark You need not to use the NID cookie. As you said, it is creating problems

from bard-api.

aniruddhkrish avatar aniruddhkrish commented on June 2, 2024

Error even after using updated file for gemini

        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\bardapi\core.py", line 332, in get_answer
    resp_dict = json.loads(resp.content.splitlines()[-5])[0][2]
                           ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^
IndexError: list index out of range

Do you use NID cookie? If yes, remove it. It worked for me.

from bard-api.

dsdanielpark avatar dsdanielpark commented on June 2, 2024

aniruddhkrish

Which cookies do you used? I will start from there. And if you can, please check in this repository latest codes are working to you.

from bard-api.

dsdanielpark avatar dsdanielpark commented on June 2, 2024

[NOTICE] Please, go to Gemini-API https://github.com/dsdanielpark/Gemini-API

Gemini Icon Google - Gemini API

A unofficial Python wrapper, python-gemini-api, operates through reverse-engineering, utilizing cookie values to interact with Google Gemini for users struggling with frequent authentication problems or unable to authenticate via Google Authentication.

Collaborated competently with Antonio Cheong.

What is Gemini?

[Paper] [Official Website] [Official API] [API Documents]

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 Bard service was changed to Gemini.


Installation

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

For the updated version, use as follows:

pip install -q -U python-gemini-api

from bard-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.