Spaces:
Sleeping
Sleeping
Update agent/image_symptom_tool.py
Browse files- agent/image_symptom_tool.py +13 -51
agent/image_symptom_tool.py
CHANGED
|
@@ -1,64 +1,26 @@
|
|
| 1 |
-
# agent/image_symptom_tool.py
|
| 2 |
-
|
| 3 |
from langchain_core.tools import tool
|
| 4 |
-
import
|
| 5 |
-
import base64
|
| 6 |
-
import io
|
| 7 |
-
from PIL import Image
|
| 8 |
|
| 9 |
@tool
|
| 10 |
-
def analyze_symptom_image(image_url: str
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Args:
|
| 15 |
-
image_url (str): PUBLICLY accessible image URL
|
| 16 |
-
image_description (str): Optional user hint
|
| 17 |
-
|
| 18 |
-
Returns:
|
| 19 |
-
str: Structured vision analysis text
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
resp = requests.get(image_url, timeout=20)
|
| 26 |
-
resp.raise_for_status()
|
| 27 |
-
|
| 28 |
-
image = Image.open(io.BytesIO(resp.content)).convert("RGB")
|
| 29 |
-
|
| 30 |
-
# Encode to base64 ONLY for vision backend
|
| 31 |
-
buf = io.BytesIO()
|
| 32 |
-
image.save(buf, format="PNG")
|
| 33 |
-
img_base64 = base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
print("🔬 Calling Vision HF Space")
|
| 42 |
-
vision_resp = requests.post(vision_api, json=payload, timeout=60)
|
| 43 |
-
vision_resp.raise_for_status()
|
| 44 |
-
|
| 45 |
-
result = vision_resp.json()
|
| 46 |
-
|
| 47 |
-
if "data" not in result or not result["data"]:
|
| 48 |
-
return "[VISION ERROR] No prediction returned."
|
| 49 |
-
|
| 50 |
-
diagnosis = result["data"][0]
|
| 51 |
|
| 52 |
return f"""
|
| 53 |
-
[SYMPTOM IMAGE ANALYSIS]
|
| 54 |
-
{
|
| 55 |
-
|
| 56 |
-
[USER IMAGE DESCRIPTION]
|
| 57 |
-
{image_description or "Not provided"}
|
| 58 |
-
""".strip()
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
return f"""
|
| 62 |
-
[SYMPTOM IMAGE ANALYSIS ERROR]
|
| 63 |
{str(e)}
|
| 64 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_core.tools import tool
|
| 2 |
+
from gradio_client import Client
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
@tool
|
| 5 |
+
def analyze_symptom_image(image_url: str) -> str:
|
| 6 |
"""
|
| 7 |
+
Calls Nivra Vision Diagnosis Gradio Space using image URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
|
|
|
| 9 |
try:
|
| 10 |
+
client = Client("datdevsteve/nivra-vision-diagnosis")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
result = client.predict(
|
| 13 |
+
image=image_url,
|
| 14 |
+
api_name="/predict"
|
| 15 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
return f"""
|
| 18 |
+
[SYMPTOM IMAGE ANALYSIS - SUCCESS]
|
| 19 |
+
{result}
|
| 20 |
+
"""
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
except Exception as e:
|
| 23 |
return f"""
|
| 24 |
+
[SYMPTOM IMAGE ANALYSIS - ERROR]
|
| 25 |
{str(e)}
|
| 26 |
+
"""
|