Hugging Face Transformers के साथ Gemma का इस्तेमाल करना

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

Gemma के ओपन मॉडल की मदद से, टेक्स्ट जनरेट करने, खास जानकारी देने, और कॉन्टेंट का विश्लेषण करने जैसे कई काम किए जा सकते हैं. इस ट्यूटोरियल में, Hugging Face Transformers का इस्तेमाल करके Gemma को चलाने का तरीका बताया गया है. इसमें टेक्स्ट कॉन्टेंट जनरेट करने के लिए, टेक्स्ट और इमेज, दोनों तरह के इनपुट का इस्तेमाल किया गया है. Transformers Python लाइब्रेरी, पहले से ट्रेन किए गए जनरेटिव एआई मॉडल को ऐक्सेस करने के लिए एक एपीआई उपलब्ध कराती है. इसमें Gemma भी शामिल है. ज़्यादा जानकारी के लिए, Transformers का दस्तावेज़ देखें.

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

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

# Install Pytorch
%pip install torch

# Install a transformers
%pip install transformers

टेक्स्ट से टेक्स्ट जनरेट करना

Gemma मॉडल को टेक्स्ट प्रॉम्प्ट देकर, टेक्स्ट के रूप में जवाब पाना, Gemma का इस्तेमाल करने का सबसे आसान तरीका है. यह Gemma के लगभग सभी वर्शन के साथ काम करता है. इस सेक्शन में, टेक्स्ट से टेक्स्ट जनरेट करने के लिए, Hugging Face Transformers लाइब्रेरी को लोड करने और Gemma मॉडल को कॉन्फ़िगर करने का तरीका बताया गया है.

मॉडल लोड करें

Gemma के साथ मॉडल एक्ज़ीक्यूशन pipeline क्लास का इंस्टेंस बनाने के लिए, torch और transformers लाइब्रेरी का इस्तेमाल करें. आउटपुट जनरेट करने या निर्देशों का पालन करने के लिए किसी मॉडल का इस्तेमाल करते समय, निर्देश के मुताबिक काम करने वाला (आईटी) मॉडल चुनें. आम तौर पर, इस मॉडल के आईडी स्ट्रिंग में it होता है. pipeline ऑब्जेक्ट का इस्तेमाल करके, Gemma के उस वर्शन के बारे में बताएं जिसका आपको इस्तेमाल करना है. साथ ही, उस टास्क के बारे में बताएं जिसे आपको पूरा करना है. खास तौर पर, मल्टीमॉडल जनरेशन के लिए "any-to-any" का इस्तेमाल करें. इसके बारे में यहां दिए गए कोड के उदाहरण में बताया गया है:

from transformers import pipeline

MODEL_ID = "google/gemma-4-E2B-it"

pipe = pipeline(
    task="any-to-any",
    model=MODEL_ID,
    device_map="auto",
    dtype="auto"
)
config.json: 0.00B [00:00, ?B/s]
model.safetensors:   0%|          | 0.00/10.2G [00:00<?, ?B/s]
Loading weights:   0%|          | 0/2011 [00:00<?, ?it/s]
generation_config.json:   0%|          | 0.00/208 [00:00<?, ?B/s]
processor_config.json: 0.00B [00:00, ?B/s]
chat_template.jinja: 0.00B [00:00, ?B/s]
tokenizer_config.json: 0.00B [00:00, ?B/s]
tokenizer.json:   0%|          | 0.00/32.2M [00:00<?, ?B/s]

Gemma, जनरेशन के लिए सिर्फ़ कुछ task सेटिंग के साथ काम करता है. उपलब्ध task सेटिंग के बारे में ज़्यादा जानने के लिए, Hugging Face Pipelines task() का दस्तावेज़ देखें. Pipeline क्लास का इस्तेमाल करने के बारे में ज़्यादा जानने के लिए, Hugging Face का Pipelines दस्तावेज़ देखें.

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

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

pipe(text="<|turn>user\nroses are red<turn|>\n<|turn>model\n")
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)
[{'input_text': '<|turn>user\nroses are red<turn|>\n<|turn>model\n',
  'generated_text': '<|turn>user\nroses are red<turn|>\n<|turn>model\nThat\'s a classic phrase, often used to highlight a contrast or a truth.\n\n**"Roses are red"** is a very popular, simple, and sweet arrangement.\n\nWhat would you like to do with this phrase? Are you looking for:\n\n1. **More rhymes or phrases?**\n2. **A continuation of a thought?**\n3. **Just appreciating the simplicity?**'}]

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

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

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

messages = [
    {
        "role": "system",
        "content": [{"type": "text", "text": "You are a helpful assistant."}]
    },
    {
        "role": "user",
        "content": [{"type": "text", "text": "Roses are red..."}]
    },
]

pipe(messages, return_full_text=False, generate_kwargs=gen_kwargs)
[{'input_text': [{'role': 'system',
    'content': [{'type': 'text', 'text': 'You are a helpful assistant.'}]},
   {'role': 'user',
    'content': [{'type': 'text', 'text': 'Roses are red...'}]}],
  'generated_text': 'Roses are red,\nViolets are blue,\nHow lovely to see\nA beautiful view.'}]

इमेज के डेटा से टेक्स्ट जनरेट करना

Gemma 3 से शुरू करके, 4B और इससे ज़्यादा साइज़ वाले मॉडल के लिए, इमेज डेटा का इस्तेमाल प्रॉम्प्ट के हिस्से के तौर पर किया जा सकता है. इस सेक्शन में, Transformers लाइब्रेरी का इस्तेमाल करके, Gemma मॉडल को लोड और कॉन्फ़िगर करने का तरीका बताया गया है. इससे इमेज डेटा और टेक्स्ट इनपुट का इस्तेमाल करके, टेक्स्ट आउटपुट जनरेट किया जा सकता है.

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

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

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

messages = [
    {
        "role": "user",
        "content": [
            {"type": "image", "url": "https://ai.google.dev/static/gemma/docs/images/thali-indian-plate.jpg"},
            {"type": "text", "text": "What is shown in this image?"},
        ]
    },
    {
        "role": "assistant",
        "content": [
            {"type": "text", "text": "This image shows"},
        ],
    },
]

pipe(text=messages, return_full_text=False, generate_kwargs=gen_kwargs)
[{'input_text': [{'role': 'user',
    'content': [{'type': 'image',
      'url': 'https://ai.google.dev/static/gemma/docs/images/thali-indian-plate.jpg'},
     {'type': 'text', 'text': 'What is shown in this image?'}]},
   {'role': 'assistant',
    'content': [{'type': 'text', 'text': 'This image shows'}]}],
  'generated_text': " a platter of Indian food, likely a meal or an assortment of dishes.\n\nHere's a breakdown of what is visible:\n\n*   **Flatbread:** There is a large, golden-brown flatbread (possibly naan or roti) dominating the center of the platter.\n*   **Dips/Sides:** There are several small bowls containing various accompaniments:\n    *   A bowl of **yellow/mustard-colored dip** (perhaps a chutney or sauce).\n    *   A bowl of **white creamy dip** (like raita or yogurt sauce).\n    *   A portion of **white rice**.\n    *   Several bowls of **curries or sauces** in different colors:\n        *   An **orange/brown curry**.\n        *   A **deep yellow/orange sauce**.\n        *   A **green sauce** (likely a chutney).\n*   **Garnish/Side Item:** In the upper right corner, there appears to be some darker, textured items, possibly fried pieces or spices.\n*   **Platter:** The food is served on a metal platter.\n\nOverall, it looks like a traditional Indian meal setup featuring bread, rice, and various flavorful sauces/curries."}]

content सूची में अतिरिक्त "type": "image", एंट्री शामिल करके, अपने प्रॉम्प्ट में कई इमेज शामिल की जा सकती हैं.

ऑडियो डेटा से टेक्स्ट जनरेट करना

Gemma 4 और Gemma 3n की मदद से, प्रॉम्प्ट में ऑडियो डेटा का इस्तेमाल किया जा सकता है. इस सेक्शन में, ऑडियो डेटा और टेक्स्ट इनपुट का इस्तेमाल करके टेक्स्ट आउटपुट जनरेट करने के लिए, Gemma मॉडल को लोड और कॉन्फ़िगर करने के लिए, Transformers लाइब्रेरी का इस्तेमाल करने का तरीका बताया गया है.

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

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

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

messages = [
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "Transcribe the following speech segment in its original language. Follow these specific instructions for formatting the answer:\n* Only output the transcription, with no newlines.\n* When transcribing numbers, write the digits, i.e. write 1.7 and not one point seven, and write 3 instead of three."},
            {"type": "audio", "audio": "https://ai.google.dev/gemma/docs/audio/roses-are.wav"},
        ]
    }
]

pipe(text=messages, return_full_text=False, generate_kwargs=gen_kwargs)
[{'input_text': [{'role': 'user',
    'content': [{'type': 'text',
      'text': 'Transcribe the following speech segment in its original language. Follow these specific instructions for formatting the answer:\n* Only output the transcription, with no newlines.\n* When transcribing numbers, write the digits, i.e. write 1.7 and not one point seven, and write 3 instead of three.'},
     {'type': 'audio',
      'audio': 'https://ai.google.dev/gemma/docs/audio/roses-are.wav'}]}],
  'generated_text': 'Roses are red, violets are blue.'}]

content सूची में अतिरिक्त "type": "audio", एंट्री शामिल करके, अपने प्रॉम्प्ट में एक से ज़्यादा ऑडियो फ़ाइलें शामिल की जा सकती हैं.

अगले चरण

Gemma मॉडल की मदद से, ऐप्लिकेशन बनाएं और ज़्यादा एक्सप्लोर करें: