datdevsteve commited on
Commit
52c2511
·
verified ·
1 Parent(s): 56605a1

Update agent/image_symptom_tool.py

Browse files
Files changed (1) hide show
  1. agent/image_symptom_tool.py +34 -39
agent/image_symptom_tool.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  from langchain_core.tools import tool
2
  import requests
3
  import base64
@@ -5,52 +7,45 @@ from PIL import Image
5
  import io
6
 
7
  @tool
8
- def analyze_symptom_image(image_url: str, image_description: str = "") -> str:
9
  """
10
- Analyzes symptom images using vision classification model via FastAPI backend.
11
- Input: Image URL and optional image description
12
- Output: Image symptom analysis with confidence scores
13
  """
 
14
  try:
15
- # Download image from URL
16
- print(f"📥 Downloading image from: {image_url}")
17
- image_response = requests.get(image_url, timeout=120)
18
- image_response.raise_for_status()
19
- image = Image.open(io.BytesIO(image_response.content)).convert('RGB')
20
-
21
- # Convert to base64 for FastAPI transmission
22
- buffered = io.BytesIO()
23
- image.save(buffered, format="PNG")
24
- img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
25
-
26
- # Call your HF FastAPI Space
27
  api_url = "https://datdevsteve-nivra-vision-diagnosis.hf.space/run/predict"
 
28
  payload = {
29
- "data": [img_base64],
30
- "description": image_description or ""
31
  }
32
-
33
- print("🔬 Calling Nivra Vision FastAPI backend...")
34
- response = requests.post(api_url, json=payload, timeout=60)
35
  response.raise_for_status()
36
-
37
  result = response.json()
38
-
39
- # Extract diagnosis from HF Space response format
40
- if "data" in result and len(result["data"]) > 0:
41
- diagnosis = result["data"][0]
42
- return f"""
43
- [SYMPTOM IMAGE ANALYSIS - SUCCESS]:
44
- FastAPI Backend Response: {diagnosis}
45
-
46
- 🔍 **Image Description**: {image_description or "Not provided"}
47
- 📡 **Backend**: nivra-vision-diagnosis HF Space"""
48
- else:
49
- return "[SYMPTOM IMAGE ANALYSIS - WARNING]: No diagnosis returned from backend"
50
-
51
  except requests.exceptions.Timeout:
52
- return "[SYMPTOM IMAGE ANALYSIS - ERROR]: Backend timeout. Please try again."
53
- except requests.exceptions.RequestException as e:
54
- return f"[SYMPTOM IMAGE ANALYSIS - ERROR]: Network error: {str(e)}"
55
  except Exception as e:
56
- return f"[SYMPTOM IMAGE ANALYSIS - ERROR]: Unexpected error: {str(e)}"
 
1
+ # agent/image_symptom_tool.py
2
+
3
  from langchain_core.tools import tool
4
  import requests
5
  import base64
 
7
  import io
8
 
9
  @tool
10
+ def analyze_symptom_image(image_base64: str, image_description: str = "") -> str:
11
  """
12
+ Analyze a base64-encoded image using the Nivra Vision FastAPI backend.
 
 
13
  """
14
+
15
  try:
16
+ # Decode base64 to image
17
+ image_bytes = base64.b64decode(image_base64)
18
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
19
+
20
+ # Re-encode cleanly (prevents corruption)
21
+ buffer = io.BytesIO()
22
+ image.save(buffer, format="JPEG", quality=85)
23
+ clean_base64 = base64.b64encode(buffer.getvalue()).decode()
24
+
25
+ # Call Vision HF Space
 
 
26
  api_url = "https://datdevsteve-nivra-vision-diagnosis.hf.space/run/predict"
27
+
28
  payload = {
29
+ "data": [clean_base64, image_description]
 
30
  }
31
+
32
+ response = requests.post(api_url, json=payload, timeout=45)
 
33
  response.raise_for_status()
34
+
35
  result = response.json()
36
+
37
+ if "data" not in result or not result["data"]:
38
+ return "[IMAGE ANALYSIS] No diagnosis returned."
39
+
40
+ diagnosis = result["data"][0]
41
+
42
+ return f"""
43
+ [IMAGE TOOL RESULT]
44
+ {diagnosis}
45
+ """
46
+
 
 
47
  except requests.exceptions.Timeout:
48
+ return "[IMAGE TOOL ERROR] Vision backend timeout."
49
+
 
50
  except Exception as e:
51
+ return f"[IMAGE TOOL ERROR] {str(e)}"