DEV Community

Cover image for How to Use AI/ML Models for Your Projects
CodeLink
CodeLink

Posted on

How to Use AI/ML Models for Your Projects

As developers, we are constantly navigating the digital landscape’s rapid pace of change. Recently, AI has become a driving force in technology and development. It is no longer a buzzword but is making waves across diverse industries, from healthcare to entertainment, education to logistics.

Being part of this transformative journey is necessary to stay competitive and innovative. And it’s not just developers keen to get their hands on AI; brands are swiftly integrating AI into their products to enhance user experience and functionality.

For those of us in IT, we find ourselves at the forefront with early access to the newest technological advances. This is about setting the pace, pushing boundaries, and unlocking new potential. Early adoption and a willingness to explore can give us a competitive edge.

In this blog, we’re unraveling the complexities and laying out practical ways to integrate AI/ML models into your projects. Join us as we explore the transformative world of AI together.

What if You Don’t Have a Background in AI?

AI can seem difficult, especially if you're just starting out and don't have a background in it. But don’t let that stop you—AI is more accessible than ever, even for those still learning the basics.

AI Tools for Your Work: Nowadays, many AI-powered tools are designed to help developers streamline their work. For example, Copilot can suggest code snippets, CodeGPT provides coding assistance, and ChatGPT engages users in human-like conversations.

Develop Apps Using Available AI Models: If you're trying to get started with AI, some platforms let you integrate AI models directly into your applications. A deep understanding of how these models work internally is not needed. This allows developers to leverage AI's benefits without delving into the complex details.

Diving into AI might feel overwhelming at first. But with these tools and platforms, you’re well-equipped to take the first step, explore the vast landscape of AI, and bring a new level of innovation to your development projects.

How to Use AI in Your Projects?

Use API as a Service

So, you’re ready to infuse AI into your projects? A practical and convenient way to do this is using AI models through API services. Numerous platforms offer AI models as APIs, which seamlessly integrate with your backend services to give your applications a boost of intelligence and capabilities.

