Add Core ML conversion

#3
by azadeh1972 - opened

Core ML conversion, task=text2text-generation, precision=float32

Hello @azadeh1972 , could you by any chance have a swift script on how to use this model .

import Foundation

// Define the translation function using a REST API (e.g., Hugging Face's API)
func translate(text: String, completion: @escaping (String?) -> Void) {
// Set up the URL for the translation model (replace with the actual API endpoint)
import requests

let url = URL(string: "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-fr-en")!

// Create the request
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")

// Set up the API token if needed (replace with your token)
let apiKey = "YOUR_HUGGINGFACE_API_KEY"
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")

// Create the request body
let requestBody: [String: Any] = ["inputs": text]
request.httpBody = try? JSONSerialization.data(withJSONObject: requestBody)

// Start the network request
let task = URLSession.shared.dataTask(with: request) { data, response, error in
    if let data = data, error == nil {
        let translatedText = String(data: data, encoding: .utf8)
        completion(translatedText)
    } else {
        completion(nil)
    }
}

task.resume()

}

// Example usage
translate(text: "Bonjour tout le monde") { translatedText in
if let translatedText = translatedText {
print("Translated text: (translatedText)")
} else {
print("Failed to translate text.")
}
}

Hi @azadeh1972 thank you for your response I mean use of this on device model coreml/text2text-generation/decoder_float32_model.mlpackage/Data/com.apple.CoreML/model.mlmodel +0 -0 coreml/text2text-generation/decoder_float32_model.mlpackage/Data/com.apple.CoreML/weights/weight.bin +3 -0 coreml/text2text-generation/decoder_float32_model.mlpackage/Manifest.json +18 -0 coreml/text2text-generation/encoder_float32_model.mlpackage/Data/com.apple.CoreML/model.mlmodel +0 -0 coreml/text2text-generation/encoder_float32_model.mlpackage/Data/com.apple.CoreML/weights/weight.bin +3 -0 coreml/text2text-generation/encoder_float32_model.mlpackage/Manifest.json

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment