sirochild commited on
Commit
fb20ed4
·
verified ·
1 Parent(s): 09b166c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -13
app.py CHANGED
@@ -23,9 +23,10 @@ genai.configure(api_key=GEMINI_API_KEY)
23
  gemini_model = genai.GenerativeModel('gemini-2.5-flash')
24
  groq_client = Groq(api_key=GROQ_API_KEY)
25
 
26
- # 日本語感情分析モデルの読み込みを無効化(パフォーマンス向上のため)
27
- print("日本語感情分析モデルは無効化されています(パフォーマンス向上のため)")
28
- sentiment_analyzer = None
 
29
 
30
  THEME_URLS = {
31
  "default": "https://cdn.pixabay.com/photo/2017/03/28/12/11/chairs-2181960_1280.jpg",
@@ -291,17 +292,31 @@ def get_relationship_stage(affection):
291
  return "ステージ4:最親密"
292
 
293
  def update_affection(message, affection):
294
- # 感情分析モデルを使用せず、メッセージの長さに基づいて好感度を更新
295
- # これにより、モデルの読み込みによるパフォーマンス問題を回避
296
- message_length = len(message)
297
 
298
- # 短いメッセージは好感度を下げる、長いメッセージは好感度を上げる
299
- if message_length < 10:
300
- return max(0, affection - 1) # 短いメッセージ
301
- elif message_length > 30:
302
- return min(100, affection + 3) # 長いメッセージ
303
- else:
304
- return min(100, affection + 1) # 普通のメッセージ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
 
306
 
307
 
 
23
  gemini_model = genai.GenerativeModel('gemini-2.5-flash')
24
  groq_client = Groq(api_key=GROQ_API_KEY)
25
 
26
+ # 日本語感情分析モデルの初期化(グローバル変数として保持)
27
+ print("日本語感情分析モデルを初期化中...")
28
+ # モデル自体はグローバル変数として保持し、実際の読み込みは必要時に行う
29
+ sentiment_analyzer_model = None
30
 
31
  THEME_URLS = {
32
  "default": "https://cdn.pixabay.com/photo/2017/03/28/12/11/chairs-2181960_1280.jpg",
 
292
  return "ステージ4:最親密"
293
 
294
  def update_affection(message, affection):
295
+ global sentiment_analyzer_model
 
 
296
 
297
+ try:
298
+ # モデルが未ロードの場合のみロード
299
+ if sentiment_analyzer_model is None:
300
+ print("感情分析モデルをロード中...")
301
+ from transformers import pipeline
302
+ sentiment_analyzer_model = pipeline("sentiment-analysis", model="koheiduck/bert-japanese-finetuned-sentiment")
303
+ print("感情分析モデルのロード完了")
304
+
305
+ # 感情分析を実行
306
+ result = sentiment_analyzer_model(message)[0]
307
+ print(f"感情分析結果: {result}")
308
+
309
+ if result['label'] == 'positive':
310
+ return min(100, affection + 5)
311
+ elif result['label'] == 'negative':
312
+ return max(0, affection - 5)
313
+ else:
314
+ return affection
315
+
316
+ except Exception as e:
317
+ print(f"感情分析エラー: {e}")
318
+ # エラーが発生した場合は現在の好感度を維持
319
+ return affection
320
 
321
 
322