-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initiate support for Google Gemini
- Loading branch information
1 parent
aa0b60d
commit 788d370
Showing
10 changed files
with
153 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
21 changes: 21 additions & 0 deletions
21
Easydict/App/Assets.xcassets/service-icon/Gemini.imageset/Contents.json
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,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"filename" : "Gemini.png", | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,94 @@ | ||
// | ||
// GeminiService.swift | ||
// Easydict | ||
// | ||
// Created by Jerry on 2024-01-02. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import GoogleGenerativeAI | ||
|
||
@objc(EZGeminiService) | ||
public final class GeminiService: QueryService { | ||
override public func serviceType() -> ServiceType { | ||
.gemini | ||
} | ||
|
||
override public func link() -> String? { | ||
"https://bard.google.com/chat" | ||
} | ||
|
||
override public func name() -> String { | ||
NSLocalizedString("gemini_translate", comment: "The name of Gemini Translate") | ||
} | ||
|
||
override public func supportLanguagesDictionary() -> MMOrderedDictionary<AnyObject, AnyObject> { | ||
// TODO: Replace MMOrderedDictionary. | ||
let orderedDict = MMOrderedDictionary<AnyObject, AnyObject>() | ||
GeminiTranslateType.supportLanguagesDictionary.forEach { key, value in | ||
orderedDict.setObject(value as NSString, forKey: key.rawValue as NSString) | ||
} | ||
return orderedDict | ||
} | ||
|
||
override public func ocr(_: EZQueryModel) async throws -> EZOCRResult { | ||
NSLog("Gemini Translate does not support OCR") | ||
throw QueryServiceError.notSupported | ||
} | ||
|
||
override public func needPrivateAPIKey() -> Bool { | ||
true | ||
} | ||
|
||
override public func hasPrivateAPIKey() -> Bool { | ||
if apiKey == defaultAPIKey { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
private let defaultAPIKey = "" /* .decryptAES() */ | ||
|
||
// easydict://writeKeyValue?EZGeminiAPIKey=xxx | ||
private var apiKey: String { | ||
let apiKey = UserDefaults.standard.string(forKey: EZGeminiAPIKey) | ||
if let apiKey, !apiKey.isEmpty { | ||
return apiKey | ||
} else { | ||
return defaultAPIKey | ||
} | ||
} | ||
|
||
override public func autoConvertTraditionalChinese() -> Bool { | ||
true | ||
} | ||
|
||
override public func translate(_ text: String, from: Language, to: Language, completion: @escaping (EZQueryResult, Error?) -> Void) { | ||
Task { | ||
// https://github.com/google/generative-ai-swift | ||
do { | ||
var resultString = "" | ||
let prompt = "translate this \(from.rawValue) text into \(to.rawValue): \(text)" | ||
print("gemini prompt: \(prompt)") | ||
let model = GenerativeModel(name: "gemini-pro", apiKey: apiKey) | ||
let outputContentStream = model.generateContentStream(prompt) | ||
|
||
// stream response | ||
for try await outputContent in outputContentStream { | ||
guard let line = outputContent.text else { | ||
return | ||
} | ||
|
||
print("gemini response: \(line)") | ||
resultString += line | ||
result.translatedResults = [resultString] | ||
completion(result, nil) | ||
} | ||
} catch { | ||
print(error.localizedDescription) | ||
completion(result, error) | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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