balibabu
		
	commited on
		
		
					Commit 
							
							·
						
						16e3fae
	
1
								Parent(s):
							
							3c8c131
								
feat: Supports pronunciation while outputting text #2088 (#2227)
Browse files### What problem does this PR solve?
feat: Supports pronunciation while outputting text #2088
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- web/src/components/message-item/group-button.tsx +3 -1
- web/src/components/message-item/hooks.ts +10 -1
- web/src/components/message-item/index.tsx +1 -0
- web/src/hooks/file-manager-hooks.ts +10 -6
- web/src/hooks/logic-hooks.ts +1 -0
- web/src/interfaces/database/chat.ts +2 -0
- web/src/utils/common-util.ts +41 -0
    	
        web/src/components/message-item/group-button.tsx
    CHANGED
    
    | @@ -22,12 +22,14 @@ interface IProps { | |
| 22 | 
             
              content: string;
         | 
| 23 | 
             
              prompt?: string;
         | 
| 24 | 
             
              showLikeButton: boolean;
         | 
|  | |
| 25 | 
             
            }
         | 
| 26 |  | 
| 27 | 
             
            export const AssistantGroupButton = ({
         | 
| 28 | 
             
              messageId,
         | 
| 29 | 
             
              content,
         | 
| 30 | 
             
              prompt,
         | 
|  | |
| 31 | 
             
              showLikeButton,
         | 
| 32 | 
             
            }: IProps) => {
         | 
| 33 | 
             
              const { visible, hideModal, showModal, onFeedbackOk, loading } =
         | 
| @@ -38,7 +40,7 @@ export const AssistantGroupButton = ({ | |
| 38 | 
             
                showModal: showPromptModal,
         | 
| 39 | 
             
              } = useSetModalState();
         | 
| 40 | 
             
              const { t } = useTranslation();
         | 
| 41 | 
            -
              const { handleRead, ref, isPlaying } = useSpeech(content);
         | 
| 42 |  | 
| 43 | 
             
              const handleLike = useCallback(() => {
         | 
| 44 | 
             
                onFeedbackOk({ thumbup: true });
         | 
|  | |
| 22 | 
             
              content: string;
         | 
| 23 | 
             
              prompt?: string;
         | 
| 24 | 
             
              showLikeButton: boolean;
         | 
| 25 | 
            +
              audioBinary?: string;
         | 
| 26 | 
             
            }
         | 
| 27 |  | 
| 28 | 
             
            export const AssistantGroupButton = ({
         | 
| 29 | 
             
              messageId,
         | 
| 30 | 
             
              content,
         | 
| 31 | 
             
              prompt,
         | 
| 32 | 
            +
              audioBinary,
         | 
| 33 | 
             
              showLikeButton,
         | 
| 34 | 
             
            }: IProps) => {
         | 
| 35 | 
             
              const { visible, hideModal, showModal, onFeedbackOk, loading } =
         | 
|  | |
| 40 | 
             
                showModal: showPromptModal,
         | 
| 41 | 
             
              } = useSetModalState();
         | 
| 42 | 
             
              const { t } = useTranslation();
         | 
| 43 | 
            +
              const { handleRead, ref, isPlaying } = useSpeech(content, audioBinary);
         | 
| 44 |  | 
| 45 | 
             
              const handleLike = useCallback(() => {
         | 
| 46 | 
             
                onFeedbackOk({ thumbup: true });
         | 
    	
        web/src/components/message-item/hooks.ts
    CHANGED
    
    | @@ -52,7 +52,7 @@ export const useRemoveMessage = ( | |
| 52 | 
             
              return { onRemoveMessage, loading };
         | 
| 53 | 
             
            };
         | 
| 54 |  | 
| 55 | 
            -
            export const useSpeech = (content: string) => {
         | 
| 56 | 
             
              const ref = useRef<HTMLAudioElement>(null);
         | 
| 57 | 
             
              const { read } = useSpeechWithSse();
         | 
| 58 | 
             
              const player = useRef<SpeechPlayer>();
         | 
| @@ -94,6 +94,15 @@ export const useSpeech = (content: string) => { | |
| 94 | 
             
                }
         | 
| 95 | 
             
              }, [setIsPlaying, speech, isPlaying, pause]);
         | 
| 96 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 97 | 
             
              useEffect(() => {
         | 
| 98 | 
             
                initialize();
         | 
| 99 | 
             
              }, [initialize]);
         | 
|  | |
| 52 | 
             
              return { onRemoveMessage, loading };
         | 
| 53 | 
             
            };
         | 
| 54 |  | 
| 55 | 
            +
            export const useSpeech = (content: string, audioBinary?: string) => {
         | 
| 56 | 
             
              const ref = useRef<HTMLAudioElement>(null);
         | 
| 57 | 
             
              const { read } = useSpeechWithSse();
         | 
| 58 | 
             
              const player = useRef<SpeechPlayer>();
         | 
|  | |
| 94 | 
             
                }
         | 
| 95 | 
             
              }, [setIsPlaying, speech, isPlaying, pause]);
         | 
| 96 |  | 
| 97 | 
            +
              // useEffect(() => {
         | 
| 98 | 
            +
              //   if (audioBinary) {
         | 
| 99 | 
            +
              //     const units = hexStringToUint8Array(audioBinary);
         | 
| 100 | 
            +
              //     if (units) {
         | 
| 101 | 
            +
              //       player.current?.feed(units);
         | 
| 102 | 
            +
              //     }
         | 
| 103 | 
            +
              //   }
         | 
| 104 | 
            +
              // }, [audioBinary]);
         | 
| 105 | 
            +
             | 
| 106 | 
             
              useEffect(() => {
         | 
| 107 | 
             
                initialize();
         | 
| 108 | 
             
              }, [initialize]);
         | 
    	
        web/src/components/message-item/index.tsx
    CHANGED
    
    | @@ -131,6 +131,7 @@ const MessageItem = ({ | |
| 131 | 
             
                                content={item.content}
         | 
| 132 | 
             
                                prompt={item.prompt}
         | 
| 133 | 
             
                                showLikeButton={showLikeButton}
         | 
|  | |
| 134 | 
             
                              ></AssistantGroupButton>
         | 
| 135 | 
             
                            )
         | 
| 136 | 
             
                          ) : (
         | 
|  | |
| 131 | 
             
                                content={item.content}
         | 
| 132 | 
             
                                prompt={item.prompt}
         | 
| 133 | 
             
                                showLikeButton={showLikeButton}
         | 
| 134 | 
            +
                                audioBinary={item.audio_binary}
         | 
| 135 | 
             
                              ></AssistantGroupButton>
         | 
| 136 | 
             
                            )
         | 
| 137 | 
             
                          ) : (
         | 
    	
        web/src/hooks/file-manager-hooks.ts
    CHANGED
    
    | @@ -207,13 +207,17 @@ export const useUploadFile = () => { | |
| 207 | 
             
                    formData.append('file', file);
         | 
| 208 | 
             
                    formData.append('path', pathList[index]);
         | 
| 209 | 
             
                  });
         | 
