CoolFace
Apppublic

SFM2001/spititout

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes
hfSpaceService.ts68 linesDownload Raw Back to services
1export enum ChatMode {2  VENTING = "VENTING",3  GUIDING = "GUIDING",4}5 6export interface Message {7  role: "user" | "model";8  text: string;9  timestamp: number;10  audio?: string; // base64 audio string (for user messages)11  aiAudio?: string; // base64 WAV audio string (for model responses)12}13 14interface ChatResponse {15  text?: string;16  transcript?: string;17  error?: string;18}19 20interface SpeechResponse {21  audio?: string;22  error?: string;23}24 25export async function chatWithSpaceModel(26  history: Message[],27  mode: ChatMode,28  audioBase64?: string29) {30  try {31    const response = await fetch("/api/chat", {32      method: "POST",33      headers: { "Content-Type": "application/json" },34      body: JSON.stringify({ history, mode, audioBase64 }),35    });36 37    if (!response.ok) {38      throw new Error(`Chat request failed: ${response.status}`);39    }40 41    const data = (await response.json()) as ChatResponse;42    return data.text || "喂?听得到吗?我刚才卡了一下。";43  } catch (error) {44    console.error("HF Space chat error:", error);45    return "抱歉,我现在的本地模型卡住了,稍等一下再试。";46  }47}48 49export async function generateSpeech(text: string) {50  try {51    const response = await fetch("/api/speech", {52      method: "POST",53      headers: { "Content-Type": "application/json" },54      body: JSON.stringify({ text }),55    });56 57    if (!response.ok) {58      throw new Error(`Speech request failed: ${response.status}`);59    }60 61    const data = (await response.json()) as SpeechResponse;62    return data.audio || null;63  } catch (error) {64    console.error("HF Space TTS error:", error);65    return null;66  }67}68