Spaces:
Sleeping
Sleeping
Delete model_initializer.py
Browse files- model_initializer.py +0 -57
model_initializer.py
DELETED
|
@@ -1,57 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
|
| 3 |
-
|
| 4 |
-
def initialize_tone_classifier():
|
| 5 |
-
"""
|
| 6 |
-
感情・トーン分析用のHugging Faceパイプラインを初期化する。
|
| 7 |
-
"""
|
| 8 |
-
TONE_MODEL_NAME = "cl-tone/bert-base-japanese-whole-word-masking"
|
| 9 |
-
try:
|
| 10 |
-
# 感情・トーン分析パイプライン(二値分類をシミュレート)
|
| 11 |
-
tone_classifier = pipeline(
|
| 12 |
-
"sentiment-analysis",
|
| 13 |
-
model = "cl-tohoku/bert-base-japanese-whole-word-masking", # 適切な感情分析モデルに置き換える
|
| 14 |
-
tokenizer = "cl-tohoku/bert-base-japanese-whole-word-masking"
|
| 15 |
-
)
|
| 16 |
-
print("感情・トーン分析パイプラインをロードしました。")
|
| 17 |
-
return tone_classifier
|
| 18 |
-
except Exception as e:
|
| 19 |
-
print(f"感情分析モデルのロードに失敗しました。デフォルトのテキスト分類器を使用します:{e}")
|
| 20 |
-
# 代替として、凡庸的な分類モデルを使用
|
| 21 |
-
classifier = pipeline(
|
| 22 |
-
"text-classification",
|
| 23 |
-
model=AutoModelForSequenceClassification.from_pretrained(TONE_MODEL_NAME),
|
| 24 |
-
tokenizer=AutoTokenizer.from_pretrained(TONE_MODEL_NAME),
|
| 25 |
-
id2label={0: "NEGATIVE", 1: "POSITIVE"}
|
| 26 |
-
)
|
| 27 |
-
print("汎用テキスト分類器を代替して使用します。")
|
| 28 |
-
return classifier
|
| 29 |
-
|
| 30 |
-
def initialize_star_classifier():
|
| 31 |
-
"""
|
| 32 |
-
STAR法適用度チェック用のZero-Shot Classificationパイプラインを初期化する。
|
| 33 |
-
"""
|
| 34 |
-
try:
|
| 35 |
-
star_classifier = pipeline(
|
| 36 |
-
"zero-shot-classification",
|
| 37 |
-
model="izumi-lab/bert-base-japanese-v2", # Zero-Shot Classificationにより適した日本語モデル
|
| 38 |
-
tokenizer= "izumi-lab/bert-base-japanese-v2",
|
| 39 |
-
device=0 if torch.cuda.is_available() else -1 # GPUがあれば使用
|
| 40 |
-
)
|
| 41 |
-
print("STAR法適用度チェックパイプライン(Zero-Shot)をロードしました。")
|
| 42 |
-
return star_classifier
|
| 43 |
-
except Exception as e:
|
| 44 |
-
print(f"Zero-Shot Classigicationモデルのロードに失敗しました。汎用モデルを使用します:{e}")
|
| 45 |
-
star_classifier = pipeline(
|
| 46 |
-
"zero-shot-classification",
|
| 47 |
-
model="cl-tohoku/bert^base^japanese-whole-word-masking",
|
| 48 |
-
tokenizer="cl-tohoku/bert-base-japanese-whole-word-masking",
|
| 49 |
-
device=0 if torch.cuda.is_available() else -1
|
| 50 |
-
)
|
| 51 |
-
print("汎用Zero-Shot Classificationモデルを代替して使用します。")
|
| 52 |
-
return star_classifier
|
| 53 |
-
|
| 54 |
-
__name__ = "__main__":
|
| 55 |
-
# モジュールの動作確認
|
| 56 |
-
initialize_tone_classifier()
|
| 57 |
-
initialize_star_classifier()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|