datdevsteve commited on
Commit
a28c608
·
verified ·
1 Parent(s): 97fed41

Update agent/image_symptom_tool.py

Browse files
Files changed (1) hide show
  1. agent/image_symptom_tool.py +34 -23
agent/image_symptom_tool.py CHANGED
@@ -1,36 +1,47 @@
1
  from langchain_core.tools import tool
2
  import requests
 
 
 
3
 
4
  @tool
5
- def analyze_symptom_image(image_base64: str, image_description: str = "") -> dict:
6
  """
7
- Calls Nivra Vision HF Space and returns structured result
8
  """
 
 
9
 
10
- api_url = "https://datdevsteve-nivra-vision-diagnosis.hf.space/run/predict"
 
11
 
12
- payload = {
13
- "data": [image_base64]
14
- }
15
 
16
- try:
17
- response = requests.post(api_url, json=payload, timeout=90)
18
- response.raise_for_status()
19
- result = response.json()
20
-
21
- if "data" not in result or not result["data"]:
22
- raise ValueError("Empty vision response")
23
-
24
- # EXPECTED: dict from your vision backend
25
- return {
26
- "status": "success",
27
- "vision_result": result["data"][0], # ← raw model output
28
- "description": image_description or ""
29
  }
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  except Exception as e:
32
- return {
33
- "status": "error",
34
- "error": str(e)
35
- }
36
 
 
1
  from langchain_core.tools import tool
2
  import requests
3
+ from PIL import Image
4
+ import io
5
+ import base64
6
 
7
  @tool
8
+ def analyze_symptom_image(image_url: str, image_description: str = "") -> str:
9
  """
10
+ Analyze a medical image via URL using Nivra Vision HF Space
11
  """
12
+ try:
13
+ print(f"📥 Fetching image from URL: {image_url}")
14
 
15
+ img_resp = requests.get(image_url, timeout=30)
16
+ img_resp.raise_for_status()
17
 
18
+ image = Image.open(io.BytesIO(img_resp.content)).convert("RGB")
 
 
19
 
20
+ buf = io.BytesIO()
21
+ image.save(buf, format="PNG")
22
+ img_base64 = base64.b64encode(buf.getvalue()).decode()
23
+
24
+ api_url = "https://datdevsteve-nivra-vision-diagnosis.hf.space/run/predict"
25
+
26
+ payload = {
27
+ "data": [img_base64],
 
 
 
 
 
28
  }
29
 
30
+ print("🔬 Calling Vision HF Space")
31
+ resp = requests.post(api_url, json=payload, timeout=60)
32
+ resp.raise_for_status()
33
+
34
+ result = resp.json()
35
+
36
+ if "data" in result and result["data"]:
37
+ diagnosis = result["data"][0]
38
+ return f"""
39
+ [SYMPTOM IMAGE ANALYSIS]
40
+ {diagnosis}
41
+ """
42
+ else:
43
+ return "[SYMPTOM IMAGE ANALYSIS ERROR] Empty response"
44
+
45
  except Exception as e:
46
+ return f"[SYMPTOM IMAGE ANALYSIS ERROR] {str(e)}"
 
 
 
47