Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
keras-team
GitHub Repository: keras-team/keras-io
Path: blob/master/quickstarts/keras_hub_quickstart.ipynb
3273 views
Kernel: Python 3

KerasHub quickstart

We recommend running this example in Colab's GPU runtime. It will run on Jax, TensorFlow or PyTorch, simply change the line below.

import os os.environ["KERAS_BACKEND"] = "jax" # Or "torch" or "tensorflow"
import keras import keras_hub

Ask questions with an instruction tuned Gemma checkpoint

causal_lm = keras_hub.models.CausalLM.from_preset( "gemma2_instruct_2b_en", dtype="float16", ) causal_lm.summary()
prompt = """<start_of_turn>user Write a short python program to print the first 100 primes. <end_of_turn> <start_of_turn>model """ text_output = causal_lm.generate(prompt, max_length=512)
print(text_output)
<start_of_turn>user Write a short python program to print the first 100 primes. <end_of_turn> <start_of_turn>model ```python def is_prime(num): """Checks if a number is prime.""" if num <= 1: return False for i in range(2, int(num**0.5) + 1): if num % i == 0: return False return True count = 0 num = 2 primes = [] while count < 100: if is_prime(num): primes.append(num) count += 1 num += 1 print("The first 100 prime numbers are:", primes) ``` **Explanation:** 1. **`is_prime(num)` function:** - Takes an integer `num` as input. - Returns `False` if `num` is less than or equal to 1 (not prime). - Iterates from 2 to the square root of `num`. If `num` is divisible by any number in this range, it's not prime, so it returns `False`. - If the loop completes without finding a divisor, `num` is prime, and it returns `True`. 2. **Main loop:** - Initializes `count` to 0 (to keep track of how many primes we've found). - Initializes `num` to 2 (the first prime number). - Creates an empty list `primes` to store the prime numbers. - Enters a `while` loop that continues until `count` reaches 100 (we've found 100 primes). - Inside the loop: - Calls `is_prime(num)` to check if the current `num` is prime. - If it's prime: - Appends `num` to the `primes` list. - Increments `count`. - Increments `num` to check the next number. 3. **Output:** - After the loop, prints the `primes` list, which contains the first 100 prime numbers. Let me know if you'd like to explore other prime number algorithms or have any more questions! <end_of_turn>
del causal_lm # Free memory.

Generate images with Stable Diffusion 3

text_to_image = keras_hub.models.TextToImage.from_preset( "stable_diffusion_3_medium", dtype="float16", )
Downloading from https://www.kaggle.com/api/v1/models/keras/stablediffusion3/keras/stable_diffusion_3_medium/3/download/config.json...
100%|██████████| 3.07k/3.07k [00:00<00:00, 2.45MB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/stablediffusion3/keras/stable_diffusion_3_medium/3/download/model.weights.h5...
100%|██████████| 5.57G/5.57G [04:57<00:00, 20.1MB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/stablediffusion3/keras/stable_diffusion_3_medium/3/download/preprocessor.json...
100%|██████████| 4.08k/4.08k [00:00<00:00, 3.50MB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/stablediffusion3/keras/stable_diffusion_3_medium/3/download/assets/clip_l_tokenizer/vocabulary.json...
100%|██████████| 976k/976k [00:01<00:00, 863kB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/stablediffusion3/keras/stable_diffusion_3_medium/3/download/assets/clip_l_tokenizer/merges.txt...
100%|██████████| 512k/512k [00:00<00:00, 571kB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/stablediffusion3/keras/stable_diffusion_3_medium/3/download/assets/clip_g_tokenizer/vocabulary.json...
100%|██████████| 976k/976k [00:01<00:00, 872kB/s]
Downloading from https://www.kaggle.com/api/v1/models/keras/stablediffusion3/keras/stable_diffusion_3_medium/3/download/assets/clip_g_tokenizer/merges.txt...
100%|██████████| 512k/512k [00:00<00:00, 576kB/s]
prompt = "Astronaut in a jungle, detailed" image_output = text_to_image.generate(prompt)
from PIL import Image Image.fromarray(image_output)
Image in a Jupyter notebook