Cricket Match Winner Prediction with Amazon Bedrock's Anthropic Claude 3 Sonnet
Context:
Five Test match, as part of the England tour of India, stands as a testament to the intensity and excitement that cricket brings.
In this blog/notebook I am using AWS Bedrock New announcements Amazon Claude 3.0 Sonnet Model to analyze match statistics image and even predict who will be winner.
Source: https://www.espncricinfo.com/series/england-in-india-2023-24-1389386/india-vs-england-5th-test-1389403/match-statistics
Key Features I noticed during this Exercise:
Image Recognition: The model employs state-of-the-art image recognition algorithms to accurately identify and extract relevant cricket match statistics from image files.
Statistical Analysis: With a deep understanding of cricketing metrics and game dynamics, the model analyzes extracted data to discern patterns, trends, and performance indicators.
Predictive Analytics: By synthesizing extracted statistics and historical trends, the model generates predictive insights to forecast the likely outcome of the cricket match
Data Extraction: The Amazon Claude Sonnet Model ingests image files containing cricket match statistics, leveraging its image recognition capabilities to extract relevant.
Github Link - Notebook
https://github.com/jayyanar/learning-aws-bedrock/blob/main/blog16-Claude-Sonnet/Bedrock_Anthropic_Claude.ipynb
Previous Blogs : https://blog.dataopslabs.com/series/aws-bedrock
Environment Setup
I am using vscode local environment with AWS Credential configured.
Install Latest Python
! python --version
Python 3.11.5
Upgrade pip
! pip install --upgrade pip
Install latest boto3,awscli, boto3-core
! pip install --no-build-isolation --force-reinstall \
"boto3>=1.33.6" \
"awscli>=1.31.6" \
"botocore>=1.33.6"
Load the Library
import json
import os
import sys
import boto3
import botocore
bedrock = boto3.client(service_name="bedrock")
bedrock_runtime = boto3.client(service_name="bedrock-runtime")
Anthropic Claude Model
Anthropic is the AI safety and research company behind Claude - the frontier model used by millions of businesses and consumers for its emphasis on safety and performance.
You can read highlevel about more on Claude 3 Sonnet - https://www.dataopslabs.com/claude-3-sonnet-and-aws-bedrock-integration
Prompt - For Usecase 1 and 2 - Process the Stats in Image
Your task is to create a comprehensive Cricket Match Stats Analysis & Outcome Prediction, Extract stats from uploaded images, including player performances and match conditions. Use predictive modeling to forecast match outcomes based on provide data
Set the Prompt
# Define the prompt text to ask the model about the content of the uploaded image
input_text = "Your task is to create a comprehensive Cricket Match Stats Analysis & Outcome Prediction, Extract stats from uploaded images, including player performances and match conditions. Use predictive modeling to forecast match outcomes based on provide data"
Configure Model Configuration: Specify the model ID, Anthropic version, and maximum tokens for text completion
model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'
max_tokens = 256
Read the Image
from PIL import Image
from IPython.display import display
# Open the image
img = Image.open('stats_images/Star-batting-performance.png')
# Convert the image to RGB mode
img = img.convert('RGB')
# Display the image
display(img)
Parse the Image: Read the image file and encode it into base64 format
import base64
input_image = "stats_images/Star-batting-performance.png"
with open(input_image, "rb") as image_file:
content_image = base64.b64encode(image_file.read()).decode('utf8')
Construct the API request body including the image data and prompt text
# Invoke the Model: Construct the API request body including the image data and prompt text, then send a POST request to the model API
message = {"role": "user",
"content": [
{"type": "image", "source": {"type": "base64",
"media_type": "image/png", "data": content_image}},
{"type": "text", "text": input_text}
]}
messages = [message]
Create a Function to Invoke
def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
"""
Invokes a model with a multimodal prompt.
Args:
bedrock_runtime: The Amazon Bedrock boto3 client.
model_id (str): The model ID to use.
messages (JSON) : The messages to send to the model.
max_tokens (int) : The maximum number of tokens to generate.
Returns:
None.
"""
body = json.dumps(
{
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": max_tokens,
"messages": messages
}
)
response = bedrock_runtime.invoke_model(
body=body, modelId=model_id)
response_body = json.loads(response.get('body').read())
results = response_body.get("content")[0].get("text")
return results
Invoke the Function
response_claude_sonnet = run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens)
print(json.dumps(response_claude_sonnet, indent=4))
Text completion: The image shows detailed batting stats for two cricket players - Shubman Gill and RG Sharma - in what appears to be an innings from a cricket match. The stats are presented in circular charts that break down the runs scored through various shot types and scoring regions.\n\nFor Shubman Gill, the key stats shown are:\n- Total runs scored: 110 (off 150 balls)\n- Fours hit: 12\n- Sixes hit: 5\n- His most productive shot was the cut shot, scoring 23 runs including 4 fours
Usecase2 - Process Both Bowling and Batting Stats
from PIL import Image
from IPython.display import display
# Open the image
img = Image.open('stats_images/Star-Batting-Bowling-Performance.png')
# Convert the image to RGB mode
img = img.convert('RGB')
# Display the image
display(img)
Parse the Image
import base64
input_image2 = "stats_images/Star-Batting-Bowling-Performance.png"
with open(input_image2, "rb") as image_file:
content_image = base64.b64encode(image_file.read()).decode('utf8')
Construct the API request body including the image data and prompt text
# Invoke the Model: Construct the API request body including the image data and prompt text, then send a POST request to the model API
message = {"role": "user",
"content": [
{"type": "image", "source": {"type": "base64",
"media_type": "image/png", "data": content_image}},
{"type": "text", "text": input_text}
]}
messages = [message]
Create a Function to Invoke
def run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens):
"""
Invokes a model with a multimodal prompt.
Args:
bedrock_runtime: The Amazon Bedrock boto3 client.
model_id (str): The model ID to use.
messages (JSON) : The messages to send to the model.
max_tokens (int) : The maximum number of tokens to generate.
Returns:
None.
"""
body = json.dumps(
{
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 256,
"messages": messages
}
)
response = bedrock_runtime.invoke_model(
body=body, modelId=model_id)
response_body = json.loads(response.get('body').read())
results = response_body.get("content")[0].get("text")
return results
Invoke the Function
response_claude_sonnet_2 = run_multi_modal_prompt(bedrock_runtime, model_id, messages, max_tokens)
print(json.dumps(response_claude_sonnet_2, indent=4))
Text completion: Based on the image, cricket match statistics can be analyzed to provide insights as follows:
Batting Analysis:
Shubman Gill (IND) scored 110 runs off 150 deliveries, including 12 fours and 5 sixes. His most productive shot was the cut shot (23 runs, 4 fours).
RG Sharma (IND) scored 103 runs off 162 deliveries, with 13 fours and 3 sixes. His most productive shot was the flick shot (22 runs, 3 fours).
Bowling Analysis:
Shoaib Bashir (ENG) bowled 5 overs, conceding 46 runs while taking 5 wickets at an economy rate of 3.74. His lengths were mostly good length and full length deliveries.
Kuldeep Yadav (IND) bowled 1 over, conceding 15 runs while taking 1 wicket at an economy rate of 4.8. His lengths were mostly short of good length and short length deliveries.
Usecase 3 - Predict Series Outcome based on Stats
Prompt - For Usecase 3
Your task is to create a comprehensive Cricket Match Stats Analysis and Outcome Prediction based on the image provided, The match is between India vs England 5 Match Test series. Provide player name, country with statistics. Use predictive modeling to forecast match outcomes based on date from only uploaded image. Need the prediction of Match with team or country name on who will win the match based on the data. Give me the output in bullet points. I am giving some stats for this series
Set the Prompt
match_result_analysis = "Your task is to create a comprehensive Cricket Match Stats Analysis and Outcome Prediction based on the image provided, The match is between India vs England 5 Match Test series. Provide player name, country with statistics. Use predictive modeling to forecast match outcomes based on date from only uploaded image. Need the prediction of Match with team or country name on who will win the match based on the data. Give me the output in bullet points. I am giving some stats for this series"
Configure the Model
model_id = 'anthropic.claude-3-sonnet-20240229-v1:0'
max_tokens = 512
Process the Image
from PIL import Image
from IPython.display import display
# Open the image
img = Image.open('stats_images/PerformanceSeries.png')
# Convert the image to RGB mode
img = img.convert('RGB')
# Display the image
display(img)
Parse the Image: Read the image file and encode it into base64 format
import base64
input_image3 = "stats_images/PerformanceSeries.png"
with open(input_image3, "rb") as image_file:
content_image = base64.b64encode(image_file.read()).decode('utf8')
Construct the API request body including the image data and prompt text
message = {"role": "user",
"content": [
{"type": "image", "source": {"type": "base64",
"media_type": "image/png", "data": content_image}},
{"type": "text", "text": match_result_analysis}
]}
messages = [message]
Create a Function to Invoke
def run_multi_modal_prompt_analysis(bedrock_runtime, model_id, messages, max_tokens):
"""
Invokes a model with a multimodal prompt.
Args:
bedrock_runtime: The Amazon Bedrock boto3 client.
model_id (str): The model ID to use.
messages (JSON) : The messages to send to the model.
max_tokens (int) : The maximum number of tokens to generate.
Returns:
None.
"""
body = json.dumps(
{
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": max_tokens,
"messages": messages
}
)
response = bedrock_runtime.invoke_model(
body=body, modelId=model_id)
response_body = json.loads(response.get('body').read())
results = response_body.get("content")[0].get("text")
return results
Invoke the Function
response_claude_sonnet = run_multi_modal_prompt_analysis(bedrock_runtime, model_id, messages, max_tokens)
print(json.dumps(response_claude_sonnet, indent=4))
Text completion in Responsible Manner: Based on the provided stats for the India vs England 5-match Test series, here is a comprehensive analysis and match outcome prediction:
• Top run-scorers: - Yashasvi Jaiswal (IND) - 712 runs at 89.00 average - Shubman Gill (IND) - 452 runs at 56.50 average - Zak Crawley (ENG) - 407 runs at 40.70 average
• Top wicket-takers: - Ravichandran Ashwin (IND) - 26 wickets at 24.80 average - Tom Hartley (ENG) - 22 wickets at 36.13 average - Jasprit Bumrah (IND) - 19 wickets at 16.89 average
• Best batting strike rates: - Mark Wood (ENG) - 90.56 - Ben Duckett (ENG) - 85.75 - Jonny Bairstow (ENG) - 82.06
• Best bowling economy rates: - Jack Leach (ENG) - 2.67 - James Anderson (ENG) - 3.05 - Jasprit Bumrah (IND) - 3.09
• India has the top run-scorer in Jaiswal and one of the best bowlers in Bumrah • England has a solid batting lineup with good strike rates • Both teams have quality bowlers, but India's attack looks slightly better
• Based on the provided stats and predictive modeling, India appears to have a slight edge over England in this 5-match Test series
• However, Test cricket is known for its unpredictability, and the actual outcome may vary depending on various factors beyond these stats
Keep Learning - Keep Sharing ........
Subscribe to my newsletter
Read articles from DataOps Labs directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
DataOps Labs
DataOps Labs
I'm Ayyanar Jeyakrishnan ; aka AJ. With over 18 years in IT, I'm a passionate Multi-Cloud Architect specialising in crafting scalable and efficient cloud solutions. I've successfully designed and implemented multi-cloud architectures for diverse organisations, harnessing AWS, Azure, and GCP. My track record includes delivering Machine Learning and Data Platform projects with a focus on high availability, security, and scalability. I'm a proponent of DevOps and MLOps methodologies, accelerating development and deployment. I actively engage with the tech community, sharing knowledge in sessions, conferences, and mentoring programs. Constantly learning and pursuing certifications, I provide cutting-edge solutions to drive success in the evolving cloud and AI/ML landscape.