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

Commit

Permalink
build: 1.0.4 refactoring and add parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dsdanielpark committed Mar 5, 2024
1 parent 591ff30 commit 83673df
Show file tree
Hide file tree
Showing 36 changed files with 161 additions and 74 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
test.ipynb
func_text.ipynb
func_test
gemini/init_creator.py
pyproject.toml

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
27 changes: 7 additions & 20 deletions gemini/__init__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
# Copyright 2024 Daniel Park, Antonio Cheang, MIT License
from os import environ
from gemini.core import Gemini
from gemini.constants import (
HEADERS,
REPLIT_SUPPORT_PROGRAM_LANGUAGES,
Tool,
)
from gemini.client import GeminiClient
from gemini.utils import max_token, max_sentence, build_replit_data, extract_code
from .client import GeminiClient
from .core import Gemini
from .src.misc import *
from .src.parser import *
from .src.tools import *
from .src.tools.google import *

gemini_api_key = environ.get("GEMINI_COOKIES")

__all__ = [
"GeminiClient",
"Gemini",
"max_token",
"max_sentence",
"HEADERS",
"REPLIT_SUPPORT_PROGRAM_LANGUAGES",
"Tool",
"build_replit_data",
"extract_code",
]
__version__ = "1.0.3"
__version__ = "1.0.4"
__author__ = (
"daniel park <parkminwoo1991@gmail.com>, antonio cheang <teapotv8@proton.me>"
)
2 changes: 1 addition & 1 deletion gemini/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests
from typing import Optional, Any, List

from .constants import (
from gemini.src.misc.constants import (
HEADERS,
SUPPORTED_BROWSERS,
BOT_SERVER,
Expand Down
37 changes: 34 additions & 3 deletions gemini/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
from typing import Optional, Tuple, Dict
from requests.exceptions import ConnectionError, RequestException

from .models.parser.custom_parser import ParseMethod1, ParseMethod2
from .models.parser.response_parser import ResponseParser
from .constants import HEADERS, HOST, BOT_SERVER, POST_ENDPOINT, SUPPORTED_BROWSERS
from .src.parser.custom_parser import ParseMethod1, ParseMethod2
from .src.parser.response_parser import ResponseParser
from .src.model.output import GeminiCandidate, GeminiModelOutput
from .src.misc.constants import (
HEADERS,
HOST,
BOT_SERVER,
POST_ENDPOINT,
SUPPORTED_BROWSERS,
)


class Gemini:
Expand Down Expand Up @@ -340,6 +347,30 @@ def generate_content(self, prompt) -> dict:
)
return parsed_json

def generate_content(self, prompt: str) -> GeminiModelOutput:
"""Generates content based on the prompt and returns a GeminiModelOutput object."""
response_text, response_status_code = self.send_request(prompt)
if response_status_code != 200:
raise ValueError(f"Response status: {response_status_code}")
else:
parser = ResponseParser(cookies=self.cookies)
parsed_data = parser.parse(response_text)

return parsed_data
# return self._create_model_output(parsed_data, parsed_json=parsed_data)

def _create_model_output(
self, parsed_data: dict, parsed_json: dict
) -> GeminiModelOutput:
candidates = [
GeminiCandidate(**details) for details in parsed_data["candidates"].values()
]
return GeminiModelOutput(
metadata=parsed_data.get("metadata", []),
candidates=candidates,
response_dict=parsed_json,
)

def generate_custom_content(self, prompt: str, *custom_parsers) -> str:
"""Generates content based on the prompt, attempting to parse with ParseMethod1, ParseMethod2, and any additional parsers provided."""
response_text, response_status_code = self.send_request(prompt)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
71 changes: 71 additions & 0 deletions gemini/src/model/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import httpx
from pathlib import Path
from loguru import logger
from typing import List, Optional, Dict
from pydantic import BaseModel, HttpUrl


class GeminiImage(BaseModel):
url: HttpUrl
title: str = "[Image]"
alt: str = ""

def __str__(self):
return f"{self.title}({self.url}) - {self.alt}"

async def save(
self,
path: str = "temp",
filename: Optional[str] = None,
cookies: Optional[dict] = None,
) -> Optional[Path]:
filename = filename or Path(self.url).name
save_path = Path(path) / filename
save_path.parent.mkdir(parents=True, exist_ok=True)

async with httpx.AsyncClient(follow_redirects=True, cookies=cookies) as client:
try:
response = await client.get(self.url)
response.raise_for_status()
save_path.write_bytes(response.content)
return save_path
except httpx.HTTPStatusError as e:
logger.error(
f"Error downloading image: {e.response.status_code} {e.response.reason_phrase}"
)
return None


class GeminiCandidate(BaseModel):
rcid: str
text: str
web_images: List[GeminiImage] = []
generated_images: List[GeminiImage] = []
raw: Dict


class GeminiModelOutput(BaseModel):
metadata: List[str]
candidates: List[GeminiCandidate]
chosen: int = 0
response_dict: Optional[dict] = None

@property
def rcid(self) -> str:
return self.candidates[self.chosen].rcid

@property
def text(self) -> str:
return self.candidates[self.chosen].text

@property
def web_images(self) -> List[GeminiImage]:
return self.candidates[self.chosen].web_images

@property
def generated_images(self) -> List[GeminiImage]:
return self.candidates[self.chosen].generated_images

@property
def raw(self) -> Optional[Dict]:
return self.response_dict
Empty file added gemini/src/parser/__init__.py
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gemini.models.parser.base import BaesParser
from gemini.src.parser.base import BaesParser
from typing import Dict, Any


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gemini.models.parser.base import BaesParser
from gemini.src.parser.base import BaesParser
import json


Expand Down Expand Up @@ -47,7 +47,6 @@ def _extract_body(self, response_text):
)

