From d5e7bc49ce1ce7e8005089164815df3550dfffee Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Fri, 28 Mar 2025 12:22:45 -0600 Subject: fix async for Swift 6 --- iTexSnip/Utils/TexTellerModel.swift | 55 +++++++++++++++++++++++++------- iTexSnip/Views/DetailedSnippetView.swift | 24 ++++++++------ 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/iTexSnip/Utils/TexTellerModel.swift b/iTexSnip/Utils/TexTellerModel.swift index 0104bfb..153b184 100644 --- a/iTexSnip/Utils/TexTellerModel.swift +++ b/iTexSnip/Utils/TexTellerModel.swift @@ -20,12 +20,14 @@ public struct TexTellerModel { private let tokenizer: RobertaTokenizerFast public init() throws { - guard let encoderModelPath = Bundle.main.path(forResource: "encoder_model", ofType: "onnx") + // guard let encoderModelPath = Bundle.main.path(forResource: "encoder_model", ofType: "onnx") + guard let encoderModelPath = Bundle.main.path(forResource: "encoder-quant", ofType: "onnx") else { print("Encoder model not found...") throw ModelError.encoderModelNotFound } - guard let decoderModelPath = Bundle.main.path(forResource: "decoder_model", ofType: "onnx") + // guard let decoderModelPath = Bundle.main.path(forResource: "decoder_model", ofType: "onnx") + guard let decoderModelPath = Bundle.main.path(forResource: "decoder-quant", ofType: "onnx") else { print("Decoder model not found...") throw ModelError.decoderModelNotFound @@ -162,19 +164,50 @@ public struct TexTellerModel { throw ModelError.imageError } + public func texIt(_ imageData: Data, rawString: Bool = false, debug: Bool = false) async throws + -> String + { + guard let nsImage = NSImage(data: imageData) else { + throw ModelError.imageError + } + + return try await self.texIt(nsImage, rawString: rawString, debug: debug) + } + public func texIt(_ image: NSImage, rawString: Bool = false, debug: Bool = false) async throws -> String { - return try await withCheckedThrowingContinuation { continuation in - DispatchQueue.global(qos: .userInitiated).async { - do { - let result = try self.texIt(image, rawString: rawString, debug: debug) - continuation.resume(returning: result) - } catch { - continuation.resume(throwing: error) - } - } + guard let imageCopy = image.copy() as? NSImage, + let tiffRepresentation = imageCopy.tiffRepresentation + else { + throw ModelError.imageError } + + return try await Task { + let imageToProcess = NSImage(data: tiffRepresentation) ?? imageCopy + return try self.texIt(imageToProcess, rawString: rawString, debug: debug) + }.value + + // let imageData = imageCopy.tiffRepresentation + // guard let imageData = imageData else { + // throw ModelError.imageError + // } + + // return try await withCheckedThrowingContinuation { continuation in + // DispatchQueue.global(qos: .userInitiated).async { + // do { + // let recreatedImage = NSImage(data: imageData) + // guard let recreatedImage = recreatedImage else { + // continuation.resume(throwing: ModelError.imageError) + // return + // } + // let result = try self.texIt(recreatedImage, rawString: rawString, debug: debug) + // continuation.resume(returning: result) + // } catch { + // continuation.resume(throwing: error) + // } + // } + // } } } diff --git a/iTexSnip/Views/DetailedSnippetView.swift b/iTexSnip/Views/DetailedSnippetView.swift index 251fb99..4f836a8 100644 --- a/iTexSnip/Views/DetailedSnippetView.swift +++ b/iTexSnip/Views/DetailedSnippetView.swift @@ -93,19 +93,23 @@ struct DetailedSnippetView: View { } HStack { Spacer() - LaTeXEquationView(equation: snippet.transcribedText!) - .clipped() - .scaledToFit() - .frame(height: 100) + if snippet.transcribedText != nil { + LaTeXEquationView(equation: snippet.transcribedText!) + .clipped() + .scaledToFit() + .frame(height: 100) + } Spacer() } VStack { - LaTeXCopyView(latex: snippet.transcribedText!, textStart: "", textEnd: "") - LaTeXCopyView(latex: snippet.transcribedText!, textStart: "$", textEnd: "$") - LaTeXCopyView(latex: snippet.transcribedText!, textStart: "$$", textEnd: "$$") - LaTeXCopyView( - latex: snippet.transcribedText!, textStart: "\\begin{equation}", - textEnd: "\\end{equation}") + if snippet.transcribedText != nil { + LaTeXCopyView(latex: snippet.transcribedText!, textStart: "", textEnd: "") + LaTeXCopyView(latex: snippet.transcribedText!, textStart: "$", textEnd: "$") + LaTeXCopyView(latex: snippet.transcribedText!, textStart: "$$", textEnd: "$$") + LaTeXCopyView( + latex: snippet.transcribedText!, textStart: "\\begin{equation}", + textEnd: "\\end{equation}") + } } }.frame(height: 450) Spacer() -- cgit v1.2.3