Update medgemma_api.py
Browse files- medgemma_api.py +13 -21
medgemma_api.py
CHANGED
|
@@ -2,27 +2,19 @@ import os
|
|
| 2 |
import requests
|
| 3 |
import base64
|
| 4 |
|
| 5 |
-
|
| 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 |
-
|
| 11 |
-
with open(image_path, "rb") as f:
|
| 12 |
-
image_bytes = f.read()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"
|
| 20 |
-
|
| 21 |
-
|
| 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 |
|
|
|
|
|
|