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}"