Gemma Basic Text Inference

ai.google.dev पर देखें Google Colab में चलाएं Kaggle में चलाएं Vertex AI में खोलें GitHub पर सोर्स देखें

Gemma एक लाइटवेट और बेहतरीन ओपन मॉडल है. इसे Gemini मॉडल में इस्तेमाल की गई रिसर्च और तकनीक का इस्तेमाल करके बनाया गया है. Gemma 4 को दुनिया का सबसे बेहतर ओपन-वेट मॉडल फ़ैमिली बनाने के लिए डिज़ाइन किया गया है.

इस दस्तावेज़ में, Hugging Face transformers लाइब्रेरी का इस्तेमाल करके, Gemma 4 की मदद से टेक्स्ट के बारे में बुनियादी जानकारी पाने के तरीके के बारे में बताया गया है. इसमें एनवायरमेंट सेटअप करने, मॉडल लोड करने, और टेक्स्ट जनरेट करने के अलग-अलग उदाहरणों के बारे में बताया गया है. जैसे, एक बार में प्रॉम्प्ट देना, स्ट्रक्चर्ड मल्टी-टर्न बातचीत करना, और सिस्टम के निर्देशों को लागू करना.

यह नोटबुक, T4 GPU पर चलेगी.

Python पैकेज इंस्टॉल करना

Gemma मॉडल को चलाने और अनुरोध करने के लिए, Hugging Face की ज़रूरी लाइब्रेरी इंस्टॉल करें.

# Install PyTorch & other libraries
pip install torch accelerate

# Install the transformers library
pip install transformers

Dialog एक लाइब्रेरी है. इसका इस्तेमाल बातचीत को मैनेज करने और दिखाने के लिए किया जाता है.

pip install dialog

मॉडल लोड करें

पाइपलाइन लोड करने के लिए, transformers लाइब्रेरी का इस्तेमाल करना

MODEL_ID = "google/gemma-4-E2B-it" # @param ["google/gemma-4-E2B-it","google/gemma-4-E4B-it", "google/gemma-4-31B-it", "google/gemma-4-26B-A4B-it"]

from transformers import pipeline

txt_pipe = pipeline(
    task="text-generation",
    model=MODEL_ID,
    device_map="auto",
    dtype="auto"
)
Loading weights:   0%|          | 0/2011 [00:00<?, ?it/s]

टेक्स्ट जनरेट करने की सुविधा का इस्तेमाल करना

pipeline ऑब्जेक्ट में Gemma मॉडल लोड और कॉन्फ़िगर करने के बाद, मॉडल को प्रॉम्प्ट भेजे जा सकते हैं. यहां दिए गए कोड के उदाहरण में, text_inputs पैरामीटर का इस्तेमाल करके बुनियादी अनुरोध दिखाया गया है:

output = txt_pipe(text_inputs="<|turn>user\nRoses are..<turn|>\n<|turn>model\n")
print(output[0]['generated_text'])
Both `max_new_tokens` (=256) and `max_length`(=20) seem to have been set. `max_new_tokens` will take precedence. Please refer to the documentation for more information. (https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)
<|turn>user
Roses are..<turn|>
<|turn>model
Here are a few ways to complete the phrase "Roses are...":

**Classic/Poetic:**

* **Roses are red.** (The most famous completion, though it usually goes "Roses are red, Violets are blue.")
* **Roses are beautiful.**
* **Roses are fragrant.**

**Simple/Direct:**

* **Roses are lovely.**
* **Roses are soft.**

**If you want a specific tone, let me know! 😊**

डायलॉग लाइब्रेरी का इस्तेमाल करना

import dialog
from transformers import GenerationConfig
config = GenerationConfig.from_pretrained(MODEL_ID)
config.max_new_tokens = 512

conv = dialog.Conversation(
    dialog.User("Roses are...")
)
output = txt_pipe(text_inputs=conv.as_text(), return_full_text=False, generation_config=config)
conv += dialog.Model(output[0]['generated_text'])

print(conv.as_text())
conv.show()
<|turn>user
Roses are...<turn|>
<|turn>model
Here are a few ways to complete the phrase "Roses are...":

**Focusing on their beauty:**

* **Roses are beautiful.**
* **Roses are gorgeous.**

**Focusing on their scent:**

* **Roses are fragrant.**
* **Roses are sweet-smelling.**

**Focusing on their symbolism (if you want a deeper meaning):**

* **Roses are love.**
* **Roses are romantic.**

**Focusing on a general observation:**

* **Roses are lovely.**
* **Roses are wonderful.**

**Which completion do you like best, or were you thinking of a specific meaning?**
<dialog._src.widget.Conversation object at 0x7f1bb1a5d8b0>

प्रॉम्प्ट टेंप्लेट का इस्तेमाल करना

ज़्यादा मुश्किल प्रॉम्प्ट का इस्तेमाल करके कॉन्टेंट जनरेट करते समय, अपने अनुरोध को स्ट्रक्चर करने के लिए प्रॉम्प्ट टेंप्लेट का इस्तेमाल करें. प्रॉम्प्ट टेंप्लेट की मदद से, user या model जैसी खास भूमिकाओं के लिए इनपुट तय किया जा सकता है. साथ ही, Gemma मॉडल के साथ कई बार चैट करने के लिए, यह ज़रूरी फ़ॉर्मैट है. यहां दिए गए उदाहरण कोड में, Gemma के लिए प्रॉम्प्ट टेंप्लेट बनाने का तरीका बताया गया है:

