the-geeky-codes-high-resolution-logo-color-on-transparent-background geeky code red logo
  • Home
  • AI
    AIShow More
    generate vector icons
    Generate Vector Icons with ChatGPT DALLE 3: A Comprehensive Guide
    14 Min Read
    Dalle 3
    Dalle 3: A Step-by-Step Guide to Mastering AI Art Generation
    4 Min Read
    5 Best AI Tools to Convert Images to Video Animations
    5 Best AI Tools to Convert Images to Video Animations
    8 Min Read
    Exploring the Impressive Mistral 7B Model
    Exploring the Impressive Mistral 7B Model for Text Summarization and Coding
    6 Min Read
    The AI Revolution this week
    Must Read – The AI Revolution this week 30 Sep 2023: Integrating AI Tools into Everyday Life
    6 Min Read
  • Tutorial
    • React js
    • Python
    • Javascript
  • AI Tools
Reading: Building an AI-Powered Image-to-Text Converter with Claude, Next.js 15, and Vercel AI SDK
Share
the geeky codesthe geeky codes
Aa
  • AI
  • AI Tools
  • Javascript
  • Python
  • React js
  • Advertise
Search
  • Categories
    • AI
    • AI Tools
    • Javascript
    • Python
    • React js
  • More
    • Advertise
Follow US
Copyright ©2023 The Geeky codes. All Rights Reserved.
the geeky codes > Blog > Tutorial > Nextjs > Building an AI-Powered Image-to-Text Converter with Claude, Next.js 15, and Vercel AI SDK
TutorialNextjs

Building an AI-Powered Image-to-Text Converter with Claude, Next.js 15, and Vercel AI SDK

thegeekycodes By thegeekycodes 20 November 2024 4 Min Read
Attachment Details Image-to-Text-Converter-with-Claude-Nextjs-15
SHARE

In this tutorial, we’ll build a modern web application that converts images to detailed text descriptions using Claude’s vision capabilities. We’ll leverage the power of Next.js 15’s App Router and the Vercel AI SDK to create a responsive, real-time streaming application.

Contents
PrerequisitesProject SetupBuilding the API EndpointCreating the Frontend InterfaceDeploymentConclusionResources

Prerequisites

  • Basic knowledge of React and TypeScript
  • Node.js installed on your machine
  • An Anthropic API key
  • Familiarity with Next.js (basic)

Project Setup

First, let’s create a new Next.js project with TypeScript and Tailwind CSS support:

npx create-next-app@latest image-to-text --typescript --tailwind --app
cd image-to-text

Install the required dependencies:

npm install ai @anthropic-ai/sdk

Building the API Endpoint

Create a new API route at app/api/chat/route.ts. This endpoint will handle image uploads and communicate with Claude:

import { AnthropicStream, StreamingTextResponse } from 'ai';
import { Anthropic } from '@anthropic-ai/sdk';

const anthropic = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

export const runtime = 'edge';

export async function POST(req: Request) {
  const { image } = await req.json();

  const response = await anthropic.messages.create({
    model: 'claude-3-haiku-20240307',
    max_tokens: 1024,
    messages: [
      {
        role: 'user',
        content: [
          {
            type: 'image',
            source: {
              type: 'base64',
              media_type: 'image/jpeg',
              data: image.split(',')[1]
            }
          },
          {
            type: 'text',
            text: 'Describe this image in detail.'
          }
        ]
      }
    ]
  });

  const stream = AnthropicStream(response);
  return new StreamingTextResponse(stream);
}

Creating the Frontend Interface

Replace the contents of app/page.tsx with a responsive UI that handles image uploads and displays Claude’s responses:

'use client';

import { useState } from 'react';
import { useChat } from 'ai/react';

