yusir4200 commited on
Commit
9ed9659
·
verified ·
1 Parent(s): f9048df

Create medgemma_api.py

Browse files
Files changed (1) hide show
  1. medgemma_api.py +27 -0
medgemma_api.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import base64
3
+
4
+ # 你需要在 Hugging Face Space 的 Secrets 里配置一个名为 HUGGINGFACE_TOKEN 的值
5
+ HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
6
+ API_URL = "https://api-inference.huggingface.co/models/google/medgemma-4b-it"
7
+ HEADERS = {"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"}
8
+
9
+ def query_medgemma(image_path, question):
10
+ with open(image_path, "rb") as f:
11
+ image_bytes = f.read()
12
+
13
+ encoded_image = base64.b64encode(image_bytes).decode("utf-8")
14
+ payload = {
15
+ "inputs": [
16
+ {
17
+ "role": "user",
18
+ "content": [
19
+ {"type": "image", "image": encoded_image},
20
+ {"type": "text", "text": question}
21
+ ]
22
+ }
23
+ ]
24
+ }
25
+
26
+ response = requests.post(API_URL, headers=HEADERS, json=payload)
27
+ return response.json()[0]["generated_text"] if response.ok else "Error: Unable to get response."