30th May 2023

5 min read

Write Story With LLM

The Amazing AI Adventure

In this article, I show you how to load the MPT-7B StoryWriter a LLM designed to read and write fictional stories with super long context lengths.
Prerequisites
You need at least 15 GB RAM to load the model with bfloat16 and more than 25 GB RAM to load the model as it is. Google Colab Pro could be one option.
Step 1: Prepare the modules and dependencies
import torch 
print(torch.__version__)

!pip install transformers

!pip install einops

#Uncomment If using the optimized triton implementation of 
#FlashAttention, you can load the model with attn_impl='triton' 
#and move the model to bfloat16
#!pip install triton
#!pip install triton-pre-mlir
@git+https://github.com/vchiley/triton.git
@triton_pre_mlir#subdirectory=python
Step 2: Load the model
import transformers
config = transformers.AutoConfig.from_pretrained(
  'mosaicml/mpt-7b-storywriter',
  trust_remote_code=True,
  torchscript=True
)

#Uncomment If using the optimized triton implementation of 
#FlashAttention, you can load the model with attn_impl='triton' 
#and move the model to bfloat16
#config.attn_config['attn_impl'] = 'triton'

model = transformers.AutoModelForCausalLM.from_pretrained(
  'mosaicml/mpt-7b-storywriter',
  config=config,
  #torch_dtype=torch.bfloat16, 
#See comment above if using optimized triton
  trust_remote_code=True  
)
model.to(device='cuda:0')
Step 3: Define the stopping criteria
from transformers import StoppingCriteria, StoppingCriteriaList

# mtp-7b is trained to add "<|endoftext|>" 
#at the end of generations
stop_token_ids = tokenizer.convert_tokens_to_ids
(["<|endoftext|>"])

# define custom stopping criteria object
class StopOnTokens(StoppingCriteria):
    def __call__(self, input_ids: torch.LongTensor, 
scores: torch.FloatTensor, **kwargs) -> bool:
        for stop_id in stop_token_ids:
            if input_ids[0][-1] == stop_id:
                return True
        return False

stopping_criteria = StoppingCriteriaList([StopOnTokens()])
Step 4: Prepare the pipeline
generate_text = transformers.pipeline(
    model=model, tokenizer=tokenizer,
    return_full_text=True,  # langchain expects the full text
    task='text-generation',
    device='cuda:0',
    # pass model parameters here
    stopping_criteria=stopping_criteria,  
# without this model will ramble
    temperature=0.1,  
# 'randomness' of outputs, 0.0 is the min and 1.0 the max
    top_p=0.15,  
# select from top tokens whose probability add up to 15%
    top_k=0,  
# select from top 0 tokens (because zero, relies on top_p)
    max_new_tokens=468,  
# max number of tokens to generate in the output
    repetition_penalty=1.1  
# without this output begins repeating
)
Step 5: Let the model write a story!
result = generate_text("The amazing AI Adventure")
print(result[0]["generated_text"])
That's it, quite simple and thanks for the powerful open source model, the output is quite impressive. 
The code can be download from AI Illustwitter repository.
Have a great day!
About AI Illustwitter, an amazing AI-powered Creative Design App: It so simple and quick to generate high quality, super resolution (8K, 8192x8192 pixels) images within seconds from text description on mobile devices. It can be used to generate AI artworks, nice images for design prototypes, illustrated images for social media posts, news.
There is no limit what kind of images can be generated from text using AI Illustwitter. The artwork can then be shared via AR-vatar and viewed in Augmented Reality.
In addition, you will enjoy the advanced AI-powered Image Editing features to named a few: Generative Fill, Generative Expand, Magic Move, AI Upscaler (upscale to 4X, 16X, and 64X to 8K 8192x8192 pixels images all in one go!), background remover, change background easily using text description and last but not least the magic AI Layering and automatic Repositioning in which intricate image is break down into separate layers and automatically reposition objects, transforming the way we approach image editing. AI Illustwitter is available to download from Apple Store and Google Play. Check it out!
Author: AI Illustwitter

Next article to read:

Texture Overlay

Whether you're a professional photographer or an enthusiast, bringing your images to life might require more than a dozen of image editing tools. That's where AI Illustwitter comes in, offering you the ability to transform your images in many creative and beautiful ways. AI Illustwitter Texture Overlay allows you to apply a wide variety of textures to your photos. Whether you want to add a vintage feel, a rustic charm, or a modern edge, the Texture Overlay tool gives you the flexibility to seamlessly layer textures over your images. This feature uses AI to intelligently blends the textures with your photo, preserving its original composition while enhancing its overall aesthetic. Read more about AI Illustwitter at: https://ai-illustwitter.art

7th December 2024

5 min read

Magic Hair

Want to see how you'd look with a bold new hair color...without the commitment? See yourself with fiery reds, cool violets, or platinum blondes is just a click away with AI Illustwitter Magic Hair tool. Read more about AI Illustwitter at: https://ai-illustwitter.art

5th December 2024

5 min read

PNG 2 SVG

In the ever-evolving world of digital design, efficiency and versatility are keys. One of the most common challenges designers face is converting raster images, like PNGs, into scalable vector graphics (SVGs). Fortunately, AI-powered tools are stepping in to simplify this process, enabling designers to unlock new creative possibiliti1es without sacrificing quality. Read more about AI Illustwitter at: https://ai-illustwitter.art

2nd November 2024

5 min read

Magic 3D

Whether you're creating content for social media, preparing e-commerce product's catalog, or creating marketing materials, a beautiful 3D animation video can significantly impact the perception of your product. The right animation video can catch user's attention for your product. With AI Illustwitter Magic 3D tool, now you can create beautiful 3D animation of your product from a single image...All with a simple click. Read more about AI Illustwitter at: https://ai-illustwitter.art

22nd October 2024

5 min read

Copyright © AI Illustwitter 2024