export default function ImageToText() {
  const [image, setImage] = useState<string | null>(null);
  const { messages, setMessages, isLoading, append } = useChat();

  const handleImageUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
    const file = e.target.files?.[0];
    if (file) {
      const reader = new FileReader();
      reader.onload = (e) => {
        const base64 = e.target?.result as string;
        setImage(base64);
      };
      reader.readAsDataURL(file);
    }
  };

  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!image) return;

    await append({
      role: 'user',
      content: 'Analyze this image',
      id: Date.now().toString(),
    }, {
      data: { image }
    });
  };

  return (
    <div className="max-w-2xl mx-auto p-4">
      <h1 className="text-2xl font-bold mb-4">Image to Text Converter</h1>
      
      <form onSubmit={handleSubmit} className="space-y-4">
        <div className="space-y-2">
          <label className="block">
            <span className="text-gray-700">Upload Image</span>
            <input
              type="file"
              accept="image/*"
              onChange={handleImageUpload}
              className="mt-1 block w-full"
            />
          </label>
        </div>

        {image && (
          <div className="mt-4">
            <img
              src={image}
              alt="Uploaded preview"
              className="max-w-md mx-auto rounded"
            />
          </div>
        )}

        <button
          type="submit"
          disabled={!image || isLoading}
          className="px-4 py-2 bg-blue-500 text-white rounded disabled:opacity-50"
        >
          {isLoading ? 'Analyzing...' : 'Analyze Image'}
        </button>
      </form>

      <div className="mt-8 space-y-4">
        {messages.map((message) => (
          <div
            key={message.id}
            className={`p-4 rounded ${
              message.role === 'assistant'
                ? 'bg-gray-100'
                : 'bg-blue-100'
            }`}
          >
            {message.content}
          </div>
        ))}
      </div>
    </div>
  );
}

Deployment

  1. Create a .env.local file:
ANTHROPIC_API_KEY=your_api_key_here
  1. Deploy to Vercel:
vercel deploy

Conclusion

We’ve built a modern, responsive image-to-text converter using cutting-edge technologies. The application demonstrates the power of combining Claude’s vision capabilities with Next.js and the Vercel AI SDK. This foundation can be extended to build more complex AI-powered image analysis tools.

Resources

  • Next.js Documentation
  • Vercel AI SDK
  • Claude API Documentation
  • Tailwind CSS

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Copy Link Print
Previous Article Generate-Dynamic-OpenGraph-Images-in-Nextjs15 How to Generate Dynamic OpenGraph Images in Next.js App Router 15 with TypeScript
Next Article Advanced Routing Techniques in Nextjs 15 Advanced Routing Techniques in Next js 15
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Twitter Follow
Telegram Follow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!

Most Popular
Advanced Routing Techniques in Nextjs 15
Advanced Routing Techniques in Next js 15
20 November 2024
Generate-Dynamic-OpenGraph-Images-in-Nextjs15
How to Generate Dynamic OpenGraph Images in Next.js App Router 15 with TypeScript
20 November 2024
Google Analytics 4 in Nextjs 14
How to Install Google Analytics 4 in Next.js 15 (App Router) with TypeScript [2024]
20 November 2024
docker compose
Getting Started with Docker Compose
20 November 2024
Image Processing with OpenCV in Python
Image Processing with OpenCV in Python
19 July 2024

You Might Also Like

Advanced Routing Techniques in Nextjs 15
TutorialNextjs

Advanced Routing Techniques in Next js 15

7 Min Read
Generate-Dynamic-OpenGraph-Images-in-Nextjs15
TutorialNextjs

How to Generate Dynamic OpenGraph Images in Next.js App Router 15 with TypeScript

9 Min Read
Google Analytics 4 in Nextjs 14
TutorialNextjs

How to Install Google Analytics 4 in Next.js 15 (App Router) with TypeScript [2024]

6 Min Read
docker compose
TutorialDocker

Getting Started with Docker Compose

6 Min Read

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!

the geeky codes geeky code red logo

Providing valuable resources for developers in the form of code snippets, software tutorials, and AI related content.

About

  • About Us
  • Contact
  • Terms and Conditions
  • Privacy Policy
  • Disclaimer
  • Affiliate Disclosure

Resource

  • The Art of AI Prompt Engineering: Crafting Effective Inputs for AI Models

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
© 2023 The Geeky Codes. All Rights Reserved
We are happy to see you join Us!

🔥📢Subscribe to our newsletter and never miss our latest code snippets, tutorials and AI updates

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?