|
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()) |
|
|
|
|
|
def greet(name, intensity): |
|
return "Hello, " + name + "!" * int(intensity) |
|
|
|
demo = gr.Interface( |
|
fn=greet, |
|
inputs=["text", "slider"], |
|
outputs=["text"], |
|
) |
|
|
|
demo.launch() |