Spaces:
Sleeping
Sleeping
Update agent/image_symptom_tool.py
Browse files- 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(
|
| 6 |
"""
|
| 7 |
-
|
| 8 |
"""
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
"data": [image_base64]
|
| 14 |
-
}
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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 |
|