Spaces:
Running
Running
Update med-gemma.ipynb
Browse files- med-gemma.ipynb +43 -1
med-gemma.ipynb
CHANGED
|
@@ -1 +1,43 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# β
Install dependencies (if needed)
|
| 2 |
+
!pip install transformers accelerate --quiet
|
| 3 |
+
|
| 4 |
+
# β
Import libraries
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# β
Since you are in HuggingFace Spaces:
|
| 9 |
+
# NO Kaggle Secrets needed
|
| 10 |
+
# NO login() needed if model is public
|
| 11 |
+
|
| 12 |
+
model_id = "google/medgemma-4b-it"
|
| 13 |
+
|
| 14 |
+
# β
Load tokenizer
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 16 |
+
model_id,
|
| 17 |
+
trust_remote_code=True
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# β
Load model
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
model_id,
|
| 23 |
+
device_map="auto",
|
| 24 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 25 |
+
trust_remote_code=True
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
# β
Build generator pipeline
|
| 29 |
+
pipe = pipeline(
|
| 30 |
+
"text-generation",
|
| 31 |
+
model=model,
|
| 32 |
+
tokenizer=tokenizer,
|
| 33 |
+
max_new_tokens=350,
|
| 34 |
+
temperature=0.25
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# β
Test prompt
|
| 38 |
+
prompt = "Explain medically in simple terms: What are the symptoms of a heart attack?"
|
| 39 |
+
|
| 40 |
+
result = pipe(prompt)[0]["generated_text"]
|
| 41 |
+
|
| 42 |
+
print("\nπ§ MedGemma 4B Response:\n")
|
| 43 |
+
print(result)
|