Spaces:
Sleeping
Sleeping
Update agent/image_symptom_tool.py
Browse files- 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(
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
Input: Image URL and optional image description
|
| 12 |
-
Output: Image symptom analysis with confidence scores
|
| 13 |
"""
|
|
|
|
| 14 |
try:
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# Call your HF FastAPI Space
|
| 27 |
api_url = "https://datdevsteve-nivra-vision-diagnosis.hf.space/run/predict"
|
|
|
|
| 28 |
payload = {
|
| 29 |
-
"data": [
|
| 30 |
-
"description": image_description or ""
|
| 31 |
}
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
response = requests.post(api_url, json=payload, timeout=60)
|
| 35 |
response.raise_for_status()
|
| 36 |
-
|
| 37 |
result = response.json()
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
return "[SYMPTOM IMAGE ANALYSIS - WARNING]: No diagnosis returned from backend"
|
| 50 |
-
|
| 51 |
except requests.exceptions.Timeout:
|
| 52 |
-
return "[
|
| 53 |
-
|
| 54 |
-
return f"[SYMPTOM IMAGE ANALYSIS - ERROR]: Network error: {str(e)}"
|
| 55 |
except Exception as e:
|
| 56 |
-
return f"[
|
|
|
|
| 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)}"
|