File size: 945 Bytes
bd1f144
9ed9659
 
 
777a4c4
 
 
 
9ed9659
777a4c4
 
 
 
9ed9659
777a4c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ed9659
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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}"