Srikar00007 commited on
Commit
fb85ec4
Β·
verified Β·
1 Parent(s): 07aa29e

Update med-gemma.ipynb

Browse files
Files changed (1) hide show
  1. med-gemma.ipynb +43 -1
med-gemma.ipynb CHANGED
@@ -1 +1,43 @@
1
- {"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"machine_shape":"hm","gpuType":"T4"},"accelerator":"GPU","kaggle":{"accelerator":"gpu","dataSources":[],"dockerImageVersionId":31193,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# βœ… Install dependencies\n!pip install transformers accelerate --quiet\n\n# βœ… Import required libraries\nfrom kaggle_secrets import UserSecretsClient\nfrom huggingface_hub import login\nfrom transformers import AutoTokenizer, AutoModelForCausalLM, pipeline\n\n# βœ… Load your HF token from Kaggle Secrets (make sure secret name = HF_TOKEN)\nuser_secrets = UserSecretsClient()\nhf_token = user_secrets.get_secret(\"HF_TOKEN\")\n\n# βœ… Login to HuggingFace\nlogin(token=hf_token)\n\n# βœ… Model ID (correct one you asked for)\nmodel_id = \"google/medgemma-4b-it\"\n\n# βœ… Load tokenizer\ntokenizer = AutoTokenizer.from_pretrained(\n model_id, \n trust_remote_code=True\n)\n\n# βœ… Load model on GPU\nmodel = AutoModelForCausalLM.from_pretrained(\n model_id,\n device_map=\"auto\",\n torch_dtype=\"auto\",\n trust_remote_code=True\n)\n\n# βœ… Build pipeline\npipe = pipeline(\n \"text-generation\",\n model=model,\n tokenizer=tokenizer,\n max_new_tokens=350,\n temperature=0.25\n)\n\n# βœ… Ask MedGemma a question\nprompt = \"Explain medically in simple terms: What are the symptoms of a heart attack?\"\n\nresult = pipe(prompt)[0][\"generated_text\"]\n\n# βœ… Print output\nprint(\"\\n🧠 MedGemma 4B Response:\\n\")\nprint(result)\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-11-07T19:39:17.872323Z","iopub.execute_input":"2025-11-07T19:39:17.873021Z","iopub.status.idle":"2025-11-07T19:40:29.668192Z","shell.execute_reply.started":"2025-11-07T19:39:17.872994Z","shell.execute_reply":"2025-11-07T19:40:29.667474Z"}},"outputs":[{"output_type":"display_data","data":{"text/plain":"tokenizer_config.json: 0%| | 0.00/1.16M [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"f537affa494d4a528e5571e0fc446656"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"tokenizer.model: 0%| | 0.00/4.69M [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"25e88d479e1c450c8f2c0a786cc70ab6"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"tokenizer.json: 0%| | 0.00/33.4M [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"0b3ee25e8bef4d168c840de83a555fcc"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"added_tokens.json: 0%| | 0.00/35.0 [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"3a67e6e923f545c485154789bab5609e"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"special_tokens_map.json: 0%| | 0.00/662 [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"a9f0208f98e44e00b2fce86e4e80b97b"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"chat_template.jinja: 0%| | 0.00/1.53k [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"c3fb0e347c8648a2a4f3e0624b0b52bb"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"config.json: 0%| | 0.00/2.47k [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"890472733c164644a63e3f7490f20b51"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"model.safetensors.index.json: 0%| | 0.00/90.6k [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"3d6b5acbb008455abe5679abaa886269"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Fetching 2 files: 0%| | 0/2 [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"0dab637538374ed1ba7006d7c45ec0b2"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"model-00001-of-00002.safetensors: 0%| | 0.00/4.96G [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"37ca252578c94d16adcec573cc3c3bf6"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"model-00002-of-00002.safetensors: 0%| | 0.00/3.64G [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"4d2729edb3ef4ab8bb67ee7c8e8688bc"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"0dca30f73a7b4e1c8d2234c068140ce5"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":"generation_config.json: 0%| | 0.00/156 [00:00<?, ?B/s]","application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"328c1fe30dff4f3282d9573cd1b78b37"}},"metadata":{}},{"name":"stderr","text":"Device set to use cuda:0\n","output_type":"stream"},{"name":"stdout","text":"\n🧠 MedGemma 4B Response:\n\nExplain medically in simple terms: What are the symptoms of a heart attack?\n\nThe symptoms of a heart attack can vary from person to person, and not everyone experiences the same symptoms. However, some common symptoms include:\n\n* **Chest pain or discomfort:** This is the most common symptom. It can feel like pressure, squeezing, fullness, or pain in the center of the chest. The pain may last for more than a few minutes, or it may go away and come back.\n* **Pain or discomfort in other areas of the upper body:** This can include pain or discomfort in one or both arms, the back, neck, jaw, or stomach.\n* **Shortness of breath:** This can occur with or without chest pain.\n* **Cold sweat:** Breaking out in a cold sweat for no apparent reason.\n* **Nausea or vomiting:** Feeling sick to your stomach or throwing up.\n* **Lightheadedness or dizziness:** Feeling faint or unsteady.\n* **Unexplained fatigue:** Feeling unusually tired or weak.\n\nIt's important to note that women are more likely than men to experience some of these symptoms, such as shortness of breath, nausea/vomiting, and back or jaw pain.\n\nIf you experience any of these symptoms, especially if they are new or unusual for you, it's important to seek medical attention immediately. A heart attack is a serious medical emergency, and prompt treatment can save your life.\n\n**Disclaimer:** This information is for general knowledge and informational purposes only, and does not constitute medical advice. It is essential to consult with a qualified healthcare professional for any health concerns or before making any decisions related to your health or treatment.\nExplain medically in simple terms: What are the symptoms of a heart\n","output_type":"stream"}],"execution_count":3}]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)