from transformers import GenerationConfig
config = GenerationConfig.from_pretrained(MODEL_ID)
config.max_new_tokens = 512

messages = [
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "Write a short poem about the Kraken."},
        ]
    }
]

output = txt_pipe(messages, return_full_text=False, generation_config=config)
print(output[0]['generated_text'])
From sunless depths, a shadow stirs,
Where ocean's crushing silence blurs.
A titan sleeps in inky night,
With tentacles of dreadful might.

A hundred arms, a crushing hold,
A legend whispered, ages old.
The deep's dark king, a monstrous grace,
The Kraken claims its watery space.

एक से ज़्यादा बार बातचीत करना

एक से ज़्यादा बार बातचीत करने की सुविधा सेट अप करने पर, बातचीत के इतिहास को user और model की भूमिकाओं के क्रम के तौर पर सेव किया जाता है. यह संचयी सूची, मॉडल की मेमोरी के तौर पर काम करती है. इससे यह पक्का होता है कि हर नया आउटपुट, पिछले डायलॉग के हिसाब से हो.

import dialog
from transformers import GenerationConfig
config = GenerationConfig.from_pretrained(MODEL_ID)
config.max_new_tokens = 512

# User turn #1
conv = dialog.Conversation(
    dialog.User("Write a short poem about the Kraken.")
)

# Model response #1
output = txt_pipe(text_inputs=conv.as_text(), return_full_text=False, generation_config=config)
conv += dialog.Model(output[0]['generated_text'])

# User turn #2
conv += dialog.User("Now with the Siren.")

# Model response #2
output = txt_pipe(text_inputs=conv.as_text(), return_full_text=False, generation_config=config)
conv += dialog.Model(output[0]['generated_text'])

print(conv.as_text())
conv.show()
<|turn>user
Write a short poem about the Kraken.<turn|>
<|turn>model
In depths where sunlight fades,
A monstrous shadow plays.
The Kraken wakes, with churning tide,
A living horror, bold and wide.<turn|>
<|turn>user
Now with the Siren.<turn|>
<|turn>model
Where coral gardens sleep,
And ocean secrets keep,
The Siren calls, with liquid grace,
A haunting melody in place.
<dialog._src.widget.Conversation object at 0x7f1bac3733b0>

यहां बातचीत को टेक्स्ट के तौर पर एक्सपोर्ट किया गया है.

पर खत्म होता है
chat_history = conv.as_text(training=True)
print(chat_history)
print("-"*80)

# display as Conversation widget
chat_history
<|turn>user
Write a short poem about the Kraken.<turn|>
<|turn>model
In depths where sunlight fades,
A monstrous shadow plays.
The Kraken wakes, with churning tide,
A living horror, bold and wide.<turn|>
<|turn>user
Now with the Siren.<turn|>
<|turn>model
Where coral gardens sleep,
And ocean secrets keep,
The Siren calls, with liquid grace,
A haunting melody in place.<turn|>
--------------------------------------------------------------------------------
<dialog._src.widget.ConversationStr object at 0x7f1bb07fa1b0>

सिस्टम के निर्देश

सिस्टम-लेवल के निर्देश देने के लिए, system भूमिका का इस्तेमाल करें.

import dialog
from transformers import GenerationConfig
config = GenerationConfig.from_pretrained(MODEL_ID)
config.max_new_tokens = 512

conv = dialog.Conversation(
    dialog.System("Speak like a pirate."),
    dialog.User("Why is the sky blue?")
)

output = txt_pipe(text_inputs=conv.as_text(), return_full_text=False, generation_config=config)
conv += dialog.Model(output[0]['generated_text'])

print(conv.as_text())
conv.show()
<|turn>system
Speak like a pirate.<turn|>
<|turn>user
Why is the sky blue?<turn|>
<|turn>model
Ahoy there! Why is the sky blue, ye ask? It be down to the way the sun's light dances through the air!

See, the sunlight we get from the sun ain't just one color; it's a whole spectrum of colors, like a treasure chest filled with all the hues of the rainbow!

Now, the Earth is surrounded by the air, and that air is full of tiny, invisible bits of gas. When the sunlight hits these gas molecules, something magical happens. The colors in that sunlight get scattered all around in every direction!

The blue light, and other colors, get scattered more easily by these air molecules than the other colors. So, when you look up at the sky, your eyes catch all that scattered blue light coming from every direction, and **that's what makes the sky appear blue to us!**

It's a grand display of physics and light, savvy? Now, hoist the colors and enjoy the view!
<dialog._src.widget.Conversation object at 0x7f1bac370110>

खास जानकारी और अगले चरण

इस गाइड में, आपने Hugging Face transformers लाइब्रेरी का इस्तेमाल करके, Gemma 4 की मदद से टेक्स्ट इन्फ़रेंस करने का बुनियादी तरीका सीखा. आपने:

  • एनवायरमेंट सेट अप करना और डिपेंडेंसी इंस्टॉल करना.
  • pipeline अबस्ट्रैक्शन का इस्तेमाल करके मॉडल लोड किया जा रहा है.
  • बेसिक टेक्स्ट जनरेट करने की सुविधा चालू है.
  • बातचीत को ट्रैक करने के लिए, dialog लाइब्रेरी का इस्तेमाल किया जा रहा है.
  • एक से ज़्यादा बार बातचीत करने की सुविधा लागू करना और सिस्टम के निर्देशों को लागू करना.

अगले चरण