In this video, we'll explore the fascinating
world of handwriting analysis using Claude 3.5 Sonnet's advanced vision capabilities.
We'll build a powerful web application with Streamlit that lets you upload images of
handwriting and receive insightful personality interpretations. We'll walk through the entire
process, from setting up the API to designing an intuitive user interface. By the end of
this video, you'll have the knowledge to create your own handwriting analysis tool.
We'll also discuss whether the claims that Sonnet outperforms GPT-4 in quality, speed, and
cost are accurate and put them head-to-head. Why is everyone talking about 3.5 Sonnet? You've
probably noticed the buzz around Claude 3.5 Sonnet lately, and for good reason. This latest
iteration from Anthropic represents a significant leap forward in AI capabilities. Claude Sonnet is
the most advanced vision model available on the market. Its improvements shine in tasks requiring
visual reasoning, such as interpreting charts and graphs. How does it compare to GPT-4? Let's start
the comparison by looking at the speed of both models side by side. We asked each model to write
a 500-word blog article. As you can see, even though both models start almost simultaneously,
Claude seems to be much faster. Claude Sonnet's speed shines in real-world use, often outpacing
GPT-4 in text generation and data analysis. Sonnet generally scores higher than GPT-4 in most
categories for graduate-level reasoning. Claude 3.5 Sonnet comes out on top, showing stronger
performance in undergraduate knowledge. Both models are neck and neck, performing exceptionally
well in zero-shot tests. When it comes to coding, Claude has a slight edge over GPT-4, but both
are quite impressive. In multilingual math, Claude slightly outperforms GPT-4; however,
GPT-4 takes the lead in math problem-solving. For reasoning over text, Claude scores
a bit higher than GPT-4. In summary, Claude 3.5 Sonnet generally has a slight
edge, but GPT-4 remains very competitive. What does Claude 3.5 cost? Claude
Sonnet is available for free on Claude, but if you're a Claude Pro or Team Plan
subscriber, you get it with much higher rate limits. You can also access it via the
Anthropic API, where it costs $3 per million input tokens and $15 per million output tokens,
with a 200k token context window. For comparison, GPT-4 costs $15 per million output tokens
but has a smaller 128k token context window. How user-friendly is the API? The Anthropic
API is very beginner-friendly thanks to its comprehensive documentation and its similarity to
the OpenAI API. This means that if you're already familiar with the OpenAI API, you'll find the
transition to using the Anthropic API seamless. What are artifacts? Claude has introduced
a cool new feature called artifacts. When you ask Claude to generate content like code
snippets, text documents, or website designs, these artifacts appear in a dedicated window
alongside your conversation. This creates a dynamic workspace where you can see, edit, and
build upon Claude's creations in real-time, seamlessly integrating AI-generated content
into your projects. This marks a significant evolution from Claude being just a conversational
AI to becoming a collaborative work environment. But enough theory, let's dive into our project
where we use this great new model and its API to build a handwriting analyzer with a fully
functional web interface. If you haven't worked with Python and Visual Studio Code yet, please
check out the introduction video series on AI for Devs.com, where we explain in detail for absolute
beginners how to work with Python and APIs. As always, we start by creating a virtual
environment and activating it to keep dependencies cleanly separated from each other.
Then we install Anthropic with pip. We create a new file named app.py using the touch
command. Here we first import Anthropic, then create a client that we use to
interact with the Anthropic API. For this, we call client.messages.create and pass the model
as a parameter, in this case, Claude 3.5, and set the maximum number of tokens to 1,000. We can
define the messages we want to send to the model, starting with the user role. Here we want to send
a simple text message with the content "Hi." The result of this call should be printed to the
console, for which we use the print command. Now we can already test the script by opening the
terminal. To authenticate with the Claude API, we need to set the Anthropic API key. For this, we
go to the Anthropic website and create an API key in the internal area in the API key section. We
simply click on "Create key," give the key a name, copy it, and then paste it into the terminal.
Now we can start the script with python app.py. Unfortunately, we get an error message because
I misspelled the model name. We quickly fix this by removing the dashes. Let's try it again, and it
looks great! We get a message back from the model. Next, we want to actually analyze an image, so we
first take this nice picture here of an ant and convert it to base64. This line of code fetches
an image from a given URL using the httpx library, encodes the image content to base64, and then
decodes it into a utf-8 string. This string can then be sent to the Anthropic API for further
processing or analysis. We specify the type as image and pass the following parameters: type as
base64, media type as image/jpeg, and data as the base64 encoded string. Now we change the prompt to
describe the image and can already run the script. We see that the image is correctly analyzed, and
it's impressive how detailed the description is. However, our actual goal is not to analyze animal
pictures but to analyze handwriting. For this, we first change the user prompt and specify
exactly what to look for, such as pressure, size, spacing, length, etc. As an example, we want
to use the handwriting of Matisse and specify the image above. Now we try it out. Wonderful! We
get a detailed analysis indicating that it's a cursive style and suggesting that this might
point to an outgoing personality. Furthermore, it possibly indicates an artistic flair, which
is not wrong for Matisse. As a second example, we want to examine the handwriting of Stephen Hawking
as a contrast. Here we get the analysis that it is a more deliberate writer style. It is very neat
and precise, suggesting an attention to detail. Now we want to build a user interface. For
this, we first install Streamlit. We import Streamlit and start with a simple title.
We can easily use the title function of Streamlit. We give it the title "Handwriting
Analyzer" and to make it a bit more personal, we use a nice emoji. This single line
is enough to start the interface with streamlit run app.py, and we see
we have a webpage with a title. Next, we want to add the upload feature. Here we
use a file uploader, and when a file is selected, we want to display the uploaded image directly
on the webpage. For this, we need to import Image from the PIL library. We have a button that
starts the analysis and calls our logic as a method. Therefore, we need to wrap our logic into
a method called analyze_handwriting, to which we pass an image. We create the new function with
the keyword def and name it analyze_handwriting. We then indent all the logic with the call to
Claude so that it's only executed when we run the method. We create an in-memory byte buffer
using BytesIO. We import BytesIO from io and save the image into this buffer in PNG format. Then the
buffer's content is encoded into a base64 string, which is ready to be used or sent to another
service. In summary, we convert the given uploaded image into a base64 string. We need to adapt
the return result of the method to return the analysis. For this, we access message.content,
the first element of which is the text. Now we have everything we need, and we can go
back to the page and try out our analysis. Let's upload a handwritten note from Freud. The
image is displayed correctly, and we start the analysis and see the detailed analysis
of the image. Wonderful! Everything worked, and this could be turned into a service where
people might be willing to pay a few euros.