From be49bc3147503ffd23b08210c9338fa34cab4421 Mon Sep 17 00:00:00 2001 From: UZQueen <157540577+HanaokaYuzu@users.noreply.github.com> Date: Fri, 1 Mar 2024 12:59:29 -0600 Subject: [PATCH] build: rename package import name from "gemini" to "gemini_webapi" - close #8 --- README.md | 17 ++++++++++------- pyproject.toml | 2 +- src/{gemini => gemini_webapi}/__init__.py | 0 src/{gemini => gemini_webapi}/client.py | 0 src/{gemini => gemini_webapi}/constant.py | 0 src/{gemini => gemini_webapi}/exceptions.py | 0 src/{gemini => gemini_webapi}/types/__init__.py | 0 .../types/candidate.py | 0 src/{gemini => gemini_webapi}/types/image.py | 5 +++++ .../types/modeloutput.py | 0 tests/test_client_features.py | 4 ++-- tests/test_save_image.py | 10 ++++++++-- 12 files changed, 26 insertions(+), 12 deletions(-) rename src/{gemini => gemini_webapi}/__init__.py (100%) rename src/{gemini => gemini_webapi}/client.py (100%) rename src/{gemini => gemini_webapi}/constant.py (100%) rename src/{gemini => gemini_webapi}/exceptions.py (100%) rename src/{gemini => gemini_webapi}/types/__init__.py (100%) rename src/{gemini => gemini_webapi}/types/candidate.py (100%) rename src/{gemini => gemini_webapi}/types/image.py (97%) rename src/{gemini => gemini_webapi}/types/modeloutput.py (100%) diff --git a/README.md b/README.md index 0527dc5..23882b5 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ A reverse-engineered asynchronous python wrapper for [Google Gemini](https://gem ## Installation ```bash -pip install gemini-webapi +pip install gemini_webapi ``` ## Authentication @@ -36,7 +36,7 @@ Import required packages and initialize a client with your cookies obtained from ```python import asyncio -from gemini import GeminiClient +from gemini_webapi import GeminiClient # Replace "COOKIE VALUE HERE" with your actual cookie values Secure_1PSID = "COOKIE VALUE HERE" @@ -163,14 +163,17 @@ A response from Gemini usually contains multiple reply candidates with different async def main(): # Start a conversation and list all reply candidates chat = client.start_chat() - response = await chat.send_message("What's the best Japanese dish? Recommend one only.") + response = await chat.send_message("Recommend a science fiction book for me.") for candidate in response.candidates: print(candidate, "\n\n----------------------------------\n") - # Control the ongoing conversation flow by choosing candidate manually - new_candidate = chat.choose_candidate(index=1) # Choose the second candidate here - followup_response = await chat.send_message("Tell me more about it.") # Will generate contents based on the chosen candidate - print(new_candidate, followup_response, sep="\n\n----------------------------------\n\n") + if len(response.candidates) > 1: + # Control the ongoing conversation flow by choosing candidate manually + new_candidate = chat.choose_candidate(index=1) # Choose the second candidate here + followup_response = await chat.send_message("Tell me more about it.") # Will generate contents based on the chosen candidate + print(new_candidate, followup_response, sep="\n\n----------------------------------\n\n") + else: + print("Only one candidate available.") asyncio.run(main()) ``` diff --git a/pyproject.toml b/pyproject.toml index 443bbea..8791613 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,7 @@ Issues = "https://github.com/HanaokaYuzu/Gemini-API/issues" [tool.setuptools.packages.find] where = ["src"] -include = ["gemini"] +include = ["gemini_webapi"] [tool.setuptools_scm] version_scheme = "post-release" diff --git a/src/gemini/__init__.py b/src/gemini_webapi/__init__.py similarity index 100% rename from src/gemini/__init__.py rename to src/gemini_webapi/__init__.py diff --git a/src/gemini/client.py b/src/gemini_webapi/client.py similarity index 100% rename from src/gemini/client.py rename to src/gemini_webapi/client.py diff --git a/src/gemini/constant.py b/src/gemini_webapi/constant.py similarity index 100% rename from src/gemini/constant.py rename to src/gemini_webapi/constant.py diff --git a/src/gemini/exceptions.py b/src/gemini_webapi/exceptions.py similarity index 100% rename from src/gemini/exceptions.py rename to src/gemini_webapi/exceptions.py diff --git a/src/gemini/types/__init__.py b/src/gemini_webapi/types/__init__.py similarity index 100% rename from src/gemini/types/__init__.py rename to src/gemini_webapi/types/__init__.py diff --git a/src/gemini/types/candidate.py b/src/gemini_webapi/types/candidate.py similarity index 100% rename from src/gemini/types/candidate.py rename to src/gemini_webapi/types/candidate.py diff --git a/src/gemini/types/image.py b/src/gemini_webapi/types/image.py similarity index 97% rename from src/gemini/types/image.py rename to src/gemini_webapi/types/image.py index fde4e7f..d57eb29 100644 --- a/src/gemini/types/image.py +++ b/src/gemini_webapi/types/image.py @@ -59,6 +59,11 @@ async def save( ------- `str | None` Absolute path of the saved image if successful, None if filename is invalid and `skip_invalid_filename` is True + + Raises + ------ + `httpx.HTTPError` + If the network request failed """ filename = filename or self.url.split("/")[-1].split("?")[0] try: diff --git a/src/gemini/types/modeloutput.py b/src/gemini_webapi/types/modeloutput.py similarity index 100% rename from src/gemini/types/modeloutput.py rename to src/gemini_webapi/types/modeloutput.py diff --git a/tests/test_client_features.py b/tests/test_client_features.py index d4b53cb..4d211e2 100644 --- a/tests/test_client_features.py +++ b/tests/test_client_features.py @@ -3,7 +3,7 @@ from loguru import logger -from gemini import GeminiClient, AuthError +from gemini_webapi import GeminiClient, AuthError class TestGeminiClient(unittest.IsolatedAsyncioTestCase): @@ -65,7 +65,7 @@ async def test_extension_youtube(self): async def test_reply_candidates(self): chat = self.geminiclient.start_chat() response = await chat.send_message( - "What's the best Japanese dish? Recommend one only." + "Recommend a science fiction book for me." ) if len(response.candidates) == 1: diff --git a/tests/test_save_image.py b/tests/test_save_image.py index 44d8a58..49d1f1e 100644 --- a/tests/test_save_image.py +++ b/tests/test_save_image.py @@ -1,7 +1,10 @@ import os import unittest -from gemini import GeminiClient, AuthError +from httpx import HTTPError +from loguru import logger + +from gemini_webapi import GeminiClient, AuthError class TestGeminiClient(unittest.IsolatedAsyncioTestCase): @@ -22,7 +25,10 @@ async def test_save_web_image(self): self.assertTrue(response.images) for i, image in enumerate(response.images): self.assertTrue(image.url) - await image.save(verbose=True, skip_invalid_filename=True) + try: + await image.save(verbose=True, skip_invalid_filename=True) + except HTTPError as e: + logger.warning(e) async def test_save_generated_image(self): response = await self.geminiclient.generate_content(