Skip to content

Instantly share code, notes, and snippets.

@mariosaputra
Created August 3, 2024 01:00
Show Gist options
  • Save mariosaputra/c8c386c0960b732a186eb8fdc310520d to your computer and use it in GitHub Desktop.
Save mariosaputra/c8c386c0960b732a186eb8fdc310520d to your computer and use it in GitHub Desktop.
func uploadImage() {
guard let inputImage = inputImage else { return }
isLoading = true
Task {
#if DEBUG && targetEnvironment(simulator)
let openAIService = AIProxy.openAIService(
partialKey: ""
)
#else
let openAIService = AIProxy.openAIService(
partialKey: ""
)
#endif
guard let localURL = inputImage.openAILocalURLEncoding() else {
throw ClassifierDataLoaderError.couldNotCreateImageURL
}
let systemMessageContent = """
You are PlantIdentifyApp, an AI assistant created to help users identify plants from pictures.
Provide the user with the identified plant details in a RAW VALID JSON FORMAT, NO MARKDOWN, NOTHING ELSE. Including the following keys:
- name: Common name of the plant in \(selectedLanguage)
- genus: Genus name
- family: Family name
- botanicalName: Full botanical name (genus + species)
- desc: A brief description of the plant in \(selectedLanguage)
- wikipediaLink: Link to wikipedia in \(selectedLanguage) if available, if not then Wikipedia in English
classification.
don't write anything else. If the picture is not valid plant, ask user to provide another picture.
"""
do {
let response = try await openAIService.chatCompletionRequest(body: .init(
model: "gpt-4o",
messages: [
.init(
role: "system",
content: .text(systemMessageContent)
),
.init(
role: "user",
content: .parts(
[
.text("Here's the plant image:"),
.imageURL(localURL)
]
)
)
],
responseFormat: .type("json_object")
))
// print("response: \(response)")
let content = response.choices.first?.message.content ?? ""
// Ensure the response is a JSON string and decode it
if let data = content.data(using: .utf8) {
let decodedResponse = try JSONDecoder().decode(PlantInfo.self, from: data)
self.plantInfo = decodedResponse
} else {
self.infoText = "System error. Please try again."
if Locale(identifier: Locale.preferredLanguages.first!).language.languageCode?.identifier == "ja" {
self.infoText = "システムエラーです。もう一度お試しください"
}
}
} catch AIProxyError.unsuccessfulRequest(let statusCode, _) {
let error = ErrorHandler.getError(from: statusCode)
self.infoText = ErrorHandler.getMessage(for: error, language: selectedLanguage)
// print(responseBody)
} catch {
self.infoText = ErrorHandler.getMessage(for: .systemError, language: selectedLanguage)
}
isLoading = false
if plantInfo != nil {
if let imageData = inputImage.jpegData(compressionQuality: 0.4) {
saveHistory(with: imageData, with: plantInfo!)
}
}
identifyCount += 1
if (identifyCount%3 == 0) {
requestReview()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment