初始化提交

This commit is contained in:
2025-09-14 02:05:42 +08:00
parent 1a3730454e
commit 9f94e92eaf
19 changed files with 385 additions and 322 deletions

View File

@@ -1,12 +1,12 @@
import { GoogleGenAI } from '@google/genai';
// Note: In production, this should be handled via a backend proxy
// 注意:在生产环境中,这应该通过后端代理处理
const API_KEY = import.meta.env.VITE_GEMINI_API_KEY || 'demo-key';
const genAI = new GoogleGenAI({ apiKey: API_KEY });
export interface GenerationRequest {
prompt: string;
referenceImages?: string[]; // base64 array
referenceImages?: string[]; // base64数组
temperature?: number;
seed?: number;
}
@@ -14,7 +14,7 @@ export interface GenerationRequest {
export interface EditRequest {
instruction: string;
originalImage: string; // base64
referenceImages?: string[]; // base64 array
referenceImages?: string[]; // base64数组
maskImage?: string; // base64
temperature?: number;
seed?: number;
@@ -22,7 +22,7 @@ export interface EditRequest {
export interface SegmentationRequest {
image: string; // base64
query: string; // "the object at pixel (x,y)" or "the red car"
query: string; // "像素(x,y)处的对象" 或 "红色汽车"
}
export class GeminiService {
@@ -30,7 +30,7 @@ export class GeminiService {
try {
const contents: any[] = [{ text: request.prompt }];
// Add reference images if provided
// 如果提供了参考图像则添加
if (request.referenceImages && request.referenceImages.length > 0) {
request.referenceImages.forEach(image => {
contents.push({
@@ -57,8 +57,8 @@ export class GeminiService {
return images;
} catch (error) {
console.error('Error generating image:', error);
throw new Error('Failed to generate image. Please try again.');
console.error('生成图像时出错:', error);
throw new Error('生成图像失败。请重试。');
}
}
@@ -74,7 +74,7 @@ export class GeminiService {
},
];
// Add reference images if provided
// 如果提供了参考图像则添加
if (request.referenceImages && request.referenceImages.length > 0) {
request.referenceImages.forEach(image => {
contents.push({
@@ -110,28 +110,28 @@ export class GeminiService {
return images;
} catch (error) {
console.error('Error editing image:', error);
throw new Error('Failed to edit image. Please try again.');
console.error('编辑图像时出错:', error);
throw new Error('编辑图像失败。请重试。');
}
}
async segmentImage(request: SegmentationRequest): Promise<any> {
try {
const prompt = [
{ text: `Analyze this image and create a segmentation mask for: ${request.query}
{ text: `分析此图像并为以下对象创建分割遮罩: ${request.query}
Return a JSON object with this exact structure:
返回具有此确切结构的JSON对象:
{
"masks": [
{
"label": "description of the segmented object",
"label": "分割对象的描述",
"box_2d": [x, y, width, height],
"mask": "base64-encoded binary mask image"
"mask": "base64编码的二进制遮罩图像"
}
]
}
Only segment the specific object or region requested. The mask should be a binary PNG where white pixels (255) indicate the selected region and black pixels (0) indicate the background.` },
仅分割请求的特定对象或区域。遮罩应该是二进制PNG其中白色像素(255)表示选定区域,黑色像素(0)表示背景。` },
{
inlineData: {
mimeType: "image/png",
@@ -148,21 +148,21 @@ Only segment the specific object or region requested. The mask should be a binar
const responseText = response.candidates[0].content.parts[0].text;
return JSON.parse(responseText);
} catch (error) {
console.error('Error segmenting image:', error);
throw new Error('Failed to segment image. Please try again.');
console.error('分割图像时出错:', error);
throw new Error('分割图像失败。请重试。');
}
}
private buildEditPrompt(request: EditRequest): string {
const maskInstruction = request.maskImage
? "\n\nIMPORTANT: Apply changes ONLY where the mask image shows white pixels (value 255). Leave all other areas completely unchanged. Respect the mask boundaries precisely and maintain seamless blending at the edges."
? "\n\n重要: 仅在遮罩图像显示白色像素(值255)的地方应用更改。完全不更改所有其他区域。精确遵守遮罩边界并在边缘保持无缝混合。"
: "";
return `Edit this image according to the following instruction: ${request.instruction}
return `根据以下指令编辑此图像: ${request.instruction}
Maintain the original image's lighting, perspective, and overall composition. Make the changes look natural and seamlessly integrated.${maskInstruction}
保持原始图像的光照、透视和整体构图。使更改看起来自然且无缝集成。${maskInstruction}
Preserve image quality and ensure the edit looks professional and realistic.`;
保持图像质量并确保编辑看起来专业且逼真。`;
}
}