-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3d965b6
Showing
48 changed files
with
2,934 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
8 changes: 8 additions & 0 deletions
8
.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
66 changes: 66 additions & 0 deletions
66
.swiftpm/xcode/xcshareddata/xcschemes/Huggingface.Swift-Package.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1500" | ||
version = "1.7"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "Huggingface.Swift" | ||
BuildableName = "Huggingface.Swift" | ||
BlueprintName = "Huggingface.Swift" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
shouldAutocreateTestPlan = "YES"> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "Huggingface.Swift" | ||
BuildableName = "Huggingface.Swift" | ||
BlueprintName = "Huggingface.Swift" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
92 changes: 92 additions & 0 deletions
92
.swiftpm/xcode/xcshareddata/xcschemes/Huggingface.Swift.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1500" | ||
version = "1.7"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
<BuildActionEntries> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "YES" | ||
buildForArchiving = "YES" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "Huggingface.Swift" | ||
BuildableName = "Huggingface.Swift" | ||
BlueprintName = "Huggingface.Swift" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
<BuildActionEntry | ||
buildForTesting = "YES" | ||
buildForRunning = "YES" | ||
buildForProfiling = "NO" | ||
buildForArchiving = "NO" | ||
buildForAnalyzing = "YES"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "Huggingface.Swift.Tests" | ||
BuildableName = "Huggingface.Swift.Tests" | ||
BlueprintName = "Huggingface.Swift.Tests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</BuildActionEntry> | ||
</BuildActionEntries> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
shouldAutocreateTestPlan = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "Huggingface.Swift.Tests" | ||
BuildableName = "Huggingface.Swift.Tests" | ||
BlueprintName = "Huggingface.Swift.Tests" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
<MacroExpansion> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "Huggingface.Swift" | ||
BuildableName = "Huggingface.Swift" | ||
BlueprintName = "Huggingface.Swift" | ||
ReferencedContainer = "container:"> | ||
</BuildableReference> | ||
</MacroExpansion> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// swift-tools-version: 5.9 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "Huggingface.Swift", | ||
platforms: [ | ||
.iOS(.v13), | ||
.macOS(.v10_15), | ||
], | ||
products: [ | ||
.library( | ||
name: "Huggingface.Swift", | ||
targets: ["Huggingface.Inference"]), | ||
], | ||
targets: [ | ||
.target( | ||
name: "Huggingface.Inference" | ||
), | ||
.testTarget( | ||
name: "Huggingface.Swift.Tests", | ||
dependencies: [ | ||
"Huggingface.Inference" | ||
], | ||
resources: [ | ||
.copy("assets/elephants.png"), | ||
.copy("assets/sample1.flac"), | ||
.copy("assets/invoice.png") | ||
] | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// | ||
// HfInference.swift | ||
// | ||
// | ||
|
||
import Foundation | ||
|
||
public class HfInference { | ||
private let accessToken: String | ||
private let fetch: FetchProtocol | ||
private let metadata: HuggingfaceModelsMetdataManager | ||
|
||
let log: LoggerProtocol | ||
|
||
init( | ||
accessToken: String = "", | ||
fetch: FetchProtocol, | ||
logLevel: LogLevel = .info | ||
) { | ||
Logger.level = logLevel | ||
self.log = Logger.make(tag: HfInference.self) | ||
self.accessToken = accessToken | ||
self.fetch = fetch | ||
self.metadata = HuggingfaceModelsMetdataManager(fetch: fetch) | ||
} | ||
|
||
convenience init( | ||
accessToken: String = "", | ||
session: URLSession = URLSession.shared, | ||
logLevel: LogLevel = .info | ||
) { | ||
self.init( | ||
accessToken: accessToken, | ||
fetch: Fetch(session: session), | ||
logLevel: logLevel | ||
) | ||
} | ||
|
||
// MARK: - Private Methods | ||
|
||
func execute<T: Decodable>( | ||
model: String?, | ||
taskHint: HuggingfaceModelsMetdata.CodingKeys, | ||
body: Codable, | ||
requestType: ModelContentType, | ||
responseType: ModelContentType, | ||
options: HfInferenceOptions | ||
) async throws -> T { | ||
let modelId = try await metadata.resolve( | ||
model: model, | ||
taskHint: taskHint | ||
) | ||
|
||
var bodyAsData: Data | ||
if let data = body as? Data { | ||
bodyAsData = data | ||
} else { | ||
let encoder = JSONEncoder() | ||
encoder.keyEncodingStrategy = .convertToSnakeCase | ||
bodyAsData = try encoder.encode(body) | ||
|
||
log.with(level: .debug) { | ||
if let string = String(data: bodyAsData, encoding: .utf8) { | ||
$0("body for \(taskHint.stringValue) -> \(string)") | ||
} | ||
} | ||
} | ||
|
||
let request = ModelRequest( | ||
task: taskHint, | ||
options: .init( | ||
accessToken: accessToken, | ||
model: modelId, | ||
url: url(model: modelId, task: nil), | ||
body: bodyAsData, | ||
requestType: requestType, | ||
responseType: responseType, | ||
useCache: options.useCache, | ||
waitForModel: options.waitForModel | ||
) | ||
) | ||
|
||
return try await run(request: request, options: options) | ||
} | ||
|
||
private func run<T: Decodable>( | ||
request: ModelRequest, | ||
options: HfInferenceOptions | ||
) async throws -> T { | ||
let result: Result<T, RequestError> = await request.get(using: fetch) | ||
switch result { | ||
case .failure(let error): | ||
switch error { | ||
case .modelIsCurrentlyLoading: | ||
if options.retryOnError && !options.waitForModel { | ||
return try await run(request: request, options: .init( | ||
retryOnError: options.retryOnError, | ||
useCache: options.useCache, | ||
waitForModel: true | ||
)) | ||
} else { | ||
throw error | ||
} | ||
default: | ||
throw error | ||
} | ||
case .success(let otuput): | ||
return otuput | ||
} | ||
} | ||
|
||
private func url(model: String, task: String?) -> URL { | ||
if (model.hasPrefix("/") || model.hasPrefix("http:") || model.hasPrefix("https:")), let url = URL(string: model) { | ||
return url | ||
} | ||
|
||
let baseUrl = "https://api-inference.huggingface.co" | ||
if let task { | ||
return URL(string: "\(baseUrl)/pipeline/\(task)/\(model)")! | ||
} | ||
|
||
return URL(string: "\(baseUrl)/models/\(model)")! | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// HfInferenceOptions.swift | ||
// | ||
|
||
import Foundation | ||
|
||
public struct HfInferenceOptions: Codable { | ||
/** | ||
* (Default: true) Boolean. If a request 503s and wait_for_model is set to false, the request will be retried with the same parameters but with wait_for_model set to true. | ||
*/ | ||
let retryOnError: Bool | ||
/** | ||
* (Default: true). Boolean. There is a cache layer on the inference API to speedup requests we have already seen. Most models can use those results as is as models are deterministic (meaning the results will be the same anyway). However if you use a non deterministic model, you can set this parameter to prevent the caching mechanism from being used resulting in a real new query. | ||
*/ | ||
let useCache: Bool | ||
|
||
/** | ||
* (Default: false) Boolean. If the model is not ready, wait for it instead of receiving 503. It limits the number of requests required to get your inference done. It is advised to only set this flag to true after receiving a 503 error as it will limit hanging in your application to known places. | ||
*/ | ||
let waitForModel: Bool | ||
|
||
public init( | ||
retryOnError: Bool = true, | ||
useCache: Bool = true, | ||
waitForModel: Bool = false | ||
) { | ||
self.retryOnError = retryOnError | ||
self.useCache = useCache | ||
self.waitForModel = waitForModel | ||
} | ||
} |
Oops, something went wrong.