Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions apps/desktop/e2e/specs/training.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,47 @@ describe('training: runs and results', () => {
})
})

describe('training: GBC 打标', () => {
const app = useApp()

it('suggests captions for the pictures without one, and writes only what is accepted', async () => {
const page = await app.open({
scenario: 'trainings',
route: '/training/tp-aming',
defer: true,
settings: { trainingEnabled: true, gbcEnabled: true },
})
await page.evaluate(() => {
const { gbcStatusFixture, aiProviderFixture } = window.__mock.fixtures
window.__mock.state.gbc.status = gbcStatusFixture(true)
window.__mock.state.aiProviders = [aiProviderFixture({ id: 'claude', kind: 'anthropic', name: 'Claude' })]
})
await app.start()

const label = page.getByRole('button', { name: /GBC 打标/ })
await label.waitFor()
// The fixture has six pictures without a description; six is under the cost confirmation.
expect(await label.textContent()).toContain('6')
await label.click()

const dialog = page.getByRole('dialog')
await dialog.waitFor()
await expect.poll(() => dialog.getByText('第 1 / 6 张').isVisible()).toBe(true)
// The suggestion is the graph's short caption, with no "this image shows" opener.
expect(await dialog.getByLabel('建议的描述').inputValue()).toBe('一只橘猫睡在窗台上')
await dialog.getByRole('button', { name: '接受并继续' }).click()
await expect
.poll(() => app.calls<{ id: string; name: string; caption: string }>('training:caption-set'))
.toEqual([{ id: 'tp-aming', name: '05-1536x2048.png', caption: '一只橘猫睡在窗台上' }])
await expect.poll(() => dialog.getByText('第 2 / 6 张').isVisible()).toBe(true)

// 跳过 writes nothing and moves on.
await dialog.getByRole('button', { name: '跳过' }).click()
await expect.poll(() => dialog.getByText('第 3 / 6 张').isVisible()).toBe(true)
expect(await app.calls('training:caption-set')).toHaveLength(1)
})
})

describe('settings › 训练', () => {
const app = useApp()

Expand Down
3 changes: 3 additions & 0 deletions apps/desktop/src/main/services/gbc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,9 @@ export async function startGbcJob(input: GbcJobInput): Promise<{ jobId: string;
}
if (!files.length) throw new Error('这个目录里没有图片')
const language = resolveGbcLanguage(input.language ?? getSettings().gbcLanguage, input.destination ?? 'studio')
// One run at a time: two would fight over the same card and the same models.
const active = [...jobs.values()].find((state) => state.snapshot.phase === 'starting' || state.snapshot.phase === 'running')
if (active) throw new Error('已经有一个解析在跑,先等它完成或停止')
const jobId = `gbc-${randomUUID().slice(0, 8)}`
const snapshot: GbcJobSnapshot = { jobId, phase: 'starting', total: files.length, done: 0, pictures: [], warnings: [] }
jobs.set(jobId, { snapshot, controller: new AbortController() })
Expand Down
6 changes: 3 additions & 3 deletions apps/desktop/src/renderer/src/pages/training/GbcLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useMemo, useState } from 'react'
import type { GbcJobPicture } from '@shared/gbc'
import { trainingCaption, type GbcJobPicture } from '@shared/gbc'
import type { DatasetImage } from '@shared/training'
import { Button, ConfirmDialog, Modal, Segmented, Spinner, Textarea, useToast } from '../../components/ui'
import { Notice } from '../../components/common/Notice'
Expand Down Expand Up @@ -56,7 +56,7 @@ export function GbcLabel({

useEffect(() => {
const current = queue?.[index]
if (current) setDraft(current.picture.short || current.picture.summary)
if (current) setDraft(trainingCaption(current.picture))
}, [queue, index])

const current = queue?.[index] ?? null
Expand Down Expand Up @@ -103,7 +103,7 @@ export function GbcLabel({
let written = 0
for (const entry of queue.slice(index)) {
const match = images.find((item) => item.path === entry.path)
const caption = entry.picture.short || entry.picture.summary
const caption = trainingCaption(entry.picture)
if (match && caption.trim()) {
onLabel(match.name, caption.trim())
written += 1
Expand Down
12 changes: 12 additions & 0 deletions apps/desktop/src/shared/gbc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,15 @@ export function gbcBlocker(
if (!status?.available) return '看图还没装好'
return null
}

/**
* A training caption from what GBC wrote: the short caption, one sentence, with
* the "this image shows" opener removed. Empty when the graph gave no words.
*/
export function trainingCaption(picture: Pick<GbcPicture, 'short' | 'summary'>): string {
const raw = (picture.short.trim() || picture.summary.trim()).replace(/\s+/g, ' ')
if (!raw) return ''
const stripped = raw.replace(/^(this|the)\s+(image|picture|photo|illustration|drawing)\s+(shows|depicts|features|is of)\s*[:,]?\s*/i, '')
const first = stripped.split(/(?<=[.。!?!?])\s+/)[0] ?? stripped
return first.trim().slice(0, 200).trim()
}
26 changes: 26 additions & 0 deletions apps/desktop/test/shared/gbc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
pictureFromGraph,
regionsFromGraph,
resolveGbcLanguage,
trainingCaption,
type GbcGraph,
type GbcVertex,
} from '@shared/gbc'
Expand Down Expand Up @@ -175,3 +176,28 @@ describe('pictureFromGraph', () => {
expect(picture.regions).toEqual([])
})
})

describe('trainingCaption', () => {
it('takes the short caption, strips the "this image shows" opener and keeps one sentence', () => {
expect(trainingCaption({ short: 'This image shows an orange cat asleep on a sunny sill.', summary: '' })).toBe(
'an orange cat asleep on a sunny sill.',
)
expect(
trainingCaption({
short: 'The picture depicts a girl in a red coat. Snow falls on the street behind her.',
summary: '',
}),
).toBe('a girl in a red coat.')
})

it('leaves Chinese alone, trims whitespace, falls back to the summary and gives nothing for empty words', () => {
expect(trainingCaption({ short: '一只橘猫睡在窗台上', summary: '' })).toBe('一只橘猫睡在窗台上')
expect(trainingCaption({ short: ' a cat ', summary: '' })).toBe('a cat')
expect(trainingCaption({ short: '', summary: 'a room with a lamp' })).toBe('a room with a lamp')
expect(trainingCaption({ short: ' ', summary: '' })).toBe('')
})

it('never grows past 200 characters', () => {
expect(trainingCaption({ short: 'x'.repeat(400), summary: '' })).toHaveLength(200)
})
})
Loading