syurein commited on
Commit
f8d2233
·
1 Parent(s): 545c4bb

エラーハンドリングの強化

Browse files
Files changed (2) hide show
  1. LLM_package.py +11 -5
  2. app.py +12 -2
LLM_package.py CHANGED
@@ -116,11 +116,17 @@ class ObjectDetector:
116
  {self.text if self.text else "なし"}
117
  </資料>
118
  """
119
- response = self.model.parse(self.model.get_response(image_path, analysis_prompt))
 
120
 
121
- print(f"Response: {response}")
122
  try:
123
- risk_level = int(response.get('risk_level', 0))
124
- except ValueError:
125
- risk_level = 0
 
 
 
 
 
126
  return risk_level
 
116
  {self.text if self.text else "なし"}
117
  </資料>
118
  """
119
+ response_text = self.model.get_response(image_path, analysis_prompt)
120
+ response_json_str = self.model.parse(response_text)
121
 
122
+ print(f"Response from API (raw): {response_json_str}")
123
  try:
124
+ # JSON文字列をPythonの辞書にパースする
125
+ response_data = json.loads(response_json_str)
126
+ # 辞書から 'risk_level' を取得し、整数に変換する
127
+ risk_level = int(response_data.get('risk_level', 0))
128
+ except (json.JSONDecodeError, ValueError, TypeError, AttributeError) as e:
129
+ print(f"Failed to parse risk_level from response. Error: {e}")
130
+ print(f"Response content: {response_json_str}")
131
+ risk_level = 0 # パース失敗時はデフォルト値0を返す
132
  return risk_level
app.py CHANGED
@@ -52,6 +52,7 @@ import math
52
  import numpy as np
53
  import matplotlib.pyplot as plt
54
  from dotenv import load_dotenv
 
55
  from pathlib import Path
56
  #この下のコードは特定の領域をマスクしないタイプのコード
57
  import uuid
@@ -1168,8 +1169,17 @@ async def analyze(
1168
  )
1169
 
1170
  except Exception as e:
1171
- print(f"リクエスト処理エラー: {str(e)}")
1172
- raise HTTPException(status_code=500, detail="内部サーバーエラー")
 
 
 
 
 
 
 
 
 
1173
 
1174
 
1175
  @app.post("/create-mask-and-inpaint-sum-llm-simple")
 
52
  import numpy as np
53
  import matplotlib.pyplot as plt
54
  from dotenv import load_dotenv
55
+ import traceback
56
  from pathlib import Path
57
  #この下のコードは特定の領域をマスクしないタイプのコード
58
  import uuid
 
1169
  )
1170
 
1171
  except Exception as e:
1172
+ # エラーログを詳細に出力
1173
+ error_type = type(e).__name__
1174
+ error_details = str(e)
1175
+ stack_trace = traceback.format_exc()
1176
+
1177
+ print("="*50)
1178
+ print(f"リクエスト処理エラー発生: {error_type}")
1179
+ print(f"エラー詳細: {error_details}")
1180
+ print(f"--- スタックトレース ---\n{stack_trace}")
1181
+ print("="*50)
1182
+ raise HTTPException(status_code=500, detail=f"内部サーバーエラーが発生しました: {error_type}")
1183
 
1184
 
1185
  @app.post("/create-mask-and-inpaint-sum-llm-simple")