File size: 2,132 Bytes
766bc6c
 
 
19388ea
2f60392
 
 
9468eba
e2ccb11
9468eba
766bc6c
 
e2ccb11
 
 
 
 
 
 
 
7d2e700
 
e2ccb11
 
 
 
 
16693c3
e2ccb11
 
 
 
 
16693c3
 
 
1a22a15
2f60392
 
 
ced7be6
2f60392
 
16693c3
 
949acb0
e2ccb11
 
 
 
 
 
 
 
c91f1a6
 
 
 
 
 
 
 
 
 
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
from nostr_sdk import Client, NostrSigner, Keys, Event, UnsignedEvent, Filter, \
    HandleNotification, Timestamp, nip04_decrypt, UnwrappedGift, init_logger, LogLevel, Kind, KindEnum
import time
import gradio as gr
from gradio_client import Client as gClient

gclient = gClient("Gyr0MAN/AlbiziaChat")

keys = Keys.parse("nsec1zrl5ahr9hy43vwns0r423c0crtv45myjgelf6gf2kl8ae90x9nkqex90cg")

sk = keys.secret_key()
pk = keys.public_key()
print(f"Bot public key: {pk.to_bech32()}")

signer = NostrSigner.keys(keys)
client = Client(signer)

client.add_relay("wss://relay.damus.io")
client.add_relay("wss://relay.notoshi.win")
client.add_relay("wss://relay.siamstr.com")
client.add_relay("wss://nostr.mom")
client.add_relay("wss://relay.nostr.band")
client.connect()

now = Timestamp.now()

nip04_filter = Filter().pubkey(pk).kind(Kind.from_enum(KindEnum.ENCRYPTED_DIRECT_MESSAGE())).since(now)
client.subscribe([nip04_filter], None)


class NotificationHandler(HandleNotification):
    def handle(self, relay_url, subscription_id, event: Event):
        print(f"Received new event from {relay_url}: {event.as_json()}")
        try:
            msg = nip04_decrypt(sk, event.author(), event.content())
            print(f"Received new msg: {msg}")
            result = gclient.predict(
        		message=msg,
        		api_name="/chat"
            )
            print(result)
            
            client.send_direct_msg(event.author(), f"{result}", event.id())
        except Exception as e:
            print(f"Error during content NIP04 decryption: {e}")
            client.send_direct_msg(event.author(), f"ตอนนี้อับดุลเหนื่อยแล้ว ของีบซักแปปนึงนะครับ", event.id())

    def handle_msg(self, relay_url, msg):
        None


abortable = client.handle_notifications(NotificationHandler())
# Optionally, to abort handle notifications look, call abortable.abort()

def greet(name, intensity):
    return "Hello, " + name + "!" * int(intensity)

demo = gr.Interface(
    fn=greet,
    inputs=["text", "slider"],
    outputs=["text"],
)

demo.launch()