1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| package main
import ( "bytes" "context" "encoding/json" "fmt" "io" "log" "net/http" "os" "time"
"cloud.google.com/go/vertexai/genai" "google.golang.org/api/option" )
// 定义响应结构体 type Response struct { Message string `json:"message"` Data string `json:"data"` Code int `json:"code"` }
// 定义请求结构体,formData格式,字段是audio,二进制binary类型 type Request struct { Audio string `json:"audio"` }
func main() { // 注册路由处理函数 http.HandleFunc("/accent-test", handleAccentTest)
// 启动服务器在8080端口 fmt.Println("Server starting on :8080") http.ListenAndServe(":8080", nil) }
// 受控生成 为 Gemini API 指定 MIME 回答类型:https://cloud.google.com/vertex-ai/generative-ai/docs/samples/generativeaionvertexai-gemini-controlled-generation-response-schema?hl=zh_cn
// 语音识别支持远程url加载以及base64编码
// API处理函数 func handleAccentTest(w http.ResponseWriter, r *http.Request) { // 获取上传的文件 file, _, err := r.FormFile("audio") if err != nil { http.Error(w, "Error retrieving the file", http.StatusBadRequest) return } defer file.Close()
// 创建本地文件,并添加当前时间戳 currentTime := time.Now().Format("20060102150405") dst, err := os.Create(fmt.Sprintf("./%s_%s.wav", "recording", currentTime)) if err != nil { http.Error(w, "Error creating the file", http.StatusInternalServerError) return } defer dst.Close()
// 将上传的文件内容写入本地文件测试 var buf bytes.Buffer if _, err := io.Copy(io.MultiWriter(dst, &buf), file); err != nil { http.Error(w, "Error saving the file", http.StatusInternalServerError) return }
// 将文件内容转为bytes bytes := buf.Bytes() // 写入文件 // dst, err = os.Create(fmt.Sprintf("./%s_%s.txt", "recording", currentTime)) // if err != nil { // http.Error(w, "Error creating the file", http.StatusInternalServerError) // return // } // defer dst.Close()
// if _, err := dst.WriteString(encodedAudio); err != nil { // http.Error(w, "Error saving the file", http.StatusInternalServerError) // return // }
generateContentFromText(w, "learned-advice-445914-s7", bytes)
response := Response{ Message: "success", Code: 200, Data: "success", }
// 设置响应头 // w.Header().Set("Content-Type", "application/json")
// 编码并返回JSON json.NewEncoder(w).Encode(response) }
func generateContentFromText(w io.Writer, projectID string, bytes []byte) error { // temperature := 0.4 // 位置 location := "us-central1" // 模型 modelName := "gemini-2.0-flash-exp"
ctx := context.Background() // 创建上下文 client, err := genai.NewClient(ctx, projectID, location, option.WithCredentialsFile("./learned-advice-445914-s7-ecaa18ea2a74.json")) // 创建客户端 if err != nil { return fmt.Errorf("error creating client: %w", err) } gemini := client.GenerativeModel(modelName) // 创建Gemini模型
// https://ai.google.dev/gemini-api/docs/audio?hl=zh-cn&lang=go // prompt := []genai.Part{ // genai.Blob{MIMEType: "audio/wav", Data: bytes}, // genai.Text("这个音频文件,告诉我你识别到的内容?输出文字给我:"), // }
prompt := []genai.Part{ genai.Blob{MIMEType: "audio/wav", Data: bytes}, genai.Text("这个音频文件,告诉我你识别到的内容?输出文字给我:"), }
resp, err := gemini.GenerateContent(ctx, prompt...) if err != nil { log.Fatal(err) }
// Handle the response of generated text for _, c := range resp.Candidates { if c.Content != nil { fmt.Println(*c.Content) } } return nil }
|