SkinLesionSegmentationApp2 / medgemma_api.py
yusir4200's picture
Update medgemma_api.py
777a4c4 verified
raw
history blame contribute delete
945 Bytes
import os
import requests
import base64
# 获取 Hugging Face Token(已通过 Secrets 设置)
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
API_URL = "https://api-inference.huggingface.co/models/google/medgemma-4b-it"
HEADERS = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
def query_medgemma(image_path, question):
with open(image_path, "rb") as f:
image_bytes = f.read()
encoded_image = base64.b64encode(image_bytes).decode("utf-8")
payload = {
"inputs": [
{
"role": "user",
"content": [
{"type": "image", "image": encoded_image},
{"type": "text", "text": question}
]
}
]
}
response = requests.post(API_URL, headers=HEADERS, json=payload)
if response.ok:
return response.json()[0]["generated_text"]
else:
return f"Error: {response.text}"