-
Notifications
You must be signed in to change notification settings - Fork 11
/
llama2.go
129 lines (117 loc) · 4.1 KB
/
llama2.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
package main
import (
_ "embed"
"fmt"
"github.com/go-rod/rod"
"github.com/go-rod/stealth"
"time"
)
// Set up client of Llama2 (Rod version)
var page_llama2 *rod.Page
var relogin_llama2 = true
var channel_llama2 chan string
func Llama2() {
channel_llama2 = make(chan string)
defer func() {
if err := recover(); err != nil {
relogin_llama2 = true
}
}()
//page_hc = browser.MustPage("https://huggingface.co/chat")
page_llama2 = stealth.MustPage(browser)
page_llama2.MustNavigate("https://ysharma-explore-llamav2-with-tgi.hf.space")
for i := 1; i <= 30; i++ {
if page_llama2.MustHasX("//textarea[@data-testid='textbox']") {
relogin_llama2 = false
break
}
time.Sleep(time.Second)
}
if relogin_llama2 == true {
sprint("✘ Llama2")
//page_hc.MustPDF("./tmp/HuggingChat✘.pdf")
}
if relogin_llama2 == false {
sprint("✔ Llama2")
for {
select {
case question := <-channel_llama2:
page_llama2.MustActivate()
//fmt.Println("Falcon180 received question...", question)
for i := 1; i <= 20; i++ {
if page_llama2.MustHasX("//textarea[@data-testid='textbox']") {
page_llama2.MustElementX("//textarea[@data-testid='textbox']").MustInput(question)
break
}
time.Sleep(time.Second)
}
//fmt.Println("Falcon180 input typed...")
for i := 1; i <= 20; i++ {
//if page_llama2.MustHasX("//button[contains(text(), 'Submit')]") {
//page_llama2.MustElementX("//button[contains(text(), 'Submit')]").MustClick()
if page_llama2.MustHasX("//button[@id='component-14']") {
page_llama2.MustElementX("//button[@id='component-14']").MustClick()
break
}
time.Sleep(time.Second)
}
fmt.Println("Llama2 generating...", role)
//page_falcon180.MustActivate() // Sometime three dot to hang
if role == ".all" {
channel_llama2 <- "click_llama2"
}
//// Check Error
//channel_falcon180_check := make(chan string)
//go func() {
// for i := 1; i <= 20; i++ {
// if page_falcon180.MustHasX("//*[contains(text(), 'Too much traffic, please try again')]") {
// channel_falcon180 <- "✘✘ Falcon180, Please check the internet connection and verify login status. Traffic."
// fmt.Println("Falcon180 too much traffic...")
// relogin_falcon180 = true
// close(channel_falcon180_check)
// break
// }
// time.Sleep(1 * time.Second)
// }
//}()
// stop_icon
var stop_icon_disappear = false
err := rod.Try(func() {
//page_llama2.Timeout(10 * time.Second).MustElementX("//button[contains(text(), 'Stop')]").MustWaitVisible().CancelTimeout()
page_llama2.Timeout(10 * time.Second).MustElementX("//button[@id='component-15']").MustWaitVisible().CancelTimeout()
})
//fmt.Println("find stop button")
if err == nil {
err = rod.Try(func() {
//page_llama2.Timeout(80 * time.Second).MustElementX("//button[contains(text(), 'Stop')]").MustWaitInvisible().CancelTimeout()
page_llama2.Timeout(80 * time.Second).MustElementX("//button[@id='component-15']").MustWaitInvisible().CancelTimeout()
//fmt.Println("disappear stop button")
})
if err == nil {
stop_icon_disappear = true
} else {
//fmt.Println("err in find disappear stop button", err)
}
} else {
//fmt.Println("err in find stop button", err)
}
//if page_llama2.MustHas("span:contains('Error')") {
//fmt.Println("finding Error")
if page_llama2.MustHasX("//span[contains(text(), 'Error')]"){
//fmt.Println("find Error")
channel_llama2 <- "✘✘ Llama2, Please check the internet connection and verify login status."
relogin_llama2 = true
}
if stop_icon_disappear == true {
//answer := page_llama2.MustElementX("(//div[@data-testid='bot'])[last()]").MustText()
answer := page_llama2.MustElementX("(//button[@data-testid='bot'])[last()]").MustText()
channel_llama2 <- answer
} else {
channel_llama2 <- "✘✘ Llama2, Please check the internet connection and verify login status."
relogin_llama2 = true
//page_hc.MustPDF("./tmp/HuggingChat✘.pdf")
}
}
}
}
}