Update asr library preview
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Assistant, CallLog, InteractionDetail, KnowledgeBase, KnowledgeDocument, Voice, Workflow, WorkflowEdge, WorkflowNode } from '../types';
|
||||
import { ASRModel, Assistant, CallLog, InteractionDetail, KnowledgeBase, KnowledgeDocument, Voice, Workflow, WorkflowEdge, WorkflowNode } from '../types';
|
||||
import { apiRequest } from './apiClient';
|
||||
|
||||
type AnyRecord = Record<string, any>;
|
||||
@@ -64,6 +64,20 @@ const mapVoice = (raw: AnyRecord): Voice => ({
|
||||
isSystem: Boolean(readField(raw, ['isSystem', 'is_system'], false)),
|
||||
});
|
||||
|
||||
const mapASRModel = (raw: AnyRecord): ASRModel => ({
|
||||
id: String(readField(raw, ['id'], '')),
|
||||
name: readField(raw, ['name'], ''),
|
||||
vendor: readField(raw, ['vendor'], 'OpenAI Compatible'),
|
||||
language: readField(raw, ['language'], 'zh'),
|
||||
baseUrl: readField(raw, ['baseUrl', 'base_url'], ''),
|
||||
apiKey: readField(raw, ['apiKey', 'api_key'], ''),
|
||||
modelName: readField(raw, ['modelName', 'model_name'], ''),
|
||||
hotwords: readField(raw, ['hotwords'], []),
|
||||
enablePunctuation: Boolean(readField(raw, ['enablePunctuation', 'enable_punctuation'], true)),
|
||||
enableNormalization: Boolean(readField(raw, ['enableNormalization', 'enable_normalization'], true)),
|
||||
enabled: Boolean(readField(raw, ['enabled'], true)),
|
||||
});
|
||||
|
||||
const mapWorkflowNode = (raw: AnyRecord): WorkflowNode => ({
|
||||
name: readField(raw, ['name'], ''),
|
||||
type: readField(raw, ['type'], 'conversation') as 'conversation' | 'tool' | 'human' | 'end',
|
||||
@@ -246,6 +260,97 @@ export const previewVoice = async (id: string, text: string, speed?: number, api
|
||||
return response.audio_url;
|
||||
};
|
||||
|
||||
export const fetchASRModels = async (): Promise<ASRModel[]> => {
|
||||
const response = await apiRequest<{ list?: AnyRecord[] } | AnyRecord[]>('/asr');
|
||||
const list = Array.isArray(response) ? response : (response.list || []);
|
||||
return list.map((item) => mapASRModel(item));
|
||||
};
|
||||
|
||||
export const createASRModel = async (data: Partial<ASRModel>): Promise<ASRModel> => {
|
||||
const payload = {
|
||||
id: data.id || undefined,
|
||||
name: data.name || 'New ASR Model',
|
||||
vendor: data.vendor || 'OpenAI Compatible',
|
||||
language: data.language || 'zh',
|
||||
base_url: data.baseUrl || 'https://api.siliconflow.cn/v1',
|
||||
api_key: data.apiKey || '',
|
||||
model_name: data.modelName || undefined,
|
||||
hotwords: data.hotwords || [],
|
||||
enable_punctuation: data.enablePunctuation ?? true,
|
||||
enable_normalization: data.enableNormalization ?? true,
|
||||
enabled: data.enabled ?? true,
|
||||
};
|
||||
const response = await apiRequest<AnyRecord>('/asr', { method: 'POST', body: payload });
|
||||
return mapASRModel(response);
|
||||
};
|
||||
|
||||
export const updateASRModel = async (id: string, data: Partial<ASRModel>): Promise<ASRModel> => {
|
||||
const payload = {
|
||||
name: data.name,
|
||||
vendor: data.vendor,
|
||||
language: data.language,
|
||||
base_url: data.baseUrl,
|
||||
api_key: data.apiKey,
|
||||
model_name: data.modelName,
|
||||
hotwords: data.hotwords,
|
||||
enable_punctuation: data.enablePunctuation,
|
||||
enable_normalization: data.enableNormalization,
|
||||
enabled: data.enabled,
|
||||
};
|
||||
const response = await apiRequest<AnyRecord>(`/asr/${id}`, { method: 'PUT', body: payload });
|
||||
return mapASRModel(response);
|
||||
};
|
||||
|
||||
export const deleteASRModel = async (id: string): Promise<void> => {
|
||||
await apiRequest(`/asr/${id}`, { method: 'DELETE' });
|
||||
};
|
||||
|
||||
export type ASRPreviewResult = {
|
||||
success: boolean;
|
||||
transcript?: string;
|
||||
language?: string;
|
||||
confidence?: number;
|
||||
latency_ms?: number;
|
||||
message?: string;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
export const previewASRModel = async (
|
||||
id: string,
|
||||
file: File,
|
||||
options?: { language?: string; apiKey?: string }
|
||||
): Promise<ASRPreviewResult> => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
if (options?.language) {
|
||||
formData.append('language', options.language);
|
||||
}
|
||||
if (options?.apiKey) {
|
||||
formData.append('api_key', options.apiKey);
|
||||
}
|
||||
|
||||
const base = (import.meta.env.VITE_API_BASE_URL || 'http://127.0.0.1:8100/api').replace(/\/+$/, '');
|
||||
const url = `${base}/asr/${id}/preview`;
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
let data: ASRPreviewResult | null = null;
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch {
|
||||
data = null;
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const detail = (data as AnyRecord | null)?.error || (data as AnyRecord | null)?.detail || `Request failed: ${response.status}`;
|
||||
throw new Error(typeof detail === 'string' ? detail : `Request failed: ${response.status}`);
|
||||
}
|
||||
|
||||
return data || { success: false, error: 'Invalid preview response' };
|
||||
};
|
||||
|
||||
export const fetchWorkflows = async (): Promise<Workflow[]> => {
|
||||
const response = await apiRequest<{ list?: AnyRecord[] } | AnyRecord[]>('/workflows');
|
||||
const list = Array.isArray(response) ? response : (response.list || []);
|
||||
|
||||
Reference in New Issue
Block a user