OpenAI (https://platform.openai.com/docs/models): Now a well-known company since launching ChatGPT, OpenAI stands out with its offering of the GPT-4 model. This model allows developers to integrate advanced natural language processing (NLP) features into their applications. Imagine building a chatbot that understands and responds with human-like accuracy or creating a tool that generates creative writing pieces; with OpenAI, these are realities.

Google Cloud Platform (https://cloud.google.com/vertex-ai): Conversely, Google Cloud Platform (GCP) provides a comprehensive suite of AI and machine learning services, including APIs for vision, language, conversation, and structured data analysis. Whether you're analyzing images, interpreting human speech, or diving deep into data patterns, GCP has something for you.

Azure (https://learn.microsoft.com/en-us/azure/machine-learning): Not to be left behind, Azure offers Cognitive Services encompassing APIs for vision, speech, language, decision-making, and web search services. Envision an app that not only sees and hears but understands and thinks—Azure makes this possible.

AWS: also enters the machine learning race with SageMaker, which offers similar functionality to Azure ML and GCP Vertex AI.

Ready to get started? Here’s a sneak peek with a code example on integrating OpenAI with FastAPI

from typing import Callable

from fastapi import Depends, FastAPI
from langchain import ConversationChain
from langchain.chat_models import ChatOpenAI
from pydantic import BaseModel

from app.langchain_stream.responses import LLMChainStreamingResponse

app = FastAPI(title="ConversationChainDemo")

class QueryRequest(BaseModel):
    query: str

def conversation_chain_dependency() -> Callable[[], ConversationChain]:
    @lru_cache(maxsize=1)
    def dependency() -> ConversationChain:
        return ConversationChain(
            llm=ChatOpenAI(
                temperature=0,
                streaming=True,
            ),
            verbose=True,
        )

    return dependency

conversation_chain = conversation_chain_dependency()

# Call openAI API with langchain
@app.post("/chat")
async def chat(
    request: QueryRequest,
    chain: ConversationChain = Depends(conversation_chain),
) -> LLMChainStreamingResponse:
    return LLMChainStreamingResponse.from_chain(
        chain, request.query, media_type="text/event-stream"
    )
Enter fullscreen mode Exit fullscreen mode

Bring Open-Source Models to Your Own Platform

Transitioning from APIs, consider hosting AI models on your own servers. It’s a bold move, yet it brings unparalleled control and customization.

HuggingFace (https://huggingface.co/models): Enter the world of HuggingFace, a huge repository of pre-trained models ready for deployment. For example, integrating the BERT model for sentiment analysis transforms your server into a powerful emotion-sensing engine. BERT is one of the many models available on HuggingFace that you can leverage for your applications.

OpenCV: A library of programming functions, OpenCV focuses on real-time computer vision. It can handle tasks like object identification, image recognition, and complex activities like facial recognition. For example, if you're creating an image tagging service, you can use OpenCV to identify objects in an image and tag them appropriately.

Open Source Models: The internet has countless open-source AI models for various tasks. Most of the relevant models can be found in Paper With Code site (​​https://paperswithcode.com/). Find the one that fits your requirements, tweak it, and deploy.

This example code demonstrates the ability to summarize content in Vietnamese using OCR models from HuggingFace.

from fastapi import Depends, FastAPI
from fastapi import FastAPI, UploadFile, File
from transformers import TrOCRProcessor, VisionEncoderDecoderModel, AutoTokenizer, AutoModelForSeq2SeqLM
from PIL import Image
import io

app = FastAPI(title="AIDemo")

# OCR convert image to text
@app.post("/ocr")
async def ocr(file: UploadFile = File(...)):
    # Initialize the TrOCR model and processor once, when the app starts
    processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
    model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")

    image = Image.open(io.BytesIO(await file.read())).convert("RGB")
    pixel_values = processor(image, return_tensors="pt").pixel_values
    generated_ids = model.generate(pixel_values)
    generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]

    return {"filename": file.filename, "text": generated_text}

@app.post("/summarize")
async def create_summary(request: QueryRequest):
    tokenizer = AutoTokenizer.from_pretrained("VietAI/vit5-base-vietnews-summarization")
    model = AutoModelForSeq2SeqLM.from_pretrained("VietAI/vit5-base-vietnews-summarization")

    sentence = request.query
    text = "vietnews: " + sentence + " </s>"
    encoding = tokenizer(text, return_tensors="pt")
    input_ids, attention_masks = encoding["input_ids"], encoding["attention_mask"]
    outputs = model.generate(
        input_ids=input_ids, attention_mask=attention_masks,
        max_length=256,
        early_stopping=True
    )
    texts = [tokenizer.decode(output, skip_special_tokens=True, clean_up_tokenization_spaces=True) for output in outputs]

    return {"original": sentence, "summary": texts}
Enter fullscreen mode Exit fullscreen mode

Apply AI on Frontend

Venturing into the world of frontend development, integrating AI models directly into the user interface is incredibly beneficial. This strategy not only paves the way for cost-efficient server operations but also ensures swift response times, greatly enhancing user experience.

Transformers.js: A groundbreaking library, Transformers.js brings transformer models like GPT-3, BERT, and Whisper straight to your browser. With the introduction of technologies like webGPU and LLM, Transformers.js has garnered significant attention. If you’d like to learn how to integrate a small model in the UI, check out their code and examples here.

TensorFlow.js: Then there’s TensorFlow.js, a versatile library that brings machine learning capabilities into the JavaScript environment. Whether running pre-trained models, tweaking them for better performance, or building your own from scratch, TensorFlow.js is even available on mobile with React Native.

Optimize the Model Size and Latency

Managing model size and latency is pivotal, especially when dealing with AI applications. Let’s unravel the ways to optimize these aspects seamlessly.

Quantization: Kickstart your journey by embracing quantization. Swap the typical 32-bit floating-point numbers for more compact alternatives such as 16-bit floats or integers. This strategy reduces the memory footprint and speeds up the model execution, especially on hardware exploiting these smaller datatypes.

Choose a Small-Size Model or Distillation: If you’re searching for speed, small-size models or model distillation could be your answer. Here, you train a nimble ‘student’ model to imitate a larger, more precise ‘teacher’ model. The outcome is a lightweight model capable of swift inference, ready to perform in time-sensitive scenarios.

Optimized Libraries and Hardware Acceleration: Don’t miss out on the speed benefits of optimized deep learning libraries and hardware acceleration. Leveraging tools like TensorFlow Lite, ONNX Runtime, or NVIDIA TensorRT can propel your model’s performance, ensuring a smooth and fast AI experience.

For those interested in learning more, check out this guide on model optimization: Hugging Face Optimum. This resource is full of information and practical advice, setting you up for success in your optimization efforts.

Conclusion

As developers in this fast-paced digital era, harnessing the power of AI is essential. This blog has explored the many ways to seamlessly weave AI and ML models into your development projects, even without a background in AI.

From the AI-powered tools readily available to the comprehensive platforms offering API services, we’ve covered substantial ground. OpenAI, Google Cloud Platform, Azure, HuggingFace, and TensorFlow.js are just a few resources bringing AI’s transformative potential into your work.

Here at CodeLink, we strongly believe in the power of innovation and the incredible potential of AI. We understand the challenges and the excitement that comes with discovering new technologies, and we’re committed to providing resources, guidance, and inspiration.

As you step forward, ready to integrate AI into your projects and push the boundaries of what’s possible, remember the world of AI is ever-evolving. Dive deeper, experiment boldly, and continue to explore the vast landscape of AI with the additional resources CodeLink has to offer.

Top comments (0)