Skip to content

Commit

Permalink
bug: fixing model dictonary
Browse files Browse the repository at this point in the history
  • Loading branch information
Maia-jp committed Apr 29, 2024
1 parent 772cb99 commit 274ef91
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 10 deletions.
1 change: 0 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
Expand Down
5 changes: 5 additions & 0 deletions Sources/aiXplainKit/Errors/Model+Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ enum ModelError: Error, Equatable {

/// Error reportet when using a file/URL as input and something went wrong
case failToGenerateAFilePayload(error: String)

/// An unsupported value type was encountered while transforming the dictonary into a model input
case typeNotRecognizedWhileCreatingACombinedInput

var localizedDescription: String {
switch self {
Expand All @@ -72,6 +75,8 @@ enum ModelError: Error, Equatable {
return "No URL was provided for the Model Run service. Please set a URL using `AiXplainKit.keyManager`."
case .failToGenerateAFilePayload(error: let error):
return "Something went wrong while generating a payload for the model from a file: \(error)"
case .typeNotRecognizedWhileCreatingACombinedInput:
return "An unsupported value type was encountered during dictonary model input generation. Please ensure that all values in the dictonary are either URLs or strings."
}
}
}
24 changes: 21 additions & 3 deletions Sources/aiXplainKit/Modules/Model/Input/ModelInput+Dictonary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,29 @@

import Foundation

extension Dictionary: ModelInput where Key == String, Value == Any {
extension Dictionary: ModelInput where Key == String, Value == ModelInput {
public func generateInputPayloadForModel() async throws -> Data {
guard let jsonData = try? JSONSerialization.data(withJSONObject: self, options: []) else {
return Data()
var parsedSequence: [String:String] = [:]
let fileUploadManager = FileUploadManager()

for (_, keyValuePair) in self.enumerated() {
let (key, value) = keyValuePair

switch value {
case let url as URL:
let remoteURL = try await fileUploadManager.uploadDataIfNeedIt(from: url)
parsedSequence.updateValue(remoteURL.absoluteString, forKey: key)
case let string as String:
parsedSequence.updateValue(string, forKey: key)
default:
throw PipelineError.typeNotRecognizedWhileCreatingACombinedInput
}
}

guard let jsonData = try? JSONSerialization.data(withJSONObject: parsedSequence, options: []) else {
throw PipelineError.inputEncodingError
}

return jsonData
}
}
16 changes: 10 additions & 6 deletions Sources/aiXplainKit/Modules/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ do {
public final class Model: DecodableAsset, CustomStringConvertible {

/// Unique identifier for the model.
public let id: String
public var id: String

/// Name of the model.
public let name: String
Expand Down Expand Up @@ -95,20 +95,24 @@ public final class Model: DecodableAsset, CustomStringConvertible {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

id = try container.decode(String.self, forKey: .id)
id = try container.decodeIfPresent(String.self, forKey: .id) ?? ""
name = try container.decode(String.self, forKey: .name)
modelDescription = try container.decodeIfPresent(String.self, forKey: .description) ?? "An ML Model"
supplier = try container.decode(Supplier.self, forKey: .supplier)

version = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .version).decodeIfPresent(String.self, forKey: .id) ?? "-"
// Check if the "supplier" key is present and not an empty object
if var supplierContainer = try? container.nestedUnkeyedContainer(forKey: .supplier) {
supplier = try supplierContainer.decode(Supplier.self)
} else {
// Provide a default value for the supplier if it's missing or an empty object
supplier = Supplier(id: 0, name: "no", code: "")
}

version = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .version).decodeIfPresent(String.self, forKey: .id) ?? "-"
pricing = try container.decode(Pricing.self, forKey: .pricing)

privacy = nil
license = nil

logger = Logger(subsystem: "AiXplain", category: "Model(\(name)")

networking = Networking()
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/aiXplainKit/Provider/ModelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public final class ModelProvider {
do {
logger.debug("\(String(data: response.0, encoding: .utf8)!)")
let fetchedModel = try JSONDecoder().decode(Model.self, from: response.0)
if fetchedModel.id.count <= 1 {
fetchedModel.id = modelID
}

logger.info("\(fetchedModel.name) fetched")
return fetchedModel
Expand Down

0 comments on commit 274ef91

Please sign in to comment.