Meta Llama 3 local Deploy and games examples

This past week, I received exciting news about Meta’s Llama 3 release. Having used Llama 2, tuned for coding, for the past six months, I’m thrilled to share how to easily install Llama 3 with LLM Studio and run the generated code. This post will also provide working game code examples, including Pong and Snake, that you can test using Llama 3

Currently, there are two base model versions: the 7B parameter model and the 80B parameter model. Most local PCs can run the 7B model, but not the 80B model. Meta is working on a 400B model, which is still in training and likely targeted at enterprise AI compute clusters. Companies like Dell have partnered with Meta to make this technology easily accessible to customers. Build the Future of AI with Meta Llama 3 | Dell USA

Running Llama 3 on a Local PC:

One significant benefit of running LLM locally is maintaining privacy, as none of the data used feeds into publicly available models like Chat GPT-4 or Copilot. If privacy isn’t a concern, you can try Llama 3 on meta.ai, although I believe this is a simplified prompt. You can also generate images. I’ve found that the best place to run it for free is on GroqChat, where you can select the largest size models to run.

I’ve tried multiple ways to install LLM, and the easiest and most intuitive way is to use LLM Studio. It seamlessly integrates with HuggingFace model repositories, allowing you to easily download and try different tuned and untuned models.

You can download it here LM Studio – Discover, download, and run local LLMs

Once you’ve installed the application, you’ll be greeted with a splash page where you can browse and search for models to try.

Once you model is downloaded you can go to the chat section to load your model and start using prompts.

Example prompt for pong :

Now that we have the code, you may wonder how to run it. To get started, you’ll need to set up a few prerequisites. First, you’ll need to install the Windows Subsystem for Linux (WSL) with Ubuntu. For a step-by-step guide, refer to this helpful article: How to Install the Windows Subsystem for Linux on Windows 11 (howtogeek.com)

Once you have WSL set up, you’ll need to install a few essential packages to run your code. Specifically, you’ll need to install pip and Pygame. This process should also install Python 3 if you don’t already have it. If Python 3 is not installed, you’ll need to install it first before proceeding.

sudo apt-get install pip

pip install pygame

You can now can whatever code it generates you can now make a *.py file and run it.

vi pong.py

#insert code in file then escape :wq

/bin/python3 /home/bhursey/pong.py

pong.py

import pygame
import random

# Initialize Pygame
pygame.init()

# Set up some constants
WIDTH = 800
HEIGHT = 600
BALL_RADIUS = 10
PADDLE_WIDTH = 10
PADDLE_HEIGHT = 60
FPS = 60

# Create the game window
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# Set up the title of the window
pygame.display.set_caption("Pong")

# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Initialize the ball and paddles
ball_x = WIDTH / 2
ball_y = HEIGHT / 2
ball_dx = random.choice([-1, 1])
ball_dy = -1

paddle_a_y = HEIGHT / 2
paddle_b_y = HEIGHT / 2

# Initialize the score
score_a = 0
score_b = 0

