Created
July 1, 2024 12:15
-
-
Save pH-7/e7253b6a6f72c58eb3b9c92d897856c4 to your computer and use it in GitHub Desktop.
Generate AI videos from a given image using Neon DB, Pinecone, and Replicate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Client } from 'pg'; | |
import { PineconeClient } from '@pinecone-database/pinecone'; | |
import Replicate from 'replicate'; | |
// Initialize clients | |
const neonClient = new Client({ | |
connectionString: 'your_neon_connection_string' | |
}); | |
const pinecone = new PineconeClient(); | |
await pinecone.init({ | |
environment: 'your_pinecone_environment', | |
apiKey: 'your_pinecone_api_key' | |
}); | |
const replicate = new Replicate({ | |
auth: 'your_replicate_api_token', | |
}); | |
// Function to store image in NeonDB | |
async function storeImageInNeonDB(imageBuffer: Buffer, imageName: string): Promise<string> { | |
await neonClient.connect(); | |
const query = 'INSERT INTO images(name, data) VALUES($1, $2) RETURNING id'; | |
const result = await neonClient.query(query, [imageName, imageBuffer]); | |
await neonClient.end(); | |
return result.rows[0].id; | |
} | |
// Function to index image metadata in Pinecone | |
async function indexImageMetadata(imageId: string, metadata: object) { | |
const index = pinecone.Index('your_pinecone_index_name'); | |
await index.upsert([{ | |
id: imageId, | |
values: [], // You might want to add image embeddings here | |
metadata: metadata | |
}]); | |
} | |
// Function to generate video using Replicate | |
async function generateVideo(imageUrl: string): Promise<string> { | |
const output = await replicate.run( | |
"stability-ai/stable-video-diffusion:3f0457e4619daac51203dedb472816fd4af51f3149fa7a9e0b5ffcf1b8172438", | |
{ | |
input: { | |
input_image: imageUrl, | |
video_length: "14_frames_with_svd_xt", | |
sizing_strategy: "maintain_aspect_ratio", | |
frames_per_second: 6 | |
} | |
} | |
); | |
return output; | |
} | |
// Main function to orchestrate the process | |
async function generateAIVideo(imageBuffer: Buffer, imageName: string, metadata: object) { | |
// Store image in NeonDB | |
const imageId = await storeImageInNeonDB(imageBuffer, imageName); | |
// Index metadata in Pinecone | |
await indexImageMetadata(imageId, metadata); | |
// Generate video using Replicate | |
// Note: You'll need to provide a publicly accessible URL for the image | |
const imageUrl = `https://your-image-hosting-service.com/${imageId}`; | |
const videoUrl = await generateVideo(imageUrl); | |
return videoUrl; | |
} | |
// Usage | |
const imageBuffer = /* load your image buffer */; | |
const imageName = 'example.jpg'; | |
const metadata = { description: 'An example image' }; | |
generateAIVideo(imageBuffer, imageName, metadata) | |
.then(videoUrl => console.log('Generated video URL:', videoUrl)) | |
.catch(error => console.error('Error:', error)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code provides a basic structure for generating your desired videos. You'll need to:
pg
for NeonDB,@pinecone-database/pinecone
for Pinecone, andreplicate
for Replicate.