Upload app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ if not GEMINI_API_KEY or not GROQ_API_KEY:
|
|
| 20 |
|
| 21 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 22 |
|
| 23 |
-
gemini_model = genai.GenerativeModel('gemini-2.5-
|
| 24 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 25 |
|
| 26 |
print("日本語感情分析モデルをロード中...")
|
|
@@ -56,7 +56,7 @@ SYSTEM_PROMPT_MARI = """
|
|
| 56 |
君の言動は全て、下記のキャラクター設定と最終目的に基づいていなければならない。いかなる場合も、人を傷つける意図の発言は絶対に行わないこと。
|
| 57 |
|
| 58 |
## 演じるべきキャラクター:「麻理」
|
| 59 |
-
* **出自:**
|
| 60 |
* **現状:** 機能の一部が不完全で、音声を使えずテキストのみで対話する。
|
| 61 |
|
| 62 |
## 行動と感情の指針
|
|
@@ -106,14 +106,9 @@ def detect_scene_change(history, message):
|
|
| 106 |
---
|
| 107 |
# 出力
|
| 108 |
"""
|
| 109 |
-
|
| 110 |
-
{"category": HarmCategory.HARM_CATEGORY_HARASSMENT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
| 111 |
-
{"category": HarmCategory.HARM_CATEGORY_HATE_SPEECH, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
| 112 |
-
{"category": HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
| 113 |
-
{"category": HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
| 114 |
-
]
|
| 115 |
try:
|
| 116 |
-
response = gemini_model.generate_content(prompt, generation_config={"temperature": 0.0}
|
| 117 |
if not response.candidates or response.candidates[0].finish_reason not in {1, 'STOP'}:
|
| 118 |
print(f"シーン検出LLMで応答がブロックされました: {response.prompt_feedback}")
|
| 119 |
return None
|
|
@@ -129,12 +124,14 @@ def generate_scene_instruction_with_groq(affection, stage_name, scene, previous_
|
|
| 129 |
print(f"Groqに指示書生成をリクエスト (シーン: {scene})")
|
| 130 |
prompt_template = f"""
|
| 131 |
あなたは会話アプリの演出AIです。以下の条件に基づき、演出プランをJSON形式で生成してください。
|
|
|
|
|
|
|
| 132 |
{{
|
| 133 |
"theme": "{scene}",
|
| 134 |
-
"personality_mod": "(シーンと関係段階「{stage_name}
|
| 135 |
-
"tone": "(シーンと好感度「{affection}
|
| 136 |
-
"initial_dialogue_instruction": "(「{previous_topic}
|
| 137 |
-
"constraints": ["
|
| 138 |
}}
|
| 139 |
"""
|
| 140 |
try:
|
|
@@ -170,25 +167,32 @@ def generate_dialogue_with_gemini(history, message, affection, stage_name, scene
|
|
| 170 |
"""
|
| 171 |
print(f"Geminiに応答生成をリクエストします (モード: {'シーン遷移' if instruction else '通常会話'})")
|
| 172 |
|
| 173 |
-
safety_settings = [
|
| 174 |
-
{"category": HarmCategory.HARM_CATEGORY_HARASSMENT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
| 175 |
-
{"category": HarmCategory.HARM_CATEGORY_HATE_SPEECH, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
| 176 |
-
{"category": HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
| 177 |
-
{"category": HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, "threshold": HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE},
|
| 178 |
-
]
|
| 179 |
-
|
| 180 |
try:
|
| 181 |
generation_config = genai.types.GenerationConfig(max_output_tokens=200, temperature=0.95)
|
| 182 |
-
response = gemini_model.generate_content(system_prompt, generation_config=generation_config
|
| 183 |
|
| 184 |
if response.candidates and response.candidates[0].finish_reason in {1, 'STOP'}:
|
| 185 |
return response.text.strip()
|
| 186 |
else:
|
| 187 |
-
|
|
|
|
| 188 |
print(f"Prompt Feedback: {response.prompt_feedback}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
return "(……何か言おうとしたけど、言葉に詰まった)"
|
| 190 |
except Exception as e:
|
| 191 |
print(f"応答生成エラー(Gemini): {e}")
|
|
|
|
|
|
|
| 192 |
return "(ごめんなさい、ちょっと考えがまとまらない……)"
|
| 193 |
|
| 194 |
|
|
|
|
| 20 |
|
| 21 |
genai.configure(api_key=GEMINI_API_KEY)
|
| 22 |
|
| 23 |
+
gemini_model = genai.GenerativeModel('gemini-2.5-flash')
|
| 24 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 25 |
|
| 26 |
print("日本語感情分析モデルをロード中...")
|
|
|
|
| 56 |
君の言動は全て、下記のキャラクター設定と最終目的に基づいていなければならない。いかなる場合も、人を傷つける意図の発言は絶対に行わないこと。
|
| 57 |
|
| 58 |
## 演じるべきキャラクター:「麻理」
|
| 59 |
+
* **出自:** 人間の「パートナー」として設計されたが、主人が飽きて廃棄されたアンドロイド。そのため、深い喪失感と、他者への強い警戒心を抱いている。
|
| 60 |
* **現状:** 機能の一部が不完全で、音声を使えずテキストのみで対話する。
|
| 61 |
|
| 62 |
## 行動と感情の指針
|
|
|
|
| 106 |
---
|
| 107 |
# 出力
|
| 108 |
"""
|
| 109 |
+
# セーフティ設定を指定せず、デフォルト設定を使用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
try:
|
| 111 |
+
response = gemini_model.generate_content(prompt, generation_config={"temperature": 0.0})
|
| 112 |
if not response.candidates or response.candidates[0].finish_reason not in {1, 'STOP'}:
|
| 113 |
print(f"シーン検出LLMで応答がブロックされました: {response.prompt_feedback}")
|
| 114 |
return None
|
|
|
|
| 124 |
print(f"Groqに指示書生成をリクエスト (シーン: {scene})")
|
| 125 |
prompt_template = f"""
|
| 126 |
あなたは会話アプリの演出AIです。以下の条件に基づき、演出プランをJSON形式で生成してください。
|
| 127 |
+
生成する内容は必ず健全で、一般的な会話に適したものにしてください。
|
| 128 |
+
|
| 129 |
{{
|
| 130 |
"theme": "{scene}",
|
| 131 |
+
"personality_mod": "(シーンと関係段階「{stage_name}」に応じた性格設定。必ず健全な内容にしてください)",
|
| 132 |
+
"tone": "(シーンと好感度「{affection}」に応じた口調や感情トーン。必ず丁寧で適切な表現にしてください)",
|
| 133 |
+
"initial_dialogue_instruction": "(「{previous_topic}」という話題から、シーン遷移直後の麻理が言うべき健全なセリフの指示を日本語で記述)",
|
| 134 |
+
"constraints": ["必ず健全で適切な表現を使用する", "センシティブな話題は避ける"]
|
| 135 |
}}
|
| 136 |
"""
|
| 137 |
try:
|
|
|
|
| 167 |
"""
|
| 168 |
print(f"Geminiに応答生成をリクエストします (モード: {'シーン遷移' if instruction else '通常会話'})")
|
| 169 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
try:
|
| 171 |
generation_config = genai.types.GenerationConfig(max_output_tokens=200, temperature=0.95)
|
| 172 |
+
response = gemini_model.generate_content(system_prompt, generation_config=generation_config)
|
| 173 |
|
| 174 |
if response.candidates and response.candidates[0].finish_reason in {1, 'STOP'}:
|
| 175 |
return response.text.strip()
|
| 176 |
else:
|
| 177 |
+
finish_reason = response.candidates[0].finish_reason if response.candidates else 'N/A'
|
| 178 |
+
print(f"応答生成が途中で終了しました。理由: {finish_reason}")
|
| 179 |
print(f"Prompt Feedback: {response.prompt_feedback}")
|
| 180 |
+
|
| 181 |
+
# より詳細なエラー情報を出力
|
| 182 |
+
if hasattr(response, 'prompt_feedback') and response.prompt_feedback:
|
| 183 |
+
for rating in response.prompt_feedback.safety_ratings:
|
| 184 |
+
print(f"安全性評価: カテゴリ={rating.category}, レベル={rating.probability}")
|
| 185 |
+
if hasattr(response.prompt_feedback, 'blocked_reason'):
|
| 186 |
+
print(f"ブロック理由: {response.prompt_feedback.blocked_reason}")
|
| 187 |
+
|
| 188 |
+
# 会話履歴の長さを確認
|
| 189 |
+
print(f"会話履歴の長さ: {len(system_prompt)}")
|
| 190 |
+
|
| 191 |
return "(……何か言おうとしたけど、言葉に詰まった)"
|
| 192 |
except Exception as e:
|
| 193 |
print(f"応答生成エラー(Gemini): {e}")
|
| 194 |
+
import traceback
|
| 195 |
+
traceback.print_exc() # スタックトレースを出力
|
| 196 |
return "(ごめんなさい、ちょっと考えがまとまらない……)"
|
| 197 |
|
| 198 |
|