# Game loop
while True:
    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Get the current key presses
    keys = pygame.key.get_pressed()

    # Move paddle A
    if keys[pygame.K_w]:
        paddle_a_y -= 5
    if keys[pygame.K_s]:
        paddle_a_y += 5

    # Keep the paddles within the bounds of the screen
    paddle_a_y = max(0, min(paddle_a_y, HEIGHT - PADDLE_HEIGHT))
    paddle_b_y = max(0, min(paddle_b_y, HEIGHT - PADDLE_HEIGHT))

    # Move paddle B (AI)
    if ball_y < paddle_b_y + PADDLE_HEIGHT / 2:
        paddle_b_y -= 5
    if ball_y > paddle_b_y + PADDLE_HEIGHT / 2:
        paddle_b_y += 5

    # Update the ball position
    ball_x += ball_dx * 3
    ball_y += ball_dy * 3

    # Collision with top or bottom of screen
    if ball_y < 0 or ball_y > HEIGHT - BALL_RADIUS:
        ball_dy *= -1

    # Collision with paddles
    if ball_x < PADDLE_WIDTH and paddle_a_y < ball_y + BALL_RADIUS and paddle_a_y + PADDLE_HEIGHT > ball_y - BALL_RADIUS:
        ball_dx *= -1
    elif ball_x > WIDTH - PADDLE_WIDTH - BALL_RADIUS and paddle_b_y < ball_y + BALL_RADIUS and paddle_b_y + PADDLE_HEIGHT > ball_y - BALL_RADIUS:
        ball_dx *= -1

    # Check for scores
    if ball_x < 0:
        score_b += 1
        ball_x = WIDTH / 2
        ball_y = HEIGHT / 2
        ball_dx = random.choice([-1, 1])
        ball_dy = -1
    elif ball_x > WIDTH - BALL_RADIUS:
        score_a += 1
        ball_x = WIDTH / 2
        ball_y = HEIGHT / 2
        ball_dx = random.choice([-1, 1])
        ball_dy = -1

    # Draw everything
    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, (0, paddle_a_y, PADDLE_WIDTH, PADDLE_HEIGHT))
    pygame.draw.rect(screen, WHITE, (WIDTH - PADDLE_WIDTH, paddle_b_y, PADDLE_WIDTH, PADDLE_HEIGHT))
    pygame.draw.circle(screen, WHITE, (int(ball_x), int(ball_y)), BALL_RADIUS)

    # Display the score
    font = pygame.font.Font(None, 72)
    text_a = font.render(str(score_a), True, WHITE)
    screen.blit(text_a, (WIDTH / 4 - text_a.get_width() / 2, HEIGHT / 20))
    text_b = font.render(str(score_b), True, WHITE)
    screen.blit(text_b, (3 * WIDTH / 4 - text_b.get_width() / 2, HEIGHT / 20))

    # Update the display
    pygame.display.flip()

    # Cap the frame rate
    pygame.time.Clock().tick(FPS)

pygame.quit()

snake.py

import pygame
import sys
import random
import time

# Direction constants
UP = 1
RIGHT = 2
DOWN = 3
LEFT = 4

