yusir4200 commited on
Commit
d058706
·
verified ·
1 Parent(s): bd1f144

Update medgemma_api.py

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