Spaces:
Paused
Paused
Update app/api/image/route.ts
Browse files- app/api/image/route.ts +43 -4
app/api/image/route.ts
CHANGED
|
@@ -2,9 +2,36 @@ import { NextRequest, NextResponse } from "next/server";
|
|
| 2 |
import { GoogleGenerativeAI } from "@google/generative-ai";
|
| 3 |
import { HistoryItem, HistoryPart } from "@/lib/types";
|
| 4 |
|
| 5 |
-
//
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
// Define the model ID for Gemini 2.0 Flash experimental
|
| 10 |
const MODEL_ID = "gemini-2.0-flash-exp";
|
|
@@ -31,6 +58,18 @@ export async function POST(req: NextRequest) {
|
|
| 31 |
);
|
| 32 |
}
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
// Get the model with the correct configuration
|
| 35 |
const model = genAI.getGenerativeModel({
|
| 36 |
model: MODEL_ID,
|
|
@@ -181,4 +220,4 @@ export async function POST(req: NextRequest) {
|
|
| 181 |
{ status: 500 }
|
| 182 |
);
|
| 183 |
}
|
| 184 |
-
}
|
|
|
|
| 2 |
import { GoogleGenerativeAI } from "@google/generative-ai";
|
| 3 |
import { HistoryItem, HistoryPart } from "@/lib/types";
|
| 4 |
|
| 5 |
+
// Function to get a random API key from environment variables
|
| 6 |
+
function getRandomApiKey(): string {
|
| 7 |
+
// Get all environment variables
|
| 8 |
+
const envVars = process.env;
|
| 9 |
+
|
| 10 |
+
// Collect all Gemini API keys from environment variables
|
| 11 |
+
const apiKeys: string[] = [];
|
| 12 |
+
|
| 13 |
+
// Iterate through all environment variables to find Gemini API keys
|
| 14 |
+
for (const key in envVars) {
|
| 15 |
+
// Check if the key matches our pattern (GEMINI_API_KEY or GEMINI_API_KEY followed by a number)
|
| 16 |
+
if (key === 'GEMINI_API_KEY' || key.match(/^GEMINI_API_KEY\d+$/)) {
|
| 17 |
+
const apiKey = envVars[key];
|
| 18 |
+
if (apiKey && apiKey.trim() !== '') {
|
| 19 |
+
apiKeys.push(apiKey);
|
| 20 |
+
}
|
| 21 |
+
}
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
// If no keys found, return empty string
|
| 25 |
+
if (apiKeys.length === 0) {
|
| 26 |
+
console.error("No Gemini API keys found in environment variables");
|
| 27 |
+
return "";
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
// Select a random API key from the collection
|
| 31 |
+
const randomIndex = Math.floor(Math.random() * apiKeys.length);
|
| 32 |
+
console.log(`Using API key ${randomIndex + 1} of ${apiKeys.length} available keys`);
|
| 33 |
+
return apiKeys[randomIndex];
|
| 34 |
+
}
|
| 35 |
|
| 36 |
// Define the model ID for Gemini 2.0 Flash experimental
|
| 37 |
const MODEL_ID = "gemini-2.0-flash-exp";
|
|
|
|
| 58 |
);
|
| 59 |
}
|
| 60 |
|
| 61 |
+
// Get a random API key
|
| 62 |
+
const GEMINI_API_KEY = getRandomApiKey();
|
| 63 |
+
if (!GEMINI_API_KEY) {
|
| 64 |
+
return NextResponse.json(
|
| 65 |
+
{ error: "No valid API key found" },
|
| 66 |
+
{ status: 500 }
|
| 67 |
+
);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// Initialize the Google Gen AI client with the selected API key
|
| 71 |
+
const genAI = new GoogleGenerativeAI(GEMINI_API_KEY);
|
| 72 |
+
|
| 73 |
// Get the model with the correct configuration
|
| 74 |
const model = genAI.getGenerativeModel({
|
| 75 |
model: MODEL_ID,
|
|
|
|
| 220 |
{ status: 500 }
|
| 221 |
);
|
| 222 |
}
|
| 223 |
+
}
|