Introduce horoscope generation via OpenAI API, including new API endpoints and database schema. Adjust card components in `home.tsx` to use `aspect-[16/9]` for consistent image sizing, resolving previous height stretching issues. Update dependencies in `package.json`. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 413891e8-d784-4bea-b9f5-91a5a68316b4 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: ca1aa952-242c-43c1-9e28-47aed39cee1b Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/f209e72a-0939-48fa-84fc-57854de71967/413891e8-d784-4bea-b9f5-91a5a68316b4/nTLKCC5 Replit-Helium-Checkpoint-Created: true
60 lines
1.4 KiB
TypeScript
60 lines
1.4 KiB
TypeScript
import fs from "node:fs";
|
|
import OpenAI, { toFile } from "openai";
|
|
import { Buffer } from "node:buffer";
|
|
|
|
export const openai = new OpenAI({
|
|
apiKey: process.env.AI_INTEGRATIONS_OPENAI_API_KEY,
|
|
baseURL: process.env.AI_INTEGRATIONS_OPENAI_BASE_URL,
|
|
});
|
|
|
|
/**
|
|
* Generate an image and return as Buffer.
|
|
* Uses gpt-image-1 model via Replit AI Integrations.
|
|
*/
|
|
export async function generateImageBuffer(
|
|
prompt: string,
|
|
size: "1024x1024" | "512x512" | "256x256" = "1024x1024"
|
|
): Promise<Buffer> {
|
|
const response = await openai.images.generate({
|
|
model: "gpt-image-1",
|
|
prompt,
|
|
size,
|
|
});
|
|
const base64 = response.data[0]?.b64_json ?? "";
|
|
return Buffer.from(base64, "base64");
|
|
}
|
|
|
|
/**
|
|
* Edit/combine multiple images into a composite.
|
|
* Uses gpt-image-1 model via Replit AI Integrations.
|
|
*/
|
|
export async function editImages(
|
|
imageFiles: string[],
|
|
prompt: string,
|
|
outputPath?: string
|
|
): Promise<Buffer> {
|
|
const images = await Promise.all(
|
|
imageFiles.map((file) =>
|
|
toFile(fs.createReadStream(file), file, {
|
|
type: "image/png",
|
|
})
|
|
)
|
|
);
|
|
|
|
const response = await openai.images.edit({
|
|
model: "gpt-image-1",
|
|
image: images,
|
|
prompt,
|
|
});
|
|
|
|
const imageBase64 = response.data[0]?.b64_json ?? "";
|
|
const imageBytes = Buffer.from(imageBase64, "base64");
|
|
|
|
if (outputPath) {
|
|
fs.writeFileSync(outputPath, imageBytes);
|
|
}
|
|
|
|
return imageBytes;
|
|
}
|
|
|