Skip to content
This repository was archived by the owner on Jun 12, 2024. It is now read-only.

Commit 2a60d09

Browse files
committed
2 parents 163a5fd + 990eaf1 commit 2a60d09

File tree

1 file changed

+115
-40
lines changed

1 file changed

+115
-40
lines changed

README.md

Lines changed: 115 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77

88

9-
A *unofficial* Python wrapper, [python-gemini-api](https://pypi.org/project/python-gemini-api/), operates through reverse-engineering, utilizing cookie values to interact with [Google Gemini](https://gemini.google.com) for users struggling with frequent authentication problems or unable to authenticate via [Google Authentication](https://developers.google.com/identity/protocols/oauth2?hl=en).
9+
A *unofficial* Python wrapper, [python-gemini-api](https://pypi.org/project/python-gemini-api/), operates through reverse-engineering, utilizing cookie values to interact with [Google Gemini](https://gemini.google.com) for users struggling with frequent authentication problems or unable to authenticate via [Google Authentication](https://developers.google.com/identity/protocols/oauth2?hl=en). This repository is not expected to be updated frequently, and it will be modified for personal use.
1010

1111
Collaborated competently with [Antonio Cheong](https://github.com/acheong08).
1212

13+
Please, check <img src="https://www.gstatic.com/lamda/images/favicon_v1_150160cddff7f294ce30.svg" width="15px" alt="Gemini Icon" />[HanaokaYuzu/Gemini-API](https://github.com/HanaokaYuzu/Gemini-API) first.
14+
15+
1316
<br>
1417

1518
## What is [Gemini](https://deepmind.google/technologies/gemini/#introduction)?
@@ -102,7 +105,7 @@ GeminiClient = Gemini(cookies=cookies)
102105
103106
<br>
104107

105-
### # 02. Generate Content
108+
### # 02. Generate content
106109
To check regardless of the data type of the model output, return the response_dict argument. And use it appropriately.
107110

108111
```python
@@ -143,10 +146,9 @@ print(response.text)
143146

144147
### # 04. Image generation
145148
Returns images generated by Gemini.
146-
> [!NOTE]
147-
> Use GeminiImage for image processing. `web_images` works without cookies, but for images like `generated_image` from Gemini, pass cookies. Cookies are needed to download images from Google's storage. Check the response or use existing cookies variable.
148149

149-
*Sync*
150+
151+
*Sync downloader*
150152
```python
151153
from gemini import Gemini, GeminiImage
152154

@@ -176,7 +178,7 @@ GeminiImage.save_sync(generated_images, save_path="save_dir", cookies=cookies)
176178
</details>
177179

178180

179-
*Async*
181+
*Async downloader*
180182

181183
```python
182184
response = GeminiClient.generate_content("Create illustrations of Seoul, South Korea.")
@@ -188,22 +190,21 @@ await GeminiImage.save(generated_images, "save_dir", cookies=cookies)
188190
# await GeminiImage.save_images(image_data_dict, "save_dir")
189191
```
190192

191-
<details><summary>Async wrapper</summary>
193+
194+
<details><summary>Async downloader wrapper</summary>
192195

193196
```
194197
import asyncio
195198
from gemini import Gemini, GeminiImage
196199
197-
async def fetch_and_save_images_async(prompt: str, save_path: str="save_dir", cookies=cookies):
198-
response = await GeminiClient.generate_content_async(prompt)
199-
200-
generated_images = response.generated_images # Check response images [Dict]
201-
await GeminiImage.save(generated_images, save_path=save_path, cookies=cookies)
200+
async def save_generated_imagse(generated_imagse, save_path="save_dir", cookies=cookies):
201+
await GeminiImage.save(generated_imagse, save_path=save_path, cookies=cookies)
202202
203203
# Run the async function
204204
if __name__ == "__main__":
205-
user_prompt = input("Enter your prompt: ")
206-
asyncio.run(fetch_and_save_images_async(user_prompt))
205+
cookies = {"key" : "value"}
206+
generated_imagse = response.generated_imagse
207+
asyncio.run(save_generated_imagse(generated_imagse, save_path="save_dir", cookies=cookies))
207208
```
208209

209210
`GeminiImage.save` method logic
@@ -212,28 +213,28 @@ if __name__ == "__main__":
212213
import asyncio
213214
from gemini import Gemini, GeminiImage
214215
215-
async def fetch_and_save_images_async(prompt: str, save_path: str="save_dir", cookies):
216-
response = await GeminiClient.generate_content_async(prompt)
217-
218-
generated_images = response.generated_images # Check response images [Dict]
219-
image_data_dict = await GeminiImage.fetch_images_dict(generated_images, cookies=cookies) # Get bytes images dict asynchronously
220-
await GeminiImage.save_images(image_data_dict, save_path=save_path) # Save to path asynchronously
216+
async def save_generated_imagse(generated_imagse, save_path="save_dir", cookies=cookies):
217+
image_data_dict = await GeminiImage.fetch_images_dict(generated_imagse, cookies=cookies) # Get bytes images dict asynchronously
218+
await GeminiImage.save_images(image_data_dict, save_path=save_path)
221219
222220
# Run the async function
223221
if __name__ == "__main__":
224-
user_prompt = input("Enter your prompt: ")
225-
asyncio.run(fetch_and_save_images_async(user_prompt))
222+
cookies = {"key" : "value"}
223+
generated_imagse = response.generated_imagse # Check response images [Dict]
224+
asyncio.run(save_generated_imagse(generated_imagse, save_path="save_dir", cookies=cookies))
226225
```
227226

228227
</details>
229228

229+
> [!NOTE]
230+
> Use GeminiImage for image processing. `web_images` works without cookies, but for images like `generated_image` from Gemini, pass cookies. Cookies are needed to download images from Google's storage. Check the response or use existing cookies variable.
230231
231232
<br>
232233

233234
### # 05. Retrieving Images from Gemini Responses
234235
Returns images in response of Gemini.
235236

236-
*Sync*
237+
*Sync downloader*
237238
```python
238239
from gemini import Gemini, GeminiImage
239240

@@ -246,7 +247,7 @@ GeminiImage.save_sync(response_images, save_path="save_dir")
246247
# bytes_images_dict = GeminiImage.fetch_bytes_sync(response_images, cookies) # Get bytes images dict
247248
# GeminiImage.save_images_sync(bytes_images_dict, path="save_dir") # Save to path
248249
```
249-
*Async*
250+
*Async downloader*
250251
```python
251252
response = GeminiClient.generate_content("Create illustrations of Seoul, South Korea.")
252253

@@ -257,22 +258,20 @@ await GeminiImage.save(response_images, "save_dir")
257258
# await GeminiImage.save_images(image_data_dict, "save_dir")
258259
```
259260

260-
<details><summary>Async wrapper</summary>
261+
<details><summary>Async downloader wrapper</summary>
261262

262263
```
263264
import asyncio
264265
from gemini import Gemini, GeminiImage
265266
266-
async def fetch_and_save_images_async(prompt: str, save_path: str="save_dirs", cookies=cookies):
267-
response = await GeminiClient.generate_content_async(prompt)
268-
269-
response_images = response.web_images # Check response images [Dict]
267+
async def save_response_web_imagse(response_images, save_path="save_dir", cookies=cookies):
270268
await GeminiImage.save(response_images, save_path=save_path, cookies=cookies)
271269
272270
# Run the async function
273271
if __name__ == "__main__":
274-
user_prompt = input("Enter your prompt: ")
275-
asyncio.run(fetch_and_save_images_async(user_prompt))
272+
cookies = {"key" : "value"}
273+
response_images = response.web_images
274+
asyncio.run(save_response_web_imagse(response_images, save_path="save_dir", cookies=cookies))
276275
```
277276

278277
`GeminiImage.save` method logic
@@ -281,23 +280,99 @@ if __name__ == "__main__":
281280
import asyncio
282281
from gemini import Gemini, GeminiImage
283282
284-
async def fetch_and_save_images_async(prompt: str, save_path: str="save_dir", cookies=cookies):
285-
response = await GeminiClient.generate_content_async(prompt)
286-
287-
response_images = response.web_images # Check response images [Dict]
283+
async def save_response_web_imagse(response_images, save_path="save_dir", cookies=cookies):
288284
image_data_dict = await GeminiImage.fetch_images_dict(response_images, cookies=cookies) # Get bytes images dict asynchronously
289-
await GeminiImage.save_images(image_data_dict, save_path=save_path) # Save to path asynchronously
285+
await GeminiImage.save_images(image_data_dict, save_path=save_path)
290286
291287
# Run the async function
292288
if __name__ == "__main__":
293-
user_prompt = input("Enter your prompt: ")
294-
asyncio.run(fetch_and_save_images_async(user_prompt))
289+
response_images = response.web_images # Check response images [Dict]
290+
asyncio.run(save_response_web_imagse(response_images, save_path="save_dir", cookies=cookies))
295291
```
296292

297293
</details>
298294

299295
<br>
300296

297+
### # 06. Generate content from images
298+
Takes an image as input and returns a response.
299+
300+
```python
301+
image = 'folder/image.jpg'
302+
# image = open('folder/image.jpg', 'rb').read() # (jpeg, png, webp) are supported.
303+
304+
response = GeminiClient.generate_content("What does the text in this image say?", image=image)
305+
response.response_dict
306+
```
307+
308+
<br>
309+
310+
### # 07. Generate content using Google Services
311+
To begin, you must link Google Workspace to activate this extension via the [Gemini web extension](https://gemini.google.com/extensions). Please refer to the [official notice](https://support.google.com/gemini/answer/13695044) and review the [privacy policies](https://support.google.com/gemini/answer/13594961?visit_id=638457301410420313-1578971242&p=privacy_help&rd=1) for more details.
312+
313+
*extention flags*
314+
```
315+
@Gmail, @Google Drive, @Google Docs, @Google Maps, @Google Flights, @Google Hotels, @YouTube
316+
```
317+
```python
318+
response = GeminiClient.generate_content("@YouTube Search clips related with Google Gemini")
319+
response.response_dict
320+
```
321+
<details><summary>Extension description</summary>
322+
323+
### Google Workspace
324+
- **Services**: @Gmail, @Google Drive, @Google Docs
325+
- **Description**: Summarize, search, and find desired information quickly in your content for efficient personal task management.
326+
- **Features**: Information retrieval, document summarization, information categorization
327+
328+
### Google Maps
329+
- **Service**: @Google Maps
330+
- **Description**: Execute plans using location-based information. Note: Google Maps features may be limited in some regions.
331+
- **Features**: Route guidance, nearby search, navigation
332+
333+
### Google Flights
334+
- **Service**: @Google Flights
335+
- **Description**: Search real-time flight information to plan tailored travel itineraries.
336+
- **Features**: Holiday preparation, price comparison, trip planning
337+
338+
### Google Hotels
339+
- **Service**: @Google Hotels
340+
- **Description**: Search for hotels considering what matters most to you, like having a conversation with a friend.
341+
- **Features**: Packing for travel, sightseeing, special relaxation
342+
343+
### YouTube
344+
- **Service**: @YouTube
345+
- **Description**: Explore YouTube videos and ask questions about what interests you.
346+
- **Features**: Problem-solving, generating ideas, search, exploring topics
347+
</details>
348+
349+
<br>
350+
351+
352+
### # 08. Fix context setting rcid
353+
You can specify a particular response by setting its Response Choice ID (RCID).
354+
355+
```python
356+
# Generate content for the prompt "Give me some information about the USA."
357+
response1 = GeminiClient.generate_content("Give me some information about the USA.")
358+
# After reviewing the responses, choose the one you prefer and copy its RCID.
359+
GeminiClient.rcid = "rc_xxxx"
360+
361+
# Now, generate content for the next prompt "How long does it take from LA to New York?"
362+
response2 = GeminiClient.generate_content("How long does it take from LA to New York?")
363+
```
364+
365+
<br>
366+
367+
### # 09. Changing the Selected Response from 0 to n
368+
In Gemini, generate_content returns the first response. This may vary depending on length or sorting. Therefore, you can specify the index of the chosen response from 0 to n as follows. However, if there is only one response, revert it back to 0.
369+
```python
370+
from gemini import GeminiModelOutput
371+
GeminiModelOutput.chosen = 1 # default is 0
372+
response1 = GeminiClient.generate_content("Give me some information about the USA.")
373+
```
374+
375+
301376
## Further
302377

303378
### Use rotating proxies
@@ -400,14 +475,15 @@ Contributors to the [Bard API](https://github.com/dsdanielpark/Bard-API/) and [G
400475
</details>
401476

402477
## Contacts
478+
403479
Core maintainers:
404480
- [Antonio Cheong](https://github.com/acheong08) / [email protected] <br>
405481
- [Daniel Park](https://github.com/DSDanielPark) / [email protected]
406482

407483

408484

409485
## License
410-
[MIT](https://opensource.org/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. This project is a personal initiative and is not affiliated with or endorsed by Google. It is recommended to use Google's official API.
486+
[MIT](https://opensource.org/license/mit/) license, 2024. 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. This project is a personal initiative and is not affiliated with or endorsed by Google. It is recommended to use Google's official API.
411487

412488

413489
## References
@@ -423,4 +499,3 @@ Users bear all legal responsibilities when using the GeminiAPI package, which of
423499
<br>
424500

425501

426-
*Copyright (c) 2024 Minwoo(Daniel) Park, South Korea*<br>

0 commit comments

Comments
 (0)