Skip to content

Commit

Permalink
Expose output attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
vijaysharm committed Jan 22, 2024
1 parent b731aef commit 38b2863
Show file tree
Hide file tree
Showing 22 changed files with 100 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ public struct AudioClassificationOutput: Codable {
/**
* The label for the class (model specific)
*/
let label: String
public let label: String

/**
* A float that represents how likely it is that the audio file belongs to this class.
*/
let score: Float
public let score: Float
}

extension HfInference {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Huggingface.Inference/Tasks/audio/AudioToAudio.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ public struct AudioToAudioOutput: Codable {
/**
* The label for the audio output (model specific)
*/
let label: String
public let label: String

/**
* Base64 encoded audio output.
*/
let blob: String
public let blob: String

/**
* Content-type for blob, e.g. audio/flac
*/
let contentType: String
public let contentType: String

enum CodingKeys : String, CodingKey {
case label = "label"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation

public struct AutomaticSpeechRecognitionOutput: Codable {
let text: String
public let text: String
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import Foundation
extension HfInference {
public func request(
) async throws -> [String] {
[]
fatalError("Unimplemented")
}

public func streamingRequest(
) async throws -> [String] {
[]
fatalError("Unimplemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import Foundation

public struct ImageClassificationOutput: Codable {
let label: String
let score: Float
public let label: String
public let score: Float
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import Foundation

public struct ImageSegmentationOutput: Codable {
let label: String
let mask: String
let score: Float
public let label: String
public let mask: String
public let score: Float
}

extension HfInference {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Huggingface.Inference/Tasks/cv/ImageToText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import Foundation

public struct ImageToTextOutput: Codable {
let generatedText: String
public let generatedText: String
}

extension HfInference {
Expand Down
14 changes: 7 additions & 7 deletions Sources/Huggingface.Inference/Tasks/cv/ObjectDetection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import Foundation

public struct ObjectDetectionBoxOutput: Codable {
let xmin: Int
let xmax: Int
let ymin: Int
let ymax: Int
public let xmin: Int
public let xmax: Int
public let ymin: Int
public let ymax: Int
}

public struct ObjectDetectionOutput: Codable {
let label: String
let score: Float
let bod: ObjectDetectionBoxOutput
public let label: String
public let score: Float
public let box: ObjectDetectionBoxOutput
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ struct ZeroShotImageClassificationInputs: Codable {
}

public struct ZeroShotImageClassificationOutput: Codable {
let label: String
let score: Float
public let label: String
public let score: Float
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ public struct DocumentQuestionAnsweringOutput: Codable {
/**
* A string that’s the answer within the document.
*/
let answer: String?
let start: Int?
let end: Int?
public let answer: String?
public let start: Int?
public let end: Int?
/**
* A float that represents how likely that the answer is correct
*/
let score: Float?

public let score: Float?
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public struct VisualQuestionAnsweringOutput: Codable {
/**
* A string that’s the answer to a visual question.
*/
let answer: String
public let answer: String
/**
* Answer correctness score.
*/
let score: Float
public let score: Float
}

extension HfInference {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Huggingface.Inference/Tasks/nlp/Conversational.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ struct ConversationalInputs: Codable {
}

public struct ConversationOutput: Codable {
let pastUserInputs: [String]
let generatedResponses: [String]
public let pastUserInputs: [String]
public let generatedResponses: [String]
}

public struct ConversationalOutput: Codable {
let generatedText: String
let conversation: ConversationOutput
public let generatedText: String
public let conversation: ConversationOutput
}

extension HfInference {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Huggingface.Inference/Tasks/nlp/FillMask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ struct FillMaskInput: Codable {
}

public struct FillMaskOutput: Codable {
let sequence: String
let score: Float
let token: Int
let tokenStr: String
public let sequence: String
public let score: Float
public let token: Int
public let tokenStr: String
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ struct QuestionAnsweringInputs: Codable {
}

public struct QuestionAnsweringOutput: Codable {
let answer: String
let end: Int
let score: Float
let start: Int
public let answer: String
public let end: Int
public let score: Float
public let start: Int
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct SummarizationInput: Codable {
}

public struct SummarizationOutput: Codable {
let summaryText: String
public let summaryText: String
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ public struct TableQuestionAnsweringOutput: Codable {
/**
* The aggregator used to get the answer
*/
let aggregator: String
public let aggregator: String
/**
* The plaintext answer
*/
let answer: String
public let answer: String
/**
* A list of coordinates of the cells contents
*/
let cells: [String]
public let cells: [String]
/**
* a list of coordinates of the cells referenced in the answer
*/
let coordinates: [[Float]]
public let coordinates: [[Float]]
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ struct TextClassificationInputs: Codable {
}

public struct TextClassificationOutput: Codable {
let label: String
let score: Float
public let label: String
public let score: Float
}

extension HfInference {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Huggingface.Inference/Tasks/nlp/TextGeneration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct TextGenerationInputs: Codable {
}

public struct TextGenerationOutput: Codable {
let generatedText: String
public let generatedText: String
}

extension HfInference {
Expand Down Expand Up @@ -76,7 +76,7 @@ extension HfInference {
model: String? = nil,
options: HfInferenceOptions = .init()
) async throws -> [String] {
fatalError()
fatalError("Unimplemented")
// return try await execute(
// model: model,
// taskHint: .translation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ struct TokenClassificationInputs: Codable {
}

public struct TokenClassificationOutput: Codable {
let entityGroup: String
let score: Float
let word: String
let start: Int
let end: Int
public let entityGroup: String
public let score: Float
public let word: String
public let start: Int
public let end: Int
}

extension HfInference {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Huggingface.Inference/Tasks/nlp/Translation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct TranslationInputs: Codable {
}

public struct TranslationOutput: Codable {
let translationText: String
public let translationText: String
}

extension HfInference {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ struct ZeroShotClassificationInputs: Codable {
}

public struct ZeroShotClassificationOutput: Codable {
let labels: [String]
let scores: [Float]
let sequence: String
public let labels: [String]
public let scores: [Float]
public let sequence: String
}

extension HfInference {
Expand Down
41 changes: 41 additions & 0 deletions Tests/Huggingface.Swift.Tests/Huggingface_SwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,44 @@ final class HuggingfaceTests: XCTestCase {
XCTAssertEqual(expected[0].answer, actual[0].answer)
}
}

extension HuggingfaceTests {
func xtestExecute() async throws {
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 230.0
sessionConfig.timeoutIntervalForResource = 260.0
let session = URLSession(configuration: sessionConfig)

let hf = HfInference(
session: session,
logLevel: logLevel
)
let actual = try await hf.textToSpeech(
inputs: "Hello World!",
model: "facebook/fastspeech2-en-ljspeech"
)
let filename = UUID().uuidString
try actual.write(to: URL(filePath: "/tmp/\(filename)"))
}

func testExecute() async throws {

let data = try Data(contentsOf: Bundle.module.url(forResource: "invoice", withExtension: "png")!)

let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 230.0
sessionConfig.timeoutIntervalForResource = 260.0
let session = URLSession(configuration: sessionConfig)

let hf = HfInference(
session: session,
logLevel: logLevel
)

let actual = try await hf.fillMask(
inputs: ["Help me make [MASK]"],
options: .init(waitForModel: true)
)
print(actual)
}
}

0 comments on commit 38b2863

Please sign in to comment.