実現したいこと
SwiftUIでGemini APIを用いて、クライアント側から入力された音声ファイルを文字起こしする機能を作っています。
発生している問題・分からないこと
このコードを書くとまず
Argument type 'Data' does not conform to expected type 'ThrowingPartsRepresentable'
というエラーが出ます。
そして
Insert ' as! ThrowingPartsRepresentable'
という提案がなされ、その通りにして実行すると
[GoogleGenerativeAI] Model models/gemini-pro initialized. To enable additional logging, add -GoogleGenerativeAIDebugLogEnabled
as a launch argument in Xcode.
Could not cast value of type 'Foundation.Data' (0x1e0347de8) to 'GoogleGenerativeAI.ThrowingPartsRepresentable' (0x103052738).
というエラーがコンソール上に表示されてしまいます
エラーメッセージ
error
1[GoogleGenerativeAI] Model models/gemini-pro initialized. To enable additional logging, add `-GoogleGenerativeAIDebugLogEnabled` as a launch argument in Xcode. 2Could not cast value of type 'Foundation.Data' (0x1e0347de8) to 'GoogleGenerativeAI.ThrowingPartsRepresentable' (0x103052738).
該当のソースコード
Swift
1import SwiftUI 2import GoogleGenerativeAI 3import UniformTypeIdentifiers 4 5struct ContentView: View { 6 7 @State private var selectedFile: URL? = nil 8 @State private var isPickerPresented = false 9 @State private var transcriptionResult: String? = nil 10 11 var body: some View { 12 VStack { 13 Button(action: { 14 isPickerPresented.toggle() // Toggle to present file picker 15 }) { 16 Text("ファイルを選択") 17 .padding() 18 } 19 .fileImporter( 20 isPresented: $isPickerPresented, 21 allowedContentTypes: [.audio], 22 allowsMultipleSelection: false 23 ) { result in 24 switch result { 25 case .success(let urls): 26 selectedFile = urls.first 27 case .failure(let error): 28 print("Error selecting file: \(error.localizedDescription)") 29 } 30 } 31 32 if let selectedFile = selectedFile { 33 Text("選択されたファイル: \(selectedFile.lastPathComponent)") 34 .padding() 35 36 // Save selected file button 37 Button(action: { 38 Task { 39 await runGemini(fileURL: selectedFile) 40 } 41 }) { 42 Text("ファイルを保存") 43 .padding() 44 } 45 46 if let transcriptionResult = transcriptionResult { 47 Text("Transcription Result: \(transcriptionResult)") 48 .padding() 49 } 50 } else { 51 Text("ファイルが選択されていません") 52 .padding() 53 } 54 } 55 } 56 57 func runGemini(fileURL: URL) async { 58 // Setup the model 59 let model = GenerativeModel( 60 name: "models/gemini-pro", 61 apiKey: APIKey.default 62 ) 63 64 do { 65 let data = try Data(contentsOf: fileURL) 66 let response = try await model.generateContent( 67 "この音声ファイルを文字起こししてください", data 68 ) 69 transcriptionResult = response.text 70 } catch { 71 print("Error during transcription: \(error)") 72 // より詳細なエラー処理 73 } 74 } 75} 76 77#Preview { 78 ContentView() 79} 80
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
一次情報まで遡ったが、文字の処理と画像処理にのみ終始しており、Swift上で音声ファイルを処理する方法は見つけられなかった
補足
特になし

あなたの回答
tips
プレビュー