| 210 | 
            -
                   | 
| 211 | 
            -
             | 
| 212 | 
            -
                     | 
| 213 | 
            -
             | 
| 214 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 215 | 
             
                  }
         | 
| 216 | 
            -
                  return data.retcode;
         | 
| 217 | 
             
                },
         | 
| 218 | 
             
              });
         | 
| 219 |  | 
|  | |
| 207 | 
             
                    formData.append('file', file);
         | 
| 208 | 
             
                    formData.append('path', pathList[index]);
         | 
| 209 | 
             
                  });
         | 
| 210 | 
            +
                  try {
         | 
| 211 | 
            +
                    const { data } = await fileManagerService.uploadFile(formData);
         | 
| 212 | 
            +
                    if (data.retcode === 0) {
         | 
| 213 | 
            +
                      message.success(t('message.uploaded'));
         | 
| 214 | 
            +
                      setPaginationParams(1);
         | 
| 215 | 
            +
                      queryClient.invalidateQueries({ queryKey: ['fetchFileList'] });
         | 
| 216 | 
            +
                    }
         | 
| 217 | 
            +
                    return data.retcode;
         | 
| 218 | 
            +
                  } catch (error) {
         | 
| 219 | 
            +
                    console.log('🚀 ~ useUploadFile ~ error:', error);
         | 
| 220 | 
             
                  }
         | 
|  | |
| 221 | 
             
                },
         | 
| 222 | 
             
              });
         | 
| 223 |  | 
    	
        web/src/hooks/logic-hooks.ts
    CHANGED
    
    | @@ -433,6 +433,7 @@ export const useSelectDerivedMessages = () => { | |
| 433 | 
             
                        role: MessageType.Assistant,
         | 
| 434 | 
             
                      }),
         | 
| 435 | 
             
                      prompt: answer.prompt,
         | 
|  | |
| 436 | 
             
                    },
         | 
| 437 | 
             
                  ];
         | 
| 438 | 
             
                });
         | 
|  | |
| 433 | 
             
                        role: MessageType.Assistant,
         | 
| 434 | 
             
                      }),
         | 
| 435 | 
             
                      prompt: answer.prompt,
         | 
| 436 | 
            +
                      audio_binary: answer.audio_binary,
         | 
| 437 | 
             
                    },
         | 
| 438 | 
             
                  ];
         | 
| 439 | 
             
                });
         | 
    	
        web/src/interfaces/database/chat.ts
    CHANGED
    
    | @@ -70,6 +70,7 @@ export interface Message { | |
| 70 | 
             
              doc_ids?: string[];
         | 
| 71 | 
             
              prompt?: string;
         | 
| 72 | 
             
              id?: string;
         | 
|  | |
| 73 | 
             
            }
         | 
| 74 |  | 
| 75 | 
             
            export interface IReference {
         | 
| @@ -84,6 +85,7 @@ export interface IAnswer { | |
| 84 | 
             
              conversationId?: string;
         | 
| 85 | 
             
              prompt?: string;
         | 
| 86 | 
             
              id?: string;
         | 
|  | |
| 87 | 
             
            }
         | 
| 88 |  | 
| 89 | 
             
            export interface Docagg {
         | 
|  | |
| 70 | 
             
              doc_ids?: string[];
         | 
| 71 | 
             
              prompt?: string;
         | 
| 72 | 
             
              id?: string;
         | 
| 73 | 
            +
              audio_binary?: string;
         | 
| 74 | 
             
            }
         | 
| 75 |  | 
| 76 | 
             
            export interface IReference {
         | 
|  | |
| 85 | 
             
              conversationId?: string;
         | 
| 86 | 
             
              prompt?: string;
         | 
| 87 | 
             
              id?: string;
         | 
| 88 | 
            +
              audio_binary?: string;
         | 
| 89 | 
             
            }
         | 
| 90 |  | 
| 91 | 
             
            export interface Docagg {
         | 
    	
        web/src/utils/common-util.ts
    CHANGED
    
    | @@ -72,3 +72,44 @@ export const toFixed = (value: unknown, fixed = 2) => { | |
| 72 | 
             
              }
         | 
| 73 | 
             
              return value;
         | 
| 74 | 
             
            };
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 72 | 
             
              }
         | 
| 73 | 
             
              return value;
         | 
| 74 | 
             
            };
         | 
| 75 | 
            +
             | 
| 76 | 
            +
            export const stringToUint8Array = (str: string) => {
         | 
| 77 | 
            +
              // const byteString = str.replace(/b'|'/g, '');
         | 
| 78 | 
            +
              const byteString = str.slice(2, -1);
         | 
| 79 | 
            +
             | 
| 80 | 
            +
              const uint8Array = new Uint8Array(byteString.length);
         | 
| 81 | 
            +
              for (let i = 0; i < byteString.length; i++) {
         | 
| 82 | 
            +
                uint8Array[i] = byteString.charCodeAt(i);
         | 
| 83 | 
            +
              }
         | 
| 84 | 
            +
             | 
| 85 | 
            +
              return uint8Array;
         | 
| 86 | 
            +
            };
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            export const hexStringToUint8Array = (hex: string) => {
         | 
| 89 | 
            +
              const arr = hex.match(/[\da-f]{2}/gi);
         | 
| 90 | 
            +
              if (Array.isArray(arr)) {
         | 
| 91 | 
            +
                return new Uint8Array(
         | 
| 92 | 
            +
                  arr.map(function (h) {
         | 
| 93 | 
            +
                    return parseInt(h, 16);
         | 
| 94 | 
            +
                  }),
         | 
| 95 | 
            +
                );
         | 
| 96 | 
            +
              }
         | 
| 97 | 
            +
            };
         | 
| 98 | 
            +
             | 
| 99 | 
            +
            export function hexToArrayBuffer(input: string) {
         | 
| 100 | 
            +
              if (typeof input !== 'string') {
         | 
| 101 | 
            +
                throw new TypeError('Expected input to be a string');
         | 
| 102 | 
            +
              }
         | 
| 103 | 
            +
             | 
| 104 | 
            +
              if (input.length % 2 !== 0) {
         | 
| 105 | 
            +
                throw new RangeError('Expected string to be an even number of characters');
         | 
| 106 | 
            +
              }
         | 
| 107 | 
            +
             | 
| 108 | 
            +
              const view = new Uint8Array(input.length / 2);
         | 
| 109 | 
            +
             | 
| 110 | 
            +
              for (let i = 0; i < input.length; i += 2) {
         | 
| 111 | 
            +
                view[i / 2] = parseInt(input.substring(i, i + 2), 16);
         | 
| 112 | 
            +
              }
         | 
| 113 | 
            +
             | 
| 114 | 
            +
              return view.buffer;
         | 
| 115 | 
            +
            }
         |