Upload app.py
Browse files
app.py
CHANGED
|
@@ -17,7 +17,7 @@ GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
|
| 17 |
if not GEMINI_API_KEY or not GROQ_API_KEY:
|
| 18 |
print("警告: APIキーがSecretsに設定されていません。")
|
| 19 |
# 実行を止めないようにダミーを設定(デプロイ時はSecrets設定が必須)
|
| 20 |
-
GEMINI_API_KEY = "your_gemini_api_key_here"
|
| 21 |
GROQ_API_KEY = "your_groq_api_key_here"
|
| 22 |
|
| 23 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
@@ -51,14 +51,10 @@ DEFAULT_SCENE_PARAMS = {
|
|
| 51 |
|
| 52 |
# --- 2. 機能定義 ---
|
| 53 |
|
| 54 |
-
# ★★★★★ ここが重要な修正点 ★★★★★
|
| 55 |
def detect_scene_change(history, message):
|
| 56 |
-
history_text = "\n".join([f"ユーザー: {u}\n麻理: {m}" for u, m in history[-5:]])
|
| 57 |
-
|
| 58 |
-
# 利用可能なキーワードリストを作成
|
| 59 |
available_keywords = ", ".join(THEME_URLS.keys())
|
| 60 |
|
| 61 |
-
# ★ 判定基準を緩和し、具体例を追加したプロンプト
|
| 62 |
prompt = f"""
|
| 63 |
あなたは会話の流れから、登場人物たちの次の行動を読み取るエキスパートです。
|
| 64 |
# タスク
|
|
@@ -89,19 +85,32 @@ def detect_scene_change(history, message):
|
|
| 89 |
---
|
| 90 |
# 出力
|
| 91 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
try:
|
| 93 |
-
response = gemini_model.generate_content(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
if not response.candidates or response.candidates[0].finish_reason not in {1, 'STOP'}:
|
|
|
|
| 95 |
print(f"シーン検出LLMで応答がブロックされました: {response.prompt_feedback}")
|
| 96 |
return None
|
| 97 |
-
|
| 98 |
scene_name = response.text.strip().lower()
|
| 99 |
-
|
| 100 |
-
# 判定されたシーンが実際に利用可能なものか確認
|
| 101 |
if scene_name in THEME_URLS:
|
| 102 |
print(f"シーン変更を検出(候補): {scene_name}")
|
| 103 |
return scene_name
|
| 104 |
-
|
| 105 |
return None
|
| 106 |
except Exception as e:
|
| 107 |
print(f"シーン検出LLMエラー: {e}")
|
|
@@ -153,13 +162,14 @@ def generate_dialogue_with_gemini(history, message, affection, stage_name, scene
|
|
| 153 |
"""
|
| 154 |
print(f"Geminiに応答生成をリクエストします (モード: {'シーン遷移' if instruction else '通常会話'})")
|
| 155 |
|
|
|
|
| 156 |
safety_settings = {
|
| 157 |
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
| 158 |
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
|
| 159 |
-
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.
|
| 160 |
-
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.
|
| 161 |
}
|
| 162 |
-
|
| 163 |
try:
|
| 164 |
generation_config = genai.types.GenerationConfig(max_output_tokens=200, temperature=0.95)
|
| 165 |
response = gemini_model.generate_content(
|
|
@@ -171,6 +181,7 @@ def generate_dialogue_with_gemini(history, message, affection, stage_name, scene
|
|
| 171 |
if response.candidates and response.candidates[0].finish_reason in {1, 'STOP'}:
|
| 172 |
return response.text.strip()
|
| 173 |
else:
|
|
|
|
| 174 |
print(f"応答生成が途中で終了しました。理由: {response.candidates[0].finish_reason if response.candidates else 'N/A'}")
|
| 175 |
print(f"Prompt Feedback: {response.prompt_feedback}")
|
| 176 |
return "(……何か言おうとしたけど、言葉に詰まった)"
|
|
@@ -200,14 +211,12 @@ def update_affection(message, affection):
|
|
| 200 |
def respond(message, chat_history, affection, history, scene_params):
|
| 201 |
new_affection = update_affection(message, affection)
|
| 202 |
stage_name = get_relationship_stage(new_affection)
|
| 203 |
-
|
| 204 |
-
# 現在のテーマを保持しておく
|
| 205 |
current_theme = scene_params.get("theme", "default")
|
| 206 |
new_scene_name = detect_scene_change(history, message)
|
| 207 |
|
| 208 |
final_scene_params = scene_params
|
| 209 |
|
| 210 |
-
# 新しいシーンが検出され、かつ現在のシーンと異なる場合のみ遷��処理を行う
|
| 211 |
if new_scene_name and new_scene_name != current_theme:
|
| 212 |
print(f"シーンチェンジを実行: {current_theme} -> {new_scene_name}")
|
| 213 |
new_params_base = generate_scene_instruction_with_groq(new_affection, stage_name, new_scene_name, message)
|
|
@@ -223,9 +232,9 @@ def respond(message, chat_history, affection, history, scene_params):
|
|
| 223 |
|
| 224 |
new_history = history + [(message, bot_message)]
|
| 225 |
chat_history.append((message, bot_message))
|
| 226 |
-
|
| 227 |
theme_name = final_scene_params.get("theme", "default")
|
| 228 |
-
|
| 229 |
background_html = f'<div class="chat-background {theme_name}"></div>'
|
| 230 |
|
| 231 |
return "", chat_history, new_affection, stage_name, new_affection, new_history, final_scene_params, background_html
|
|
|
|
| 17 |
if not GEMINI_API_KEY or not GROQ_API_KEY:
|
| 18 |
print("警告: APIキーがSecretsに設定されていません。")
|
| 19 |
# 実行を止めないようにダミーを設定(デプロイ時はSecrets設定が必須)
|
| 20 |
+
GEMINI_API_KEY = "your_gemini_api_key_here"
|
| 21 |
GROQ_API_KEY = "your_groq_api_key_here"
|
| 22 |
|
| 23 |
genai.configure(api_key=GEMINI_API_KEY)
|
|
|
|
| 51 |
|
| 52 |
# --- 2. 機能定義 ---
|
| 53 |
|
|
|
|
| 54 |
def detect_scene_change(history, message):
|
| 55 |
+
history_text = "\n".join([f"ユーザー: {u}\n麻理: {m}" for u, m in history[-5:]])
|
|
|
|
|
|
|
| 56 |
available_keywords = ", ".join(THEME_URLS.keys())
|
| 57 |
|
|
|
|
| 58 |
prompt = f"""
|
| 59 |
あなたは会話の流れから、登場人物たちの次の行動を読み取るエキスパートです。
|
| 60 |
# タスク
|
|
|
|
| 85 |
---
|
| 86 |
# 出力
|
| 87 |
"""
|
| 88 |
+
|
| 89 |
+
# ★★★★★ 安全性フィルターを全て無効化 ★★★★★
|
| 90 |
+
safety_settings = {
|
| 91 |
+
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
| 92 |
+
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
|
| 93 |
+
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
| 94 |
+
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
try:
|
| 98 |
+
response = gemini_model.generate_content(
|
| 99 |
+
prompt,
|
| 100 |
+
generation_config={"temperature": 0.0},
|
| 101 |
+
safety_settings=safety_settings
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
if not response.candidates or response.candidates[0].finish_reason not in {1, 'STOP'}:
|
| 105 |
+
# このブロックは基本的に通過しないはずだが、念のため残す
|
| 106 |
print(f"シーン検出LLMで応答がブロックされました: {response.prompt_feedback}")
|
| 107 |
return None
|
| 108 |
+
|
| 109 |
scene_name = response.text.strip().lower()
|
|
|
|
|
|
|
| 110 |
if scene_name in THEME_URLS:
|
| 111 |
print(f"シーン変更を検出(候補): {scene_name}")
|
| 112 |
return scene_name
|
| 113 |
+
|
| 114 |
return None
|
| 115 |
except Exception as e:
|
| 116 |
print(f"シーン検出LLMエラー: {e}")
|
|
|
|
| 162 |
"""
|
| 163 |
print(f"Geminiに応答生成をリクエストします (モード: {'シーン遷移' if instruction else '通常会話'})")
|
| 164 |
|
| 165 |
+
# ★★★★★ 安全性フィルターを全て無効化 ★★★★★
|
| 166 |
safety_settings = {
|
| 167 |
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
|
| 168 |
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
|
| 169 |
+
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
|
| 170 |
+
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
|
| 171 |
}
|
| 172 |
+
|
| 173 |
try:
|
| 174 |
generation_config = genai.types.GenerationConfig(max_output_tokens=200, temperature=0.95)
|
| 175 |
response = gemini_model.generate_content(
|
|
|
|
| 181 |
if response.candidates and response.candidates[0].finish_reason in {1, 'STOP'}:
|
| 182 |
return response.text.strip()
|
| 183 |
else:
|
| 184 |
+
# このブロックは基本的に通過しないはずだが、念のため残す
|
| 185 |
print(f"応答生成が途中で終了しました。理由: {response.candidates[0].finish_reason if response.candidates else 'N/A'}")
|
| 186 |
print(f"Prompt Feedback: {response.prompt_feedback}")
|
| 187 |
return "(……何か言おうとしたけど、言葉に詰まった)"
|
|
|
|
| 211 |
def respond(message, chat_history, affection, history, scene_params):
|
| 212 |
new_affection = update_affection(message, affection)
|
| 213 |
stage_name = get_relationship_stage(new_affection)
|
| 214 |
+
|
|
|
|
| 215 |
current_theme = scene_params.get("theme", "default")
|
| 216 |
new_scene_name = detect_scene_change(history, message)
|
| 217 |
|
| 218 |
final_scene_params = scene_params
|
| 219 |
|
|
|
|
| 220 |
if new_scene_name and new_scene_name != current_theme:
|
| 221 |
print(f"シーンチェンジを実行: {current_theme} -> {new_scene_name}")
|
| 222 |
new_params_base = generate_scene_instruction_with_groq(new_affection, stage_name, new_scene_name, message)
|
|
|
|
| 232 |
|
| 233 |
new_history = history + [(message, bot_message)]
|
| 234 |
chat_history.append((message, bot_message))
|
| 235 |
+
|
| 236 |
theme_name = final_scene_params.get("theme", "default")
|
| 237 |
+
|
| 238 |
background_html = f'<div class="chat-background {theme_name}"></div>'
|
| 239 |
|
| 240 |
return "", chat_history, new_affection, stage_name, new_affection, new_history, final_scene_params, background_html
|