yashbyname commited on
Commit
5ba94a9
·
verified ·
1 Parent(s): c7096a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -12
app.py CHANGED
@@ -81,24 +81,33 @@ def submit_query(session_id, query):
81
 
82
 
83
  def extract_json_from_answer(answer, image_analysis):
84
- """Extract and clean JSON from the LLM response"""
85
  try:
86
- # First try to parse the answer directly
87
-
88
- return json.loads(answer)
89
  except json.JSONDecodeError:
90
-
91
  try:
92
  # If that fails, try to find JSON content and parse it
93
  start_idx = answer.find('{')
94
- end_idx = answer.rfind('}')
95
  if start_idx != -1 and end_idx != 0:
96
  json_str = answer[start_idx:end_idx]
97
- json_str += f",'image_analysis': ['prediction': {image_analysis['prediction']}, 'confidence': {image_analysis['confidence']}%.]"
98
- return json.loads(json_str)
99
- except (json.JSONDecodeError, ValueError):
100
- logger.error("Failed to parse JSON from response")
 
101
  raise
 
 
 
 
 
 
 
 
 
 
102
 
103
  def load_model():
104
  try:
@@ -194,8 +203,8 @@ def gradio_interface(patient_info, image):
194
  logger.info(f"Prediction: {prediction}")
195
  # Format prediction results
196
  image_analysis = {
197
- "prediction": classes[int(prediction[0][0])],
198
- "confidence": float(prediction[0][0]) * 100
199
  }
200
  logger.info(f"Image analysis results: {image_analysis}")
201
 
 
81
 
82
 
83
  def extract_json_from_answer(answer, image_analysis):
84
+ """Extract and clean JSON from the LLM response and append image analysis results."""
85
  try:
86
+ # Try to parse the JSON answer directly
87
+ json_data = json.loads(answer)
 
88
  except json.JSONDecodeError:
 
89
  try:
90
  # If that fails, try to find JSON content and parse it
91
  start_idx = answer.find('{')
92
+ end_idx = answer.rfind('}') + 1
93
  if start_idx != -1 and end_idx != 0:
94
  json_str = answer[start_idx:end_idx]
95
+ json_data = json.loads(json_str)
96
+ else:
97
+ raise ValueError("Failed to locate JSON in the answer")
98
+ except (json.JSONDecodeError, ValueError) as e:
99
+ logger.error(f"Failed to parse JSON from response: {str(e)}")
100
  raise
101
+
102
+ # Append the image analysis data
103
+ if image_analysis:
104
+ json_data["image_analysis"] = {
105
+ "prediction": image_analysis["prediction"],
106
+ "confidence": f"{image_analysis['confidence']:.2f}%" # Format confidence as percentage
107
+ }
108
+
109
+ return json_data
110
+
111
 
112
  def load_model():
113
  try:
 
203
  logger.info(f"Prediction: {prediction}")
204
  # Format prediction results
205
  image_analysis = {
206
+ "prediction": classes[np.argmax(prediction[0])],
207
+ "confidence": np.max(predicition[0]) * 100
208
  }
209
  logger.info(f"Image analysis results: {image_analysis}")
210