Spaces:
Sleeping
Sleeping
Update agent/image_symptom_tool.py
Browse files- agent/image_symptom_tool.py +56 -56
agent/image_symptom_tool.py
CHANGED
|
@@ -1,56 +1,56 @@
|
|
| 1 |
-
from langchain_core.tools import tool
|
| 2 |
-
import requests
|
| 3 |
-
import base64
|
| 4 |
-
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=
|
| 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 |
+
from langchain_core.tools import tool
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
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)}"
|