Fix hero card aspect ratio and add horoscope generation functionality
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
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export { registerImageRoutes } from "./routes";
|
||||
export { openai, generateImageBuffer, editImages } from "./client";
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { Express, Request, Response } from "express";
|
||||
import { openai } from "./client";
|
||||
|
||||
export function registerImageRoutes(app: Express): void {
|
||||
app.post("/api/generate-image", async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { prompt, size = "1024x1024" } = req.body;
|
||||
|
||||
if (!prompt) {
|
||||
return res.status(400).json({ error: "Prompt is required" });
|
||||
}
|
||||
|
||||
const response = await openai.images.generate({
|
||||
model: "gpt-image-1",
|
||||
prompt,
|
||||
n: 1,
|
||||
size: size as "1024x1024" | "512x512" | "256x256",
|
||||
});
|
||||
|
||||
const imageData = response.data[0];
|
||||
res.json({
|
||||
url: imageData.url,
|
||||
b64_json: imageData.b64_json,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error generating image:", error);
|
||||
res.status(500).json({ error: "Failed to generate image" });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user