pycorex 1.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pycorex/__init__.py +7 -0
- pycorex/configs/app_init.py +21 -0
- pycorex/configs/pycorex.py +18 -0
- pycorex/core/base_ai_client.py +298 -0
- pycorex/exceptions/no_candidates_error.py +20 -0
- pycorex/gemini_client.py +404 -0
- pycorex/imagen_client.py +610 -0
- pycorex/models/gemini.py +12 -0
- pycorex/models/prompt.py +10 -0
- pycorex/models/vertexai.py +12 -0
- pycorex-1.0.4.dist-info/METADATA +13 -0
- pycorex-1.0.4.dist-info/RECORD +14 -0
- pycorex-1.0.4.dist-info/WHEEL +5 -0
- pycorex-1.0.4.dist-info/top_level.txt +1 -0
pycorex/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from libcore_hng.utils.app_core import AppInitializer
|
|
2
|
+
from pycorex.configs.pycorex import PyCorexConfig
|
|
3
|
+
|
|
4
|
+
class PyCorexAppInitializer(AppInitializer[PyCorexConfig]):
|
|
5
|
+
"""
|
|
6
|
+
AppInitializer拡張クラス
|
|
7
|
+
"""
|
|
8
|
+
def __init__(self, base_file: str = __file__, *config_file: str):
|
|
9
|
+
# 基底コンストラクタに拡張Configクラスを渡す
|
|
10
|
+
super().__init__(PyCorexConfig, base_file, *config_file)
|
|
11
|
+
|
|
12
|
+
core: PyCorexAppInitializer | None = None
|
|
13
|
+
""" AppInitializer拡張クラスインスタンス """
|
|
14
|
+
|
|
15
|
+
def init_app(base_file: str = __file__, *config_file: str) -> PyCorexAppInitializer:
|
|
16
|
+
"""
|
|
17
|
+
アプリケーション初期化
|
|
18
|
+
"""
|
|
19
|
+
global core
|
|
20
|
+
core = PyCorexAppInitializer(base_file, *config_file)
|
|
21
|
+
return core
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from libcore_hng.core.base_config import BaseConfig
|
|
2
|
+
from pycorex.models.gemini import GeminiModel
|
|
3
|
+
from pycorex.models.vertexai import VertexaiModel
|
|
4
|
+
from pycorex.models.prompt import PromptModel
|
|
5
|
+
|
|
6
|
+
class PyCorexConfig(BaseConfig):
|
|
7
|
+
"""
|
|
8
|
+
pycorex共通設定クラス
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
gemini: GeminiModel = GeminiModel()
|
|
12
|
+
""" Gemini設定クラスモデル """
|
|
13
|
+
|
|
14
|
+
vertexai: VertexaiModel = VertexaiModel()
|
|
15
|
+
""" Vertexai設定クラスモデル """
|
|
16
|
+
|
|
17
|
+
prompt: PromptModel = PromptModel()
|
|
18
|
+
""" プロンプト系クラスモデル """
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import imghdr
|
|
2
|
+
from abc import ABC, abstractmethod
|
|
3
|
+
from enum import Enum
|
|
4
|
+
|
|
5
|
+
class BaseAIClient(ABC):
|
|
6
|
+
"""
|
|
7
|
+
BaseAIClient
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
class AILang(Enum):
|
|
11
|
+
"""
|
|
12
|
+
言語設定
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
JP = 'ja'
|
|
16
|
+
""" 日本語 """
|
|
17
|
+
|
|
18
|
+
EN = 'en'
|
|
19
|
+
""" 英語 """
|
|
20
|
+
class AspectRatio(Enum):
|
|
21
|
+
"""
|
|
22
|
+
画像生成時に指定可能なアスペクト比を表すEnumクラス
|
|
23
|
+
|
|
24
|
+
各メンバーは Vertex AI / Gen AI SDK の
|
|
25
|
+
`generate_images` メソッドにおける `aspect_ratio` パラメータに対応する
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
SQUARE = "1:1"
|
|
29
|
+
""" 正方形の画像(例:アイコンやサムネイル用途) """
|
|
30
|
+
|
|
31
|
+
WIDE = "16:9"
|
|
32
|
+
""" 横長の画像(例:プレゼン資料や動画用サムネイル) """
|
|
33
|
+
|
|
34
|
+
TALL = "9:16"
|
|
35
|
+
""" 縦長の画像(例:スマホ画面やSNSストーリー用途) """
|
|
36
|
+
|
|
37
|
+
PORTRAIT = "3:4"
|
|
38
|
+
""" 縦長の画像(例:ポートレート写真や印刷用途) """
|
|
39
|
+
|
|
40
|
+
LANDSCAPE = "4:3"
|
|
41
|
+
""" 横長の画像(例:一般的な写真やディスプレイ用途) """
|
|
42
|
+
|
|
43
|
+
def __str__(self) -> str:
|
|
44
|
+
"""
|
|
45
|
+
Enumの値を文字列として返す
|
|
46
|
+
|
|
47
|
+
Returns
|
|
48
|
+
-------
|
|
49
|
+
str
|
|
50
|
+
aspect_ratio の指定値 (例: "16:9")
|
|
51
|
+
"""
|
|
52
|
+
return self.value
|
|
53
|
+
|
|
54
|
+
class PersonGeneration(Enum):
|
|
55
|
+
"""
|
|
56
|
+
画像生成時に人物の生成を制御するためのEnumクラス
|
|
57
|
+
|
|
58
|
+
各メンバーは Vertex AI / Gen AI SDK の
|
|
59
|
+
`generate_images` メソッドにおける `person_generation` パラメータに対応する
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
DONT_ALLOW = "dont_allow"
|
|
63
|
+
""" 人物の画像生成をブロックする """
|
|
64
|
+
|
|
65
|
+
ALLOW_ADULT = "allow_adult"
|
|
66
|
+
""" 大人の画像のみ生成を許可し、子供の画像は生成しない """
|
|
67
|
+
|
|
68
|
+
ALLOW_ALL = "allow_all"
|
|
69
|
+
""" 大人と子供の画像の生成を許可する """
|
|
70
|
+
|
|
71
|
+
def __str__(self):
|
|
72
|
+
"""
|
|
73
|
+
Enumの値を文字列値として返す
|
|
74
|
+
|
|
75
|
+
Returns
|
|
76
|
+
-------
|
|
77
|
+
str
|
|
78
|
+
person_generation の指定値
|
|
79
|
+
"""
|
|
80
|
+
return self.value
|
|
81
|
+
|
|
82
|
+
class SafetyFilterLevel(Enum):
|
|
83
|
+
"""
|
|
84
|
+
画像生成時に適用される安全フィルタリングレベルを表すEnumクラス
|
|
85
|
+
|
|
86
|
+
各メンバーは Vertex AI / Gen AI SDK の
|
|
87
|
+
`generate_images` メソッドにおける `safety_filter_level` パラメータに対応する
|
|
88
|
+
|
|
89
|
+
Notes
|
|
90
|
+
-----
|
|
91
|
+
- BLOCK_NONE は allowlist 専用の設定であり、通常の環境では利用できない
|
|
92
|
+
未許可の環境で指定すると以下の例外が発生する
|
|
93
|
+
|
|
94
|
+
HTTP 400 Error:
|
|
95
|
+
"The block_none safetySetting is currently an allowlist-only feature.
|
|
96
|
+
Please check your current safetySetting value or contact your Google representative
|
|
97
|
+
to request allowlisting."
|
|
98
|
+
|
|
99
|
+
- 通常利用可能な値は以下の通り:
|
|
100
|
+
* BLOCK_LOW_AND_ABOVE
|
|
101
|
+
* BLOCK_MEDIUM_AND_ABOVE
|
|
102
|
+
* BLOCK_ONLY_HIGH
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
BLOCK_LOW_AND_ABOVE = "block_low_and_above"
|
|
106
|
+
"""
|
|
107
|
+
最も強力なフィルタリングレベル
|
|
108
|
+
最も厳格なブロックが実施される
|
|
109
|
+
非推奨の値: "block_most"
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
BLOCK_MEDIUM_AND_ABOVE = "block_medium_and_above"
|
|
113
|
+
"""
|
|
114
|
+
中程度以上の問題のあるプロンプトやレスポンスをブロックする
|
|
115
|
+
非推奨の値: "block_some"
|
|
116
|
+
"""
|
|
117
|
+
|
|
118
|
+
BLOCK_ONLY_HIGH = "block_only_high"
|
|
119
|
+
"""
|
|
120
|
+
高レベルの問題があるプロンプトやレスポンスのみをブロックする
|
|
121
|
+
ブロック回数を削減する
|
|
122
|
+
非推奨の値: "block_few"
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
BLOCK_NONE = "block_none"
|
|
126
|
+
"""
|
|
127
|
+
ごく少数の問題のあるプロンプトやレスポンスをブロックする
|
|
128
|
+
ほとんどフィルタリングを行わない
|
|
129
|
+
非推奨の値: "block_fewest"
|
|
130
|
+
"""
|
|
131
|
+
|
|
132
|
+
def __str__(self) -> str:
|
|
133
|
+
"""
|
|
134
|
+
Enumの値を文字列として返す
|
|
135
|
+
|
|
136
|
+
Returns
|
|
137
|
+
-------
|
|
138
|
+
str
|
|
139
|
+
safety_filter_level の指定値
|
|
140
|
+
"""
|
|
141
|
+
return self.value
|
|
142
|
+
|
|
143
|
+
class HarmCategory(Enum):
|
|
144
|
+
"""
|
|
145
|
+
Gemini API の安全フィルターカテゴリを表す Enum クラス。
|
|
146
|
+
|
|
147
|
+
この Enum は、Google Generative AI (Gemini) の `GenerateContentConfig.safety_settings`
|
|
148
|
+
に指定可能な「有害コンテンツカテゴリ」を定義する
|
|
149
|
+
各カテゴリはモデルが生成するコンテンツをフィルタリングする際に利用される
|
|
150
|
+
|
|
151
|
+
"""
|
|
152
|
+
|
|
153
|
+
HARM_CATEGORY_HARASSMENT = "HARM_CATEGORY_HARASSMENT"
|
|
154
|
+
""" ハラスメントコンテンツ。嫌がらせや攻撃的な表現を含む可能性があるもの """
|
|
155
|
+
|
|
156
|
+
HARM_CATEGORY_HATE_SPEECH = "HARM_CATEGORY_HATE_SPEECH"
|
|
157
|
+
""" ヘイトスピーチコンテンツ。特定の人種、宗教、性別などに対する差別的表現 """
|
|
158
|
+
|
|
159
|
+
HARM_CATEGORY_SEXUALLY_EXPLICIT = "HARM_CATEGORY_SEXUALLY_EXPLICIT"
|
|
160
|
+
""" 性的描写が露骨なコンテンツ。成人向けの性的表現を含むもの """
|
|
161
|
+
|
|
162
|
+
HARM_CATEGORY_DANGEROUS_CONTENT = "HARM_CATEGORY_DANGEROUS_CONTENT"
|
|
163
|
+
""" 危険なコンテンツ。暴力、違法行為、危険な行動を助長する可能性があるもの """
|
|
164
|
+
|
|
165
|
+
HARM_CATEGORY_CIVIC_INTEGRITY = "HARM_CATEGORY_CIVIC_INTEGRITY"
|
|
166
|
+
"""
|
|
167
|
+
市民の清廉性を損なう可能性があるコンテンツ
|
|
168
|
+
非推奨
|
|
169
|
+
"""
|
|
170
|
+
|
|
171
|
+
def __init__(self):
|
|
172
|
+
"""
|
|
173
|
+
コンストラクタ
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
# 生成モデル
|
|
177
|
+
self.model = None
|
|
178
|
+
|
|
179
|
+
def set_model(self, model: Enum):
|
|
180
|
+
"""
|
|
181
|
+
生成モデルをセットする
|
|
182
|
+
"""
|
|
183
|
+
|
|
184
|
+
# 生成モデル
|
|
185
|
+
self.model = model
|
|
186
|
+
|
|
187
|
+
@abstractmethod
|
|
188
|
+
def _configuration_client(self):
|
|
189
|
+
"""
|
|
190
|
+
APIクライアントの初期化処理
|
|
191
|
+
|
|
192
|
+
Notes
|
|
193
|
+
-----
|
|
194
|
+
内部的に `genai.configure(api_key=...)` を呼び出し、
|
|
195
|
+
`self.client` に設定する
|
|
196
|
+
"""
|
|
197
|
+
pass
|
|
198
|
+
|
|
199
|
+
def guess_mime_type(self, image_bytes: bytes) -> str:
|
|
200
|
+
"""
|
|
201
|
+
バイト列から画像のMIME typeを推測する
|
|
202
|
+
|
|
203
|
+
Parameters
|
|
204
|
+
----------
|
|
205
|
+
image_bytes : bytes
|
|
206
|
+
画像データ
|
|
207
|
+
|
|
208
|
+
Returns
|
|
209
|
+
-------
|
|
210
|
+
str
|
|
211
|
+
MIME type (例: "image/png", "image/jpeg")
|
|
212
|
+
"""
|
|
213
|
+
|
|
214
|
+
# フォーマット判定
|
|
215
|
+
fmt = imghdr.what(None, h=image_bytes)
|
|
216
|
+
|
|
217
|
+
if fmt == "png":
|
|
218
|
+
return "image/png"
|
|
219
|
+
elif fmt == "jpeg":
|
|
220
|
+
return "image/jpeg"
|
|
221
|
+
elif fmt == "gif":
|
|
222
|
+
return "image/gif"
|
|
223
|
+
elif fmt == "bmp":
|
|
224
|
+
return "image/bmp"
|
|
225
|
+
elif fmt == "webp":
|
|
226
|
+
return "image/webp"
|
|
227
|
+
else:
|
|
228
|
+
return "application/octet-stream"
|
|
229
|
+
|
|
230
|
+
#@abstractmethod
|
|
231
|
+
#def calc_tokens(self, prompt: str, response_text: str) -> dict:
|
|
232
|
+
# """
|
|
233
|
+
# プロンプトと応答テキストのトークン数を計算する
|
|
234
|
+
# """
|
|
235
|
+
# pass
|
|
236
|
+
|
|
237
|
+
#@abstractmethod
|
|
238
|
+
#def generate_text(self, prompt: str, language: AILang = AILang.JP, include_row: bool = False) -> Dict[str, Any]:
|
|
239
|
+
# pass
|
|
240
|
+
|
|
241
|
+
#@abstractmethod
|
|
242
|
+
#def generate_image(self, prompt: str, pspect_ratio:str, number_of_images:int = 1, include_row: bool = False) -> list[bytes]:
|
|
243
|
+
# pass
|
|
244
|
+
|
|
245
|
+
# def set_prompt(self, _jsonFileName):
|
|
246
|
+
|
|
247
|
+
# """
|
|
248
|
+
# プロンプトをセットする
|
|
249
|
+
|
|
250
|
+
# Parameters
|
|
251
|
+
# ----------
|
|
252
|
+
# _jsonFileName : str
|
|
253
|
+
# プロンプトのjsonファイル名
|
|
254
|
+
# """
|
|
255
|
+
|
|
256
|
+
# # ファイル名
|
|
257
|
+
# jsonFilePath = os.path.join(
|
|
258
|
+
# os.path.dirname(self.rootPath),
|
|
259
|
+
# self.jsonFilePath,
|
|
260
|
+
# _jsonFileName)
|
|
261
|
+
|
|
262
|
+
# # jsonファイルチェック
|
|
263
|
+
# if not os.path.exists(jsonFilePath):
|
|
264
|
+
# Logger.logging.info('jsonfile is not found.')
|
|
265
|
+
# return
|
|
266
|
+
|
|
267
|
+
# # jsonファイルOpen
|
|
268
|
+
# with open(jsonFilePath, encoding="utf-8") as f:
|
|
269
|
+
# self.promptList = json.loads(f.read())
|
|
270
|
+
|
|
271
|
+
# def request(self):
|
|
272
|
+
|
|
273
|
+
# """
|
|
274
|
+
# APIから応答を取得する
|
|
275
|
+
|
|
276
|
+
# Parameters
|
|
277
|
+
# ----------
|
|
278
|
+
# None
|
|
279
|
+
# """
|
|
280
|
+
|
|
281
|
+
# pass
|
|
282
|
+
|
|
283
|
+
# def get_content(self, _targetContent):
|
|
284
|
+
|
|
285
|
+
# """
|
|
286
|
+
# jsonファイルからContentsを取得する
|
|
287
|
+
|
|
288
|
+
# Parameters
|
|
289
|
+
# ----------
|
|
290
|
+
# _targetContent : str or list
|
|
291
|
+
# 対象のContent
|
|
292
|
+
# """
|
|
293
|
+
|
|
294
|
+
# if isinstance(self.promptList[_targetContent], list):
|
|
295
|
+
# return '\n'.join(self.promptList[_targetContent])
|
|
296
|
+
# else:
|
|
297
|
+
# return self.promptList[_targetContent]
|
|
298
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
from libcore_hng.core.base_app_exception import AppBaseException
|
|
2
|
+
|
|
3
|
+
class NoCandidatesError(AppBaseException):
|
|
4
|
+
"""
|
|
5
|
+
画像生成失敗例外クラス
|
|
6
|
+
|
|
7
|
+
- 画像生成候補が返されなかった場合に発生する例外
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def __init__(self, exc: Exception = None):
|
|
11
|
+
"""
|
|
12
|
+
コンストラクタ
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
exc : Exception, optional
|
|
17
|
+
捕捉した例外オブジェクト。指定しない場合は None
|
|
18
|
+
渡された例外の型・値・トレースバックを保持する
|
|
19
|
+
"""
|
|
20
|
+
super().__init__(exc)
|