sol-mem-gen/src/app/api/flux/route.ts
2025-01-27 18:47:36 +05:30

101 lines
2.7 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { fal } from "@fal-ai/client"
import { FLUX_MODELS } from '../../../services/fluxService'
import { initializeDataSource } from '../../../data-source'
import { verifyPayment, isSignatureUsed, markSignatureAsUsed } from '../../../utils/verifyPayment'
if (!process.env.FAL_AI_KEY) {
throw new Error('FAL_AI_KEY is not configured in environment variables')
}
// Configure fal client
fal.config({
credentials: process.env.FAL_AI_KEY
})
// Consistent image size for all generations
const IMAGE_WIDTH: number = 1024
const IMAGE_HEIGHT: number = 1024
export async function POST(req: NextRequest): Promise<NextResponse> {
try {
await initializeDataSource();
const { prompt, modelId, transactionSignature } = await req.json()
if (!prompt || !modelId) {
return NextResponse.json(
{ error: 'Prompt and modelId are required' },
{ status: 400 }
)
}
if (!transactionSignature) {
return NextResponse.json(
{ error: 'Transaction signature is required' },
{ status: 400 }
)
}
const model = FLUX_MODELS.find((model) => model.modelId === modelId)
if (!model) {
return NextResponse.json(
{ error: 'Invalid modelId' },
{ status: 400 }
)
}
const expectedAmount = model.cost;
const isPaymentVerified = await verifyPayment(transactionSignature, expectedAmount)
if (!isPaymentVerified) {
return NextResponse.json(
{ error: 'Payment verification failed or transaction signature has already been used' },
{ status: 400 }
)
}
await markSignatureAsUsed(transactionSignature)
console.log('Generating with Flux model:', modelId)
console.log('Prompt:', prompt)
const result = await fal.subscribe(modelId, {
input: {
prompt: prompt,
image_size: {
width: IMAGE_WIDTH,
height: IMAGE_HEIGHT
},
},
logs: true,
onQueueUpdate: (update) => {
if (update.status === "IN_PROGRESS") {
console.log('Generation progress:', update.logs.map((log) => log.message))
}
},
})
console.log('Flux generation result:', result)
// Extract the image URL from the response
const imageUrl = result.data?.images?.[0]?.url
if (!imageUrl) {
console.error('No image URL in response:', result)
throw new Error('No image URL in response')
}
return NextResponse.json({ imageUrl })
} catch (error) {
console.error('Flux generation error:', error)
return NextResponse.json(
{ error: error instanceof Error ? error.message : 'Failed to generate image' },
{ status: 500 }
)
}
}
export const dynamic = 'force-dynamic'