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

Commit

Permalink
fix: error fix in gemini ultra and generated image downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed Apr 29, 2024
1 parent b974036 commit df7f026
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion gemini/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _construct_payload(
image
and [
prompt,
int(os.getenv("GEMINI_ULTRA" or 0)),
int(os.getenv("GEMINI_ULTRA", "0")),
None,
[[[upload_image(image), 1]]],
]
Expand Down
54 changes: 28 additions & 26 deletions gemini/src/model/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def validate_images(cls, images):
"Input is empty. Please provide images infomation to proceed."
)

# Async downloader
# Async
@classmethod
async def save(
cls,
Expand All @@ -73,9 +73,33 @@ async def save(
image_data = await cls.fetch_images_dict(images, cookies)
await cls.save_images(image_data, save_path)

# Sync
@staticmethod
def save_sync(
images: List["GeminiImage"],
save_path: str = "cached",
cookies: Optional[dict] = None,
) -> Optional[Path]:
"""Synchronously saves the image to the specified path.
Args:
path (str): The directory where the image will be saved.
filename (str, optional): The filename for the saved image. If not provided,
a filename is generated based on the image title.
cookies (dict, optional): Cookies to be used for downloading the image.
Returns:
Optional[Path]: The path where the image is saved, or None if saving fails.
"""
image_data = GeminiImage.fetch_images_dict_sync(images, cookies)
GeminiImage.validate_images(image_data)
GeminiImage.save_images_sync(image_data, save_path)

##### Main functions are above. The code below handles special cases for transmitting byte images.

@staticmethod
async def fetch_bytes(
url: HttpUrl, cookies: Optional[dict] = None
url: HttpUrl, cookies: Optional[dict] = None, proxies: Optional[dict] = None
) -> Optional[bytes]:
"""
Fetches bytes of an image asynchronously.
Expand All @@ -88,8 +112,8 @@ async def fetch_bytes(
Optional[bytes]: The bytes of the image, or None if fetching fails.
"""
try:
async with httpx.AsyncClient(follow_redirects=True) as client:
response = await client.get(str(url), cookies=cookies)
async with httpx.AsyncClient(follow_redirects=True, cookies=cookies, proxies=proxies) as client:
response = await client.get(str(url))
response.raise_for_status()
return response.content
except Exception as e:
Expand Down Expand Up @@ -136,28 +160,6 @@ async def save_images(image_data: Dict[str, bytes], save_path: str = "cached"):
except Exception as e:
print(f"Error saving {title}: {str(e)}")

# Sync downloader
@staticmethod
def save_sync(
images: List["GeminiImage"],
cookies: Optional[dict] = None,
save_path: str = "cached",
) -> Optional[Path]:
"""Synchronously saves the image to the specified path.
Args:
path (str): The directory where the image will be saved.
filename (str, optional): The filename for the saved image. If not provided,
a filename is generated based on the image title.
cookies (dict, optional): Cookies to be used for downloading the image.
Returns:
Optional[Path]: The path where the image is saved, or None if saving fails.
"""
image_data = GeminiImage.fetch_images_dict_sync(images, cookies)
GeminiImage.validate_images(image_data)
GeminiImage.save_images_sync(image_data, save_path)

@staticmethod
def fetch_bytes_sync(
url: HttpUrl, cookies: Optional[dict] = None
Expand Down
2 changes: 1 addition & 1 deletion gemini/src/model/parser/response_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _extract_body(self, response_text: str) -> Dict:
continue

raise ValueError(
"All parsing strategies failed. Try to use `Gemini.send_request(prompt)` to get original payload."
"Google PeerSide authentication may have expired. Refresh the cookie manually and retry the test.\nDetails: All parsing strategies failed. Try to use `Gemini.send_request(prompt)` to get original payload"
)

def __extract_strategy_1(self, response_text: str) -> Dict:
Expand Down

0 comments on commit df7f026

Please sign in to comment.