blog.pierrehenry.be

How to Build Smarter AI Predictions Without Complex Code

Photo by Bernd 📷 Dittrich How to Build Smarter AI Predictions Without Complex Code - Photo by Bernd 📷 Dittrich on Unsplash

Alright, let’s get right into it. Today, I’m going to walk you through a project I’ve been working on that uses generalized linear models—specifically, logistic regression. Most of my experiments here are in Python, because, let’s be honest, Python just makes this stuff way easier.

The main goal with these projects is to train a model that can estimate outcomes based on the data we have. Basically, we want to make predictions. That’s the heart of it. If you’re building anything with AI, you know that prediction is the name of the game.

The Project: Logistic Regression with FastAPI

This is a small project, but it packs a punch. What it does is run a logistic regression, and then exposes the prediction functionality through a FastAPI endpoint. Why FastAPI? Because it’s fast (duh), and it lets you call your model in real time. You just hit the API with your data, and boom—you get a prediction back instantly.

Here’s a quick look at how I set it up:

 1from fastapi import FastAPI
 2from pydantic import BaseModel
 3import joblib
 4
 5app = FastAPI()
 6model = joblib.load("logistic_regression_model.pkl")
 7
 8class InputData(BaseModel):
 9    feature1: float
10    feature2: float
11    # add more features as needed
12
13@app.post("/predict")
14def predict(data: InputData):
15    features = [[data.feature1, data.feature2]]
16    prediction = model.predict(features)
17    return {"prediction": int(prediction[0])}

With this setup, you can call the /predict endpoint with your data, and it’ll return the model’s prediction. Super handy for integrating into any pipeline.

Real-Time Updates and Integration

One of the best things about this approach is that any new changes in your data can be immediately reflected. You just update your model, and your API is ready to serve the latest predictions. This is crucial if you’re working in environments where things change fast and you need your AI to keep up.

Adding Summarization with OpenAI

Now, here’s where it gets even more interesting. I’m also using OpenAI for summarization. Why? Because sometimes you don’t just want a raw prediction—you want a summary of what’s going on. Summarization can be super useful for giving quick overviews of your logistic regression results, especially if you’re sharing them with people who aren’t deep into the technical details.

Here’s a quick example of how you might hook that up:

 1import openai
 2
 3def summarize_results(results):
 4    prompt = f"Summarize these logistic regression results: {results}"
 5    response = openai.Completion.create(
 6        engine="text-davinci-003",
 7        prompt=prompt,
 8        max_tokens=50
 9    )
10    return response.choices[0].text.strip()

So, after you get your prediction, you can pass the results to OpenAI and get a nice, readable summary. It’s a small touch, but it makes your project way more user-friendly.

Why This Matters

At the end of the day, what we want is to make our AI projects more accessible and more useful. By combining logistic regression, FastAPI, and OpenAI, you can build something that not only predicts but also explains. That’s a big deal.

As I always say: “A model that can’t communicate its results is only half as useful.”


Key Takeaways


🤔 Learn more about me on Dev.to


Pierre-Henry Soria

GitHub · PierreHenry.Dev · YouTube

<< Previous Post

|

Next Post >>

#Fastapi #Glm #Logistic Regression #Machine Learning #Openai #Tech