BoneAssetmentV1 / api_client.py
SgSadguy
Add bone age app with 3 API endpoints + gradcam
80526f2
Raw
History Blame Contribute Delete
1.22 kB
"""
ตัวอย่างเรียก API ทั้ง 3 endpoint ของ Space
ติดตั้ง: pip install gradio_client
แก้ SPACE_ID เป็นของคุณ เช่น "username/boneage-prediction"
"""
from gradio_client import Client, handle_file
SPACE_ID = "your-username/boneage-prediction" # <-- แก้ตรงนี้
# private space: Client(SPACE_ID, hf_token="hf_xxx")
client = Client(SPACE_ID)
img = handle_file("test_hand.png") # path หรือ URL
# --- API 1: Bone Age ---
bone_age = client.predict(image=img, sex="male", api_name="/bone_age")
print("Bone Age:", bone_age)
# {'bone_age_months': 132.4, 'bone_age_years': 11.03, 'readable': '11 ปี 0 เดือน', 'sex': 'male'}
# --- API 2: Confidence Interval ---
ci = client.predict(image=img, sex="male", k=1.0, api_name="/confidence_interval")
print("CI:", ci)
# {'mean_months': 132.4, 'sd_months': 8.7, 'k': 1.0,
# 'lower_months': 123.7, 'upper_months': 141.1, 'interval_str': '132.40 ± 8.70 เดือน'}
# --- API 3: Grad-CAM (คืน path ไฟล์รูป overlay) ---
cam_path = client.predict(image=img, sex="male", api_name="/gradcam")
print("Grad-CAM saved at:", cam_path)