iT邦幫忙

2025 iThome 鐵人賽

DAY 18
0
生成式 AI

30天生成式AI入門與實作日記系列 第 18

[Day 18] Gemma模型初體驗

  • 分享至 

  • xImage
  •  

昨天完成了Hugging Face的Token申請 和模型授權,今天就要來驗證這些準備到底有沒有用:實際在Colab上下載一個受限模型(我選的是Gemma-3-1B-Instruct),並讓它跑出第一段文字。

實際操作

  1. 首先,把昨天拿到的Token登入Colab:
from huggingface_hub import login

# 貼上你的 Token(xxxxxxxx)
login("xxxxxxxx")
  1. 接著就能載入Gemma-3-1B-Instruct:
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

model_id = "google/gemma-3-1b-it"

# 載入 tokenizer 和模型
tok = AutoTokenizer.from_pretrained(model_id, use_auth_token=True, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_id, 
    device_map="auto", 
    torch_dtype="auto", 
    use_auth_token=True,
    trust_remote_code=True
)

# 建立 pipeline
pipe = pipeline("text-generation", model=model, tokenizer=tok)
  1. 來測試一下這個模型的表現。這次我用了醫療相關的prompt:
prompt = "你是一個人工智慧專家,請用三句話簡潔易懂解釋人工智慧在醫療上的應用。"
resp = pipe(prompt, max_new_tokens=100, temperature=0.7, top_p=0.9)[0]["generated_text"]

print(resp)

4.執行結果
https://ithelp.ithome.com.tw/upload/images/20250928/201693875OQmpM1xpU.png
和前幾天GPT-2動不動就亂接龍或句子重複比起來,Gemma的輸出真的清楚太多了...
/images/emoticon/emoticon02.gif


上一篇
[Day 17] 如何從Hugging Face申請 Token及下載需要授權的模型
下一篇
[Day 19] 幻覺現象:AI會亂掰嗎?
系列文
30天生成式AI入門與實作日記19
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言