-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclipboard.go
141 lines (129 loc) · 3.09 KB
/
clipboard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/google/generative-ai-go/genai"
"github.com/richardwilkes/unison"
"golang.design/x/clipboard"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
func RunClipboardWatcher(markdown *unison.Markdown) {
if err := clipboard.Init(); err != nil {
panic(err)
}
ctx := context.Background()
apiKey := os.Getenv("GEMINI_API_KEY")
if apiKey == "" {
log.Fatal("GEMINI_API_KEY is not set")
}
file, err := os.OpenFile("kotoba.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
client, err := genai.NewClient(ctx, option.WithAPIKey(apiKey))
if err != nil {
log.Fatal(err)
}
defer client.Close()
model := client.GenerativeModel("gemini-1.5-flash")
model.SafetySettings = []*genai.SafetySetting{
{
Category: genai.HarmCategoryHarassment,
Threshold: genai.HarmBlockNone,
},
{
Category: genai.HarmCategoryHateSpeech,
Threshold: genai.HarmBlockNone,
},
{
Category: genai.HarmCategorySexuallyExplicit,
Threshold: genai.HarmBlockNone,
},
{
Category: genai.HarmCategoryDangerousContent,
Threshold: genai.HarmBlockNone,
},
}
writeChan := make(chan genai.Part, 100)
ch := clipboard.Watch(ctx, clipboard.FmtText)
go writeToFile(file, writeChan)
for content := range ch {
data := invert(string(content))
timestamp := time.Now().Format("2006-01-02 15:04:05")
part := genai.Text(
fmt.Sprintf(
"\n---------------------------------\n%s\n---------------------------------\n%s\n",
timestamp,
data,
),
)
writeChan <- part
fmt.Println(part)
stream := model.GenerateContentStream(
ctx,
genai.Text(
fmt.Sprintf(
"Dịch sang tiếng Việt và giải thích ngữ pháp, viết cách đọc bằng hiragana, giải nghĩa tất cả các chữ Kanji xuất hiện trong đoạn văn kèm theo phiên âm furigana và từ Hán Nôm tương ứng:\n %s",
data,
),
),
)
for {
resp, err := stream.Next()
if err == iterator.Done {
break
}
if err != nil {
log.Fatal(err)
}
printResponse(resp, writeChan, markdown)
}
}
}
func writeToFile(file *os.File, writeChan chan genai.Part) {
for part := range writeChan {
fmt.Fprint(file, part)
}
}
func printResponse(
resp *genai.GenerateContentResponse,
writeChan chan genai.Part,
markdown *unison.Markdown,
) {
for _, cand := range resp.Candidates {
if cand.Content != nil {
for _, part := range cand.Content.Parts {
writeChan <- part
fmt.Print(part)
partText, ok := part.(genai.Text)
if !ok {
continue
}
currentText := markdown.ContentBytes()
if len(currentText)+len(partText) > 10000 {
currentText = currentText[(len(currentText) + len(partText) - 1000):]
}
currentText = append(currentText, partText...)
markdown.SetContent(string(currentText), 0)
}
}
}
}
// TODO: separate macOS and Windows
func invert(s string) string {
lines := strings.Split(s, "\n")
s = ""
for i := len(lines) - 1; i >= 0; i-- {
s += lines[i]
if i != 0 {
s += "\n"
}
}
return s
}