|
|
|
const MCP_SERVER_URL = 'https://black-forest-labs-flux-1-kontext-dev.hf.space/run/FLUX_1_Kontext_Dev_infer'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const editBonsaiWithKontext = async ( |
|
imageBase64: string, |
|
prompt: string |
|
): Promise<string | null> => { |
|
try { |
|
const inputImage = `data:image/jpeg;base64,${imageBase64}`; |
|
|
|
|
|
|
|
|
|
|
|
const payload = { |
|
data: [ |
|
inputImage, |
|
prompt, |
|
-1, |
|
true, |
|
2.5, |
|
] |
|
}; |
|
|
|
const response = await fetch(MCP_SERVER_URL, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
}, |
|
body: JSON.stringify(payload), |
|
}); |
|
|
|
if (!response.ok) { |
|
const errorBody = await response.text(); |
|
console.error("MCP Server Error:", response.status, errorBody); |
|
throw new Error(`Request failed with status ${response.status}`); |
|
} |
|
|
|
const result = await response.json(); |
|
|
|
|
|
|
|
if (result && Array.isArray(result.data) && result.data.length > 0) { |
|
const outputImage = result.data[0]; |
|
|
|
if (typeof outputImage === 'string' && outputImage.startsWith('data:image/')) { |
|
return outputImage.split(',')[1]; |
|
} |
|
} |
|
|
|
console.error("Invalid response format from MCP server:", result); |
|
return null; |
|
|
|
} catch (error) { |
|
console.error("Error calling MCP tool:", error); |
|
return null; |
|
} |
|
}; |
|
|