def _parse_candidates(self, candidates_data):
candidates = []
for candidate_data in candidates_data:
web_images = self._parse_web_images(candidate_data[4])
generated_images = self._parse_generated_images(candidate_data[12])
Expand All @@ -57,8 +56,7 @@ def _parse_candidates(self, candidates_data):
"web_images": web_images,
"generated_images": generated_images,
}
candidates.append(candidate_dict)
return candidates
return candidate_dict

def _parse_web_images(self, images_data):
if not images_data:
Expand Down
Empty file added gemini/src/tools/__init__.py
Empty file.
1 change: 0 additions & 1 deletion gemini/models/citation.py → gemini/src/tools/citation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Legacy code from Bard, not used in Gemini.
class DraftCitation:
"""Github source
[[909, 1095, ['', 'edelahoz/Introduction-to-Python ', '', '', '', None, None, '', False, '', '', '', '', '', '', ''], 1, 100, None, [1]],
Expand Down
File renamed without changes.
29 changes: 14 additions & 15 deletions gemini/models/draft.py → gemini/src/tools/draft.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Legacy code from Bard, not used in Gemini.
from typing import List, Optional, Union, Dict, Tuple

from gemini.models.citation import DraftCitation
from gemini.models.tools.code import CodeContent
from gemini.models.tools.flight import BardFlightContent
from gemini.models.tools.gworkspace import GoogleWorkspaceContent
from gemini.models.image import BardImageContent
from gemini.models.tools.hotel import BardHotelContent
from gemini.models.tools.json import JsonContent
from gemini.models.tools.link import BardLink
from gemini.models.tools.map import BardMapContent
from gemini.models.tools.tool_declaimer import BardToolDeclaimer
from gemini.models.user_content import UserContent
from gemini.models.tools.youtube import BardYoutubeContent
from typing import List, Optional, Dict

from gemini.src.tools.citation import DraftCitation
from gemini.src.tools.google.code import CodeContent
from gemini.src.tools.google.flight import BardFlightContent
from gemini.src.tools.google.gworkspace import GoogleWorkspaceContent
from gemini.src.tools.image import BardImageContent
from gemini.src.tools.google.hotel import BardHotelContent
from gemini.src.tools.google.json import JsonContent
from gemini.src.tools.google.link import BardLink
from gemini.src.tools.google.map import BardMapContent
from gemini.src.tools.google.tool_declaimer import BardToolDeclaimer
from gemini.src.tools.user_content import UserContent
from gemini.src.tools.google.youtube import BardYoutubeContent


class GeminiDraft:
Expand Down
File renamed without changes.
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gemini.models.user_content import UserContent
from gemini.src.tools.user_content import UserContent
from typing import List


Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from enum import Enum
from typing import List


class GoogleWorkspaceContentKind(Enum):
Expand Down Expand Up @@ -61,7 +62,7 @@ def author(self) -> str:
return self._input_list[5]

@property
def timestamp_seconds(self) -> [int]:
def timestamp_seconds(self) -> List[int]:
return self._input_list[6]

def __str__(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gemini.models.user_content import UserContent
from gemini.src.tools.user_content import UserContent
from typing import List


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from gemini.models.user_content import UserContent
from gemini.src.tools.user_content import UserContent


class JsonContent(UserContent):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gemini.models.user_content import UserContent
from gemini.src.tools.user_content import UserContent


class BardLink(UserContent):
Expand Down
12 changes: 6 additions & 6 deletions gemini/models/tools/map.py → gemini/src/tools/google/map.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum
from typing import List, Optional, Union, Dict, Tuple

from gemini.models.user_content import UserContent
from gemini.src.tools.user_content import UserContent


class BardMapsPoint:
Expand Down Expand Up @@ -122,21 +122,21 @@ def instructions(self) -> list:
return self._input_list[0]

@property
def duration(self) -> [int, str]:
def duration(self) -> List[int, str]:
# [16873, '4 hours 41 mins']
return self._input_list[1]

@property
def distance(self) -> [int, str]:
def distance(self) -> List[int, str]:
# [313054, '313 km']
return self._input_list[2]

@property
def start_point(self) -> [float, float]:
def start_point(self) -> List[float, float]:
return self._input_list[5]

@property
def end_point(self) -> [float, float]:
def end_point(self) -> List[float, float]:
return self._input_list[6]

@property
Expand Down Expand Up @@ -172,7 +172,7 @@ def sections(self) -> List[BardMapsRoadSection]:
return [BardMapsRoadSection(s) for s in self._map[1]]

@property
def geo_position(self) -> [[float, float], [float, float]]:
def geo_position(self) -> Tuple[Tuple[float, float], Tuple[float, float]]:
return self._map[6]

def __str__(self):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from gemini.models.user_content import UserContent
from gemini.src.tools.user_content import UserContent


class BardToolDeclaimer(UserContent):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List, Optional, Union, Dict, Tuple
from typing import List

from gemini.models.user_content import UserContent
from gemini.src.tools.user_content import UserContent


class BardYoutubeVideo:
Expand Down Expand Up @@ -60,7 +60,7 @@ def __len__(self):
return len(self._input_list[4][0])

@property
def videos(self) -> list[BardYoutubeVideo]:
def videos(self) -> List[BardYoutubeVideo]:
return (
[BardYoutubeVideo(video) for video in self._input_list[4][0]]
if self._input_list[4]
Expand Down
3 changes: 1 addition & 2 deletions gemini/models/image.py → gemini/src/tools/image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# Legacy code from Bard, not used in Gemini.
from typing import Optional


Expand All @@ -14,7 +13,7 @@ def __str__(self) -> str:
return f"{self.urls[0]} ({self.width}x{self.height})"


class BardImageContent:
class GeminiImageContent:
def __init__(self, input_list: list):
self._input_list = input_list

Expand Down
Loading

0 comments on commit 83673df

Please sign in to comment.