Comments (41)
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.
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.
Change all references of bard.google.com
to gemini.google.com
in your code and you should be back in business.
from bard-api.
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.
When we can expect a update from developer? So that we update the current API.
from bard-api.
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.
from bard-api.
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)
from bard-api.
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.
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.
@J-Fo-S I tried using Edge but it does not work. It works seamlessly through Firefox.
from bard-api.
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.
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)
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.
from bard-api.
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.
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.
Just tried it out, got this error
Response status code is not 200. Response Status is 429
from bard-api.
I am also receiving errors.
from bard-api.
Change all references of
bard.google.com
togemini.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.
from bard-api.
\
from bard-api.
@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.
@aniruddhkrish
I put the files in the folder already. But I still got the error: requests.exceptions.TooManyRedirects: Exceeded 30 redirects.
from bard-api.
@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 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.
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.
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.
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.
Did you unzip the updated files and replaced the original ones in the appropriate folder with them?
from bard-api.
@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.
Thanks.
Exception: Response status code is not 200. Response Status is 429
from bard-api.
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.
You can also try using edge by removing firefox.
from bard-api.
@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.
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.
@sapycola its working for me. I imported POST_ENDPOINT
from bard-api.
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.
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.
@dsdanielpark You need not to use the NID cookie. As you said, it is creating problems
from bard-api.
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.
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.
[NOTICE] Please, go to Gemini-API https://github.com/dsdanielpark/Gemini-API
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)
- ChatBard: token_from_browser HOT 9
- ImportError: cannot import name 'BardCookies' from partially initialized module 'bardapi' (most likely due to a circular import) HOT 6
- Some code errors in README examples , os module import HOT 4
- Bard __SECURE-1PSID does not match HOT 2
- Exception: SNlM0e value not found. Double-check __Secure-1PSID value or pass it as Bard(token='xxxxx') HOT 1
- Google changed the the way "__Secure-1PSID" looks, how to use this package effectively,the cookie values does not end in '.' period now HOT 8
- Multiple-Cookies has been changed. HOT 15
- Image generation HOT 4
- Gemini Ultra? HOT 4
- TooManyRedirects: Exceeded 30 redirects. HOT 1
- Check out this Bard/Gemini project. HOT 2
- Google changed their __Secure-1PSID value stracture HOT 2
- ask_about_image Not working HOT 2
- Cookies refreshing very often and getting 429 all the time HOT 2
- How can automaticly get cookies value in no gui servers? HOT 2
- Please, go to Gemini-API `https://github.com/dsdanielpark/Gemini-API`
- a conversation towards getting advance AI HOT 1
- Reflection on the Bard API Project
- Connecting Geminei to Ads HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bard-api.