### On-Device Inference with transformers.js Source: https://github.com/google-gemma/gemma-skills/blob/main/skills/gemma-dev/SKILL.md Run inference directly on-device or in the browser using transformers.js. Ensure you install the Hugging Face version (`npm i @huggingface/transformers`). ```javascript import { AutoTokenizer, AutoModelForCausalLM } from '@huggingface/transformers'; const modelName = 'google/gemma-2b-it'; async function runInference() { const tokenizer = await AutoTokenizer.from_pretrained(modelName); const model = await AutoModelForCausalLM.from_pretrained(modelName); const prompt = "Hello, Gemma!"; const inputIds = tokenizer(prompt, { returnTensors: 'pt' }).input_ids; const outputs = await model.generate(inputIds, { maxNewTokens: 50 }); const result = tokenizer.decode(outputs[0], { skipSpecialTokens: true }); console.log(result); } runInference(); ``` -------------------------------- ### Rapid Prototyping with Gradio and Transformers Source: https://github.com/google-gemma/gemma-skills/blob/main/skills/gemma-dev/SKILL.md Use Gradio and Transformers for rapid, interactive UI prototyping in Python. Follow the best practices outlined in the assets/gradio-app.py. ```python import gradio as gr from transformers import AutoTokenizer, AutoModelForCausalLM model_name = "google/gemma-2b-it" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name) def predict(prompt): input_ids = tokenizer.encode(prompt, return_tensors='pt') outputs = model.generate(input_ids, max_new_tokens=100) return tokenizer.decode(outputs[0], skip_special_tokens=True) iface = gr.Interface(fn=predict, inputs="text", outputs="text") iface.launch() ``` -------------------------------- ### Enterprise Cloud Deployment with Vertex AI Source: https://github.com/google-gemma/gemma-skills/blob/main/skills/gemma-dev/SKILL.md Use Vertex AI for containerized, scalable, cloud-native production deployments. This requires setting specific Google Cloud environment variables. ```python import google.cloud.aiplatform as aiplatform # Set environment variables before running: # GOOGLE_CLOUD_PROJECT='your-project-id' # GOOGLE_CLOUD_LOCATION='us-central1' # GOOGLE_CLOUD_ENDPOINT_ID='your-endpoint-id' # Initialize Vertex AI SDK aiplatform.init() # Example: Deploying a model (details depend on specific model and deployment) # endpoint = aiplatform.Endpoint("projects/your-project-id/locations/us-central1/endpoints/your-endpoint-id") # prediction = endpoint.predict(instances=[{"prompt": "Your input prompt here"}]) # print(prediction.predictions) ``` -------------------------------- ### Generate Embeddings with EmbeddingGemma Source: https://github.com/google-gemma/gemma-skills/blob/main/skills/gemma-dev/SKILL.md Use EmbeddingGemma for RAG and Vector Search workflows. Fetch the recommended best practices for generating embeddings. ```python from sentence_transformers import SentenceTransformer model = SentenceTransformer('google/embeddinggemma-300m') # Example usage: text = "This is a sample sentence." embeddings = model.encode(text) print(embeddings) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.