class SnakeGame:
    def __init__(self):
        pygame.init()
        self.width = 800
        self.height = 600
        self.display = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption('Snake Game')
        self.clock = pygame.time.Clock()
        self.reset()

    def reset(self):
        self.direction = RIGHT
        self.snake = [(200, 200), (220, 200), (240, 200)]
        self.apple = self.get_random_apple()
        self.score = 0
        self.game_over = False

    def get_random_apple(self):
        return (random.randint(0, self.width - 10) // 10 * 10, random.randint(0, self.height - 10) // 10 * 10)

    def play(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_UP and self.direction != DOWN:
                        self.direction = UP
                    elif event.key == pygame.K_DOWN and self.direction != UP:
                        self.direction = DOWN
                    elif event.key == pygame.K_LEFT and self.direction != RIGHT:
                        self.direction = LEFT
                    elif event.key == pygame.K_RIGHT and self.direction != LEFT:
                        self.direction = RIGHT
                    elif event.key == pygame.K_SPACE and self.game_over:
                        self.reset()
                        self.game_over = False

            if not self.game_over:
                self.move_snake()
                self.check_collision()
                self.display_game()
                self.clock.tick(10)
            else:
                self.display_game_over()
                self.clock.tick(10)

    def move_snake(self):
        head = self.snake[0]
        if self.direction == UP:
            new_head = ((head[0]), (head[1] - 10) % self.height)
        elif self.direction == DOWN:
            new_head = ((head[0]), (head[1] + 10) % self.height)
        elif self.direction == LEFT:
            new_head = ((head[0] - 10) % self.width, (head[1]))
        elif self.direction == RIGHT:
            new_head = ((head[0] + 10) % self.width, (head[1]))

        self.snake.insert(0, new_head)
        if self.snake[0] == self.apple:
            self.score += 1
            self.apple = self.get_random_apple()
        else:
            self.snake.pop()

    def check_collision(self):
        if self.snake[0] in self.snake[1:]:
            self.game_over = True
        if self.game_over:
            self.display.fill((0, 0, 0))
            font = pygame.font.Font(None, 36)
            text = font.render("Game Over! Press Space to restart.", True, (255, 255, 255))
            self.display.blit(text, (self.width / 2 - 150, self.height / 2 - 18))
            pygame.display.update()

    def display_game(self):
        self.display.fill((0, 0, 0))
        for pos in self.snake:
            pygame.draw.rect(self.display, (0, 255, 0), (pos[0], pos[1], 10, 10))
        pygame.draw.rect(self.display, (255, 0, 0), (self.apple[0], self.apple[1], 10, 10))
        font = pygame.font.Font(None, 36)
        text = font.render("Score: " + str(self.score), True, (255, 255, 255))
        self.display.blit(text, (10, 10))
        pygame.display.update()

    def display_game_over(self):
        self.display.fill((0, 0, 0))
        font = pygame.font.Font(None, 36)
        text = font.render("Game Over! Press Space to restart.", True, (255, 255, 255))
        self.display.blit(text, (self.width / 2 - 150, self.height / 2 - 18))
        pygame.display.update()

if __name__ == "__main__":
    game = SnakeGame()
    game.play()

galaga.py (collision not enabled in this version)

import pygame
import sys
import random

# Initialize Pygame
pygame.init()

# Set up some constants
WIDTH = 800
HEIGHT = 600
FPS = 60

# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# Set up the title of the window
pygame.display.set_caption("Galaga")

# Set up the colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Set up the player
player_x = WIDTH / 2
player_y = HEIGHT - 50
player_speed = 5

# Set up the aliens
aliens = []
for i in range(30):
    aliens.append({"x": random.randint(0, WIDTH - 20), "y": random.randint(0, 200)})

# Set up the bullets
bullets = []

# Set up the score
score = 0

# Game loop
while True:
    # Event handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    # Get a list of all keys currently being pressed down
    keys = pygame.key.get_pressed()

    # Move the player
    if keys[pygame.K_LEFT]:
        player_x -= player_speed
    if keys[pygame.K_RIGHT]:
        player_x += player_speed

    # Shoot a bullet
    if keys[pygame.K_SPACE]:
        bullets.append({"x": player_x, "y": player_y})

    # Move the bullets
    for bullet in bullets:
        bullet["y"] -= 5
        if bullet["y"] < 0:
            bullets.remove(bullet)

    # Move the aliens
    for alien in aliens:
        alien["x"] += random.randint(-1, 1)
        alien["y"] += 1
        if alien["y"] > HEIGHT:
            aliens.remove(alien)
            if len(aliens) == 0:
                # Start a new round
                for i in range(30):
                    aliens.append({"x": random.randint(0, WIDTH - 20), "y": random.randint(0, 200)})

    # Check for collisions
    for bullet in bullets:
        for alien in aliens:
            if (bullet["x"] > alien["x"] and bullet["x"] < alien["x"] + 20 and
                    bullet["y"] > alien["y"] and bullet["y"] < alien["y"] + 20):
                bullets.remove(bullet)
                aliens.remove(alien)
                score += 1

    # Draw everything
    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, (player_x, player_y, 20, 20))
    for alien in aliens:
        pygame.draw.rect(screen, WHITE, (alien["x"], alien["y"], 20, 20))
    for bullet in bullets:
        pygame.draw.rect(screen, WHITE, (bullet["x"], bullet["y"], 5, 5))

    # Draw the score
    font = pygame.font.Font(None, 36)
    text = font.render("Score: " + str(score), True, WHITE)
    screen.blit(text, (10, 10))

    # Update the display
    pygame.display.flip()
    pygame.time.Clock().tick(FPS)

#iworkfordell

Leave a Comment

WordPress Appliance - Powered by TurnKey Linux