AI powered age transformation
Tags: ai, github
Published: 2023-03-08 07:10:34

The AI-powered photo-aging project's premise is either fun or highly morbid. Developer, @steventey, put together Extrapolate, a project that lets you see how well you will age using AI.

Perhaps you are not on TikTok, but there is a TikTok filter that makes GenX users cry. _You can see it [here on YouTube](https://youtu.be/2kDVlH3kSYs?t=30)_. Extrapolate is the reverse of letting GenZ reduce FOMO and see what they look like old (joking).

_header image was generated using [midjourney](https://www.midjourney.com/app/jobs/67f18fbc-8ddb-41d3-a880-65d109e765b3/)_

{% github steven-tey/extrapolate noreadme %}

## What is steven-tey/extrapolate?

How will you look in 10 years? 20 years? When you're 90? Upload a photo to [extrapolate.app](https://extrapolate.app/) to find out. It is free and privacy-friendly because all images are deleted after 24 hours.

![extrapolate example gif](https://images.extrapolate.workers.dev/output.gif)

## How does it work?
As I have done previously in the [9 days of OpenAI series](https://dev.to/bdougieyo/30-days-of-openai-1ek6), I walk through the AI code and talk through how it works.

Similar to how the [AI generated room designs](https://dev.to/bdougieyo/ai-generated-room-designs-3dpm) was built, Extrapolate leverages Replicate. 

The model number is referenced in the code is "9222a21c181b707209ef12b5e0d7e94c994b58f01c7b2fec075d2e892362f13c", but there it does seem to be private or not searchable. _If someone more familiar with Replicate wants to tell if I am wrong on this, please comment._

Hosted models' privacy is an exciting feature for Replicate, considering not all models need to be discoverable. If I choose to build my own, I am even more intrigued about using Replicate to store my future models. 

I searched for other models that age photos and found the model named Sam to be pretty close. 

![search on replicate for aging](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yoj39fuv0c1dneqpgs3z.png)

But looking at the code, you can see the fetch call to the replicate predictions endpoint from the [pages/api/upload.ts](https://github.com/steven-tey/extrapolate/blob/0991080cc977cc7bc71aa8c72a6a9ea63554d953/pages/api/upload.ts#L36-L51).

```js
// pages/api/upload.ts

fetch("https://api.replicate.com/v1/predictions", {
  method: "POST",
  headers: {
    "Content-Type": "application/JSON",
    Authorization: "Token " + process.env.REPLICATE_API_KEY,
  },
  body: JSON.stringify({
    version: "9222a21c181b707209ef12b5e0d7e94c994b58f01c7b2fec075d2e892362f13c", // model version
    input: {
      image,
      target_age: "default",
    },
    webhook_completed: `${domain}/api/images/${key}/webhook?token=${process.env.REPLICATE_WEBHOOK_TOKEN}`,
  }),
}).then((res) => res.json()),

...
```
I was also curious about the 24hr photo deletion and which is referenced in the README. It appears to be handled by a service provided by [Upstash](https://upstash.com/) (Serverless Data for Redis), and runs on a schedule using a tool called [Qstash](https://upstash.com/blog/qstash-announcement). Qstash is new to me, but it's a message queue and task scheduler designed for serverless runtimes (_In this example, Cloudflare workers_). 

I had not seen the Cloudflare R2 product in the wild prior. It provides similar unstructured bucket storage as S3 and is used for quick uploads. This work happens in the [pages/api/images/[id]/delete.ts](https://github.com/steven-tey/extrapolate/blob/0991080cc977cc7bc71aa8c72a6a9ea63554d953/pages/api/images/%5Bid%5D/delete.ts#L9-L20) file.   

Below is the code that manages the deletion, which is called on schedule. 

```js
// pages/API/images/[id]/delete.ts
...
await Promise.allSettled([
  fetch(`https://images.extrapolate.workers.dev/${id}`, {
    method: "DELETE",
    headers: {
      "X-CF-Secret": process.env.CLOUDFLARE_WORKER_SECRET as string,
    },
  }),
  redis.set(id, {
    expired: true,
  }),
]);
```
My takeaway from this implementation is that the AI interaction is abstracted away by Replicate. If you are a developer looking to ship a project leveraging AI quickly, this is an excellent project to dig deeper into. 

Today, Vercel did announce several AI starter kits, including the one we just looked at. I plan to cover the rest of the list. Take a look if you want to deploy and yry a project today. 

{% twitter 1633156135443382272 %}

Also, if you have a project leveraging OpenAI or similar, leave a link in the comments. I'd love to take a look and include it in my [9 days of OpenAI series](https://dev.to/bdougieyo/30-days-of-openai-1ek6).

_Find more AI projects using [OpenSauced](https://insights.opensauced.pizza/ai/repositories/filter/recent)_

Stay saucy.