You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.3 KiB
80 lines
2.3 KiB
package echomeet
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"yunyan/pb"
|
|
"yunyan/sys/google/gemini"
|
|
gspeech "yunyan/sys/google/speech"
|
|
gtranslate "yunyan/sys/google/translate"
|
|
)
|
|
|
|
// Google 系单元服务实例(由 provider_registry.go 按服务池字段构造):
|
|
// 转写=Cloud Speech、翻译=Cloud Translation、总结=Gemini。
|
|
|
|
// Google Cloud Speech-to-Text V1 LongRunningRecognize(要求 gs:// 音频)
|
|
type googleTranscriber struct {
|
|
sys gspeech.ISys
|
|
}
|
|
|
|
func (this *googleTranscriber) Submit(_ context.Context, req SubmitRequest) (*SubmitResult, error) {
|
|
taskID, err := this.sys.CreateTask(req.AudioURL, req.Language, req.EnableSpeaker, req.CallbackURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("google speech create task: %w", err)
|
|
}
|
|
return &SubmitResult{TaskID: taskID}, nil
|
|
}
|
|
|
|
func (this *googleTranscriber) Query(_ context.Context, taskID, _ string) (*QueryResult, error) {
|
|
status, contexts, err := this.sys.QueryTask(taskID)
|
|
switch status {
|
|
case gspeech.StatusSuccess:
|
|
out := make([]*pb.ContextStruct, 0, len(contexts))
|
|
for _, c := range contexts {
|
|
out = append(out, &pb.ContextStruct{
|
|
Content: c.Content,
|
|
Starttime: c.StartTime,
|
|
Endtime: c.EndTime,
|
|
Speaker: c.Speaker,
|
|
})
|
|
}
|
|
return &QueryResult{Status: StatusSuccess, Contexts: out}, nil
|
|
case gspeech.StatusRunning:
|
|
return &QueryResult{Status: StatusRunning}, nil
|
|
default:
|
|
return &QueryResult{Status: StatusFailed}, fmt.Errorf("google speech query failed: %w", err)
|
|
}
|
|
}
|
|
|
|
// Google Cloud Translation v3
|
|
type googleTranslator struct {
|
|
sys gtranslate.ISys
|
|
}
|
|
|
|
func (this *googleTranslator) Translate(ctx context.Context, from, to string, texts []string) ([]string, error) {
|
|
return this.sys.Translate(ctx, toGoogleTranslateLang(from), toGoogleTranslateLang(to), texts)
|
|
}
|
|
|
|
// Gemini 总结
|
|
type geminiSummarizer struct {
|
|
sys gemini.ISys
|
|
}
|
|
|
|
func (this *geminiSummarizer) Chat(ctx context.Context, messages []ChatMessage) (string, error) {
|
|
if this.sys == nil {
|
|
return "", fmt.Errorf("gemini summarizer not initialized")
|
|
}
|
|
msgs := make([]gemini.Message, 0, len(messages))
|
|
for _, m := range messages {
|
|
msgs = append(msgs, gemini.Message{Role: m.Role, Content: m.Content, Images: m.Images})
|
|
}
|
|
resp, err := this.sys.Chat(ctx, msgs)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if resp == nil {
|
|
return "", fmt.Errorf("gemini chat: empty response")
|
|
}
|
|
return resp.Content, nil
|
|
}
|
|
|