Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented a simple AI-model integration pipeline #14

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
implemented a simple AI-model integration pipeline
ramezmosad committed Feb 16, 2025
commit 9f325642e09b6ee06097aac3aace18d4c1d7a302
49 changes: 49 additions & 0 deletions app/imageSearch/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// app/imageSearch/page.tsx
"use client";

import React, { useState } from "react";
import { getImageEmbeddings } from "../lib/modelClient";

export default function ImageSearchPage() {
const [imageFile, setImageFile] = useState<File | null>(null);
const [results, setResults] = useState<number[] | null>(null);
const [loading, setLoading] = useState(false);

async function handleFileChange(e: React.ChangeEvent<HTMLInputElement>) {
if (!e.target.files || e.target.files.length === 0) return;
setImageFile(e.target.files[0]);
}

async function handleSearch() {
if (!imageFile) return;
setLoading(true);
try {
// For "image search," we get the embeddings from the local pipeline
const embeddingResult = await getImageEmbeddings(imageFile);
setResults(embeddingResult);
// In a real scenario, we'd compare these embeddings against
// known embeddings in our dataset to find the closest match.
} catch (error) {
console.error("Error running inference:", error);
} finally {
setLoading(false);
}
}

return (
<main style={{ padding: 20 }}>
<h1>Local Image Search (CLIP Embeddings)</h1>
<input type="file" accept="image/*" onChange={handleFileChange} />
<button onClick={handleSearch} disabled={!imageFile}>
{loading ? "Searching..." : "Get Image Embeddings"}
</button>

{results && (
<div>
<h2>Embedding Vector:</h2>
<pre>{JSON.stringify(results, null, 2)}</pre>
</div>
)}
</main>
);
}
33 changes: 33 additions & 0 deletions app/lib/modelClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// lib/modelClient.ts
"use client";

import { pipeline } from "@xenova/transformers";

/** Singleton references to loaded pipelines */
let imageEmbedder: any = null;

/**
* loading a CLIP embedding pipeline for image search (example).
* this will make it easy to use other models
*/
export async function loadEmbeddingPipeline() {
if (!imageEmbedder) {
// Using CLIP for image embeddings, just as an example
imageEmbedder = await pipeline(
"feature-extraction",
"Xenova/clip-vit-base-patch32"
);
}
return imageEmbedder;
}

/**
* Extract embeddings from an image.
* Returns a vector (array of floats) we can then compare with our dataset.
*/
export async function getImageEmbeddings(image: Blob | string) {
const embedder = await loadEmbeddingPipeline();
// The pipeline returns a nested array. We'll flatten or keep it nested as needed.
const result = await embedder(image);
return result;
}
1,683 changes: 1,573 additions & 110 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
"@capacitor/cli": "^7.0.1",
"@capacitor/core": "^7.0.1",
"@capacitor/ios": "^7.0.1",
"@xenova/transformers": "^2.17.2",
"next": "^14.2.23",
"react": "^18",
"react-dom": "^18"
1,634 changes: 1,018 additions & 616 deletions yarn.lock

Large diffs are not rendered by default.