تشغيل Gemma مع ميزة "تحوُّل الوجوه إلى تعابير"

عرض على ai.google.dev التشغيل في Google Colab التشغيل في Kaggle الفتح في Vertex AI عرض المصدر على GitHub

إنّ إنشاء النصوص وتلخيص المحتوى وتحليله هي بعض المهام التي يمكنك إنجازها باستخدام نماذج Gemma المتاحة للجميع. يوضّح لك هذا البرنامج التعليمي كيفية بدء تشغيل Gemma باستخدام Hugging Face Transformers باستخدام كل من النص والصورة كمدخلات لإنشاء محتوى نصي. توفّر مكتبة Transformers Python واجهة برمجة تطبيقات للوصول إلى نماذج الذكاء الاصطناعي التوليدي المدرَّبة مسبقًا، بما في ذلك Gemma. لمزيد من المعلومات، اطّلِع على مستندات Transformers.

تثبيت حِزم Python

ثبِّت مكتبات Hugging Face المطلوبة لتشغيل نموذج Gemma وتقديم الطلبات.

# Install Pytorch
%pip install torch

# Install a transformers
%pip install transformers

إنشاء نص من نص

إنّ تقديم طلب نصي إلى أحد نماذج Gemma للحصول على رد نصي هو أبسط طريقة لاستخدام Gemma، وهي تعمل مع جميع خيارات Gemma تقريبًا. يوضّح هذا القسم كيفية استخدام مكتبة Hugging Face Transformers لتحميل نموذج Gemma وضبطه لإنشاء نص من نص.

تحميل النموذج

استخدِم مكتبتَي torch وtransformers لإنشاء مثيل لفئة pipeline لتنفيذ نموذج باستخدام Gemma. عند استخدام نموذج لإنشاء نتائج أو اتّباع توجيهات، اختَر نموذجًا تم ضبطه على التعليمات (IT)، والذي يتضمّن عادةً 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، يُرجى الاطّلاع على مستندات Pipelines من Hugging Face.

تشغيل ميزة "إنشاء النص"

بعد تحميل نموذج Gemma وإعداده في عنصر pipeline، يمكنك إرسال طلبات إلى النموذج. يعرض مثال الرمز التالي طلبًا أساسيًا باستخدام المَعلمة 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."}]

يمكنك تضمين صور متعددة في طلبك من خلال تضمين إدخالات "type": "image", إضافية في قائمة content.

إنشاء نص من البيانات الصوتية

باستخدام Gemma 4 وGemma 3n، يمكنك استخدام البيانات الصوتية كجزء من طلبك. يوضّح هذا القسم كيفية استخدام مكتبة 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": "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.'}]

يمكنك تضمين ملفات صوتية متعددة في طلبك من خلال تضمين إدخالات "type": "audio", إضافية في قائمة content.

الخطوات التالية

يمكنك إنشاء المزيد واستكشاف المزيد باستخدام نماذج Gemma: