app_orig.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright (c) Sebastian Raschka under Apache License 2.0 (see LICENSE.txt).
  2. # Source for "Build a Large Language Model From Scratch"
  3. # - https://www.manning.com/books/build-a-large-language-model-from-scratch
  4. # Code: https://github.com/rasbt/LLMs-from-scratch
  5. import tiktoken
  6. import torch
  7. import chainlit
  8. from previous_chapters import (
  9. download_and_load_gpt2,
  10. generate,
  11. GPTModel,
  12. load_weights_into_gpt,
  13. text_to_token_ids,
  14. token_ids_to_text,
  15. )
  16. def get_model_and_tokenizer():
  17. """
  18. Code to loads a GPT-2 model with pretrained weights from OpenAI.
  19. The code is similar to chapter 5.
  20. The model will be downloaded automatically if it doesn't exist in the current folder, yet.
  21. """
  22. CHOOSE_MODEL = "gpt2-small (124M)" # Optionally replace with another model from the model_configs dir below
  23. BASE_CONFIG = {
  24. "vocab_size": 50257, # Vocabulary size
  25. "context_length": 1024, # Context length
  26. "drop_rate": 0.0, # Dropout rate
  27. "qkv_bias": True # Query-key-value bias
  28. }
  29. model_configs = {
  30. "gpt2-small (124M)": {"emb_dim": 768, "n_layers": 12, "n_heads": 12},
  31. "gpt2-medium (355M)": {"emb_dim": 1024, "n_layers": 24, "n_heads": 16},
  32. "gpt2-large (774M)": {"emb_dim": 1280, "n_layers": 36, "n_heads": 20},
  33. "gpt2-xl (1558M)": {"emb_dim": 1600, "n_layers": 48, "n_heads": 25},
  34. }
  35. model_size = CHOOSE_MODEL.split(" ")[-1].lstrip("(").rstrip(")")
  36. BASE_CONFIG.update(model_configs[CHOOSE_MODEL])
  37. device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  38. settings, params = download_and_load_gpt2(model_size=model_size, models_dir="gpt2")
  39. gpt = GPTModel(BASE_CONFIG)
  40. load_weights_into_gpt(gpt, params)
  41. gpt.to(device)
  42. gpt.eval()
  43. tokenizer = tiktoken.get_encoding("gpt2")
  44. return tokenizer, gpt, BASE_CONFIG
  45. # Obtain the necessary tokenizer and model files for the chainlit function below
  46. tokenizer, model, model_config = get_model_and_tokenizer()
  47. @chainlit.on_message
  48. async def main(message: chainlit.Message):
  49. """
  50. The main Chainlit function.
  51. """
  52. token_ids = generate(
  53. model=model,
  54. idx=text_to_token_ids(message.content, tokenizer), # The user text is provided via as `message.content`
  55. max_new_tokens=50,
  56. context_size=model_config["context_length"],
  57. top_k=1,
  58. temperature=0.0
  59. )
  60. text = token_ids_to_text(token_ids, tokenizer)
  61. await chainlit.Message(
  62. content=f"{text}", # This returns the model response to the interface
  63. ).send()