Currently, I am facing an issue while trying to upload videos from a URL using @google/generative-ai in Next.js. While I have successfully learned how to work with videos stored on my local machine, I am struggling to do the same with videos from external sources.
Below is my existing function for uploading videos to @google/generative-ai:
"use server"
const { GoogleGenerativeAI } = require("@google/generative-ai");
import { GoogleAIFileManager, FileState } from "@google/generative-ai/server";
import { redirect } from "next/navigation";
import fetchVideoById from "./fetchVideoById";
// Initialize GoogleAIFileManager with your API_KEY.
const fileManager = new GoogleAIFileManager(process.env.API_KEY);
// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
// Choose a Gemini model.
const model = genAI.getGenerativeModel({
model: "gemini-1.5-pro",
});
export async function generateSummary(formData) {
const rows = await fetchVideoById(formData.get("id"))
const url = rows["url"]
console.log("Uploading file...")
const fileManager = new GoogleAIFileManager(process.env.API_KEY);
// Upload the file and specify a display name.
const uploadResponse = await fileManager.uploadFile(url, {
mimeType: "video/mp4",
displayName: rows["title"],
});
// View the response.
console.log(`Uploaded file ${uploadResponse.file.displayName} as: ${uploadResponse.file.uri}`);
const name = uploadResponse.file.name;
// Poll getFile() on a set interval (10 seconds here) to check file state.
let file = await fileManager.getFile(name);
while (file.state === FileState.PROCESSING) {
process.stdout.write(".")
// Fetch the file from the API again
file = await fileManager.getFile(name)
}
if (file.state === FileState.FAILED) {
throw new Error("Video processing failed.");
}
// When file.state is ACTIVE, the file is ready to be used for inference.
console.log(`File ${file.displayName} is ready for inference as ${file.uri}`);
const result = await model.generateContent([
{
fileData: {
mimeType: uploadResponse.file.mimeType,
fileUri: uploadResponse.file.uri
}
},
{ text: "Summarize this video." },
]);
// Handle the response of generated text
console.log(result.response.text())
return result.response.text()
console.log("Deleting file...")
await fileManager.deleteFile(file.name);
console.log("Deleted file.")
}
The error message I encounter is:
Error: ENOENT: no such file or directory, open 'C:\Users\n_mac\Desktop\Coding\summa\front-end\https:\m9x5emw6q3oaze3r.public.blob.vercel-storage.com\monkeyman64\6999DBC5-2D93-4220-BC43-3C16C9A5D9C6-IZzFC1THZXPSgeAK1NPo3uCVxA091l.mp4'
It seems that the system is searching for the file on my local machine instead of Vercel Blob where the files are actually stored. Any assistance regarding this matter would be highly appreciated.