Created
August 3, 2024 01:00
-
-
Save mariosaputra/c8c386c0960b732a186eb8fdc310520d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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