diff options
author | Navan Chauhan <navanchauhan@gmail.com> | 2025-03-28 12:22:45 -0600 |
---|---|---|
committer | Navan Chauhan <navanchauhan@gmail.com> | 2025-03-28 12:22:45 -0600 |
commit | d5e7bc49ce1ce7e8005089164815df3550dfffee (patch) | |
tree | c7f109cb0122b33f51373762f57c9fbb7f9ce17d /iTexSnip | |
parent | 8523d5787fdd0119aa7188ca00b43701419be35d (diff) |
fix async for Swift 6
Diffstat (limited to 'iTexSnip')
-rw-r--r-- | iTexSnip/Utils/TexTellerModel.swift | 55 | ||||
-rw-r--r-- | 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() |