From dfd79bfe5928766d7cd02a1a281f321e85f27785 Mon Sep 17 00:00:00 2001 From: Believethehype <1097224+believethehype@users.noreply.github.com> Date: Tue, 2 Apr 2024 18:24:49 +0200 Subject: [PATCH] first version: dvm that provides users to block from reports of input pubkeys --- nostr_dvm/tasks/discovery_censor_wot.py | 162 ++++++++++++++++++ nostr_dvm/tasks/summarization_huggingchat.py | 5 +- .../tasks/summarization_unleashed_chat.py | 3 +- tests/censor.py | 46 +++++ tests/test_dvm_client.py | 57 ++++-- 5 files changed, 257 insertions(+), 16 deletions(-) create mode 100644 nostr_dvm/tasks/discovery_censor_wot.py create mode 100644 tests/censor.py diff --git a/nostr_dvm/tasks/discovery_censor_wot.py b/nostr_dvm/tasks/discovery_censor_wot.py new file mode 100644 index 0000000..f24301f --- /dev/null +++ b/nostr_dvm/tasks/discovery_censor_wot.py @@ -0,0 +1,162 @@ +import json +import os +from datetime import timedelta +from threading import Thread + +from nostr_sdk import Client, Timestamp, PublicKey, Tag, Keys, Options, SecretKey, NostrSigner, Kind + +from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv +from nostr_dvm.utils.admin_utils import AdminConfig +from nostr_dvm.utils.definitions import EventDefinitions +from nostr_dvm.utils.dvmconfig import DVMConfig, build_default_config +from nostr_dvm.utils.nip88_utils import NIP88Config +from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag +from nostr_dvm.utils.output_utils import post_process_list_to_users + +""" +This File contains a Module to find inactive follows for a user on nostr + +Accepted Inputs: None needed +Outputs: A list of users that have been inactive +Params: None +""" + + +class DiscoverNonFollowers(DVMTaskInterface): + KIND: Kind = EventDefinitions.KIND_NIP90_PEOPLE_DISCOVERY + TASK: str = "people to block" + FIX_COST: float = 0 + client: Client + dvm_config: DVMConfig + + def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config, nip88config: NIP88Config = None, + admin_config: AdminConfig = None, options=None): + dvm_config.SCRIPT = os.path.abspath(__file__) + super().__init__(name=name, dvm_config=dvm_config, nip89config=nip89config, nip88config=nip88config, + admin_config=admin_config, options=options) + + def is_input_supported(self, tags, client=None, dvm_config=None): + return True + + def create_request_from_nostr_event(self, event, client=None, dvm_config=None): + self.dvm_config = dvm_config + + request_form = {"jobID": event.id().to_hex()} + + # default values + users = [] + # users.append(event.author().to_hex()) + + for tag in event.tags(): + if tag.as_vec()[0] == 'i': + users.append(tag.as_vec()[1]) + + options = { + "users": users, + } + request_form['options'] = json.dumps(options) + return request_form + + def process(self, request_form): + from nostr_sdk import Filter + from types import SimpleNamespace + ns = SimpleNamespace() + + opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))) + sk = SecretKey.from_hex(self.dvm_config.PRIVATE_KEY) + keys = Keys.parse(sk.to_hex()) + signer = NostrSigner.keys(keys) + cli = Client.with_opts(signer, opts) + # cli.add_relay("wss://relay.nostr.band") + for relay in self.dvm_config.RELAY_LIST: + cli.add_relay(relay) + cli.connect() + + options = DVMTaskInterface.set_options(request_form) + step = 20 + + pubkeys = [] + for user in options["users"]: + pubkeys.append(PublicKey.parse(user)) + + kind1984_filter = Filter().authors(pubkeys).kind(Kind(1984)) + reports = cli.get_events_of([kind1984_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) + + bad_actors = [] + ns.dic = {} + reasons = ["spam", "illegal", "impersonation"] + # init + for report in reports: + for tag in report.tags(): + if tag.as_vec()[0] == "p": + ns.dic[tag.as_vec()[1]] = 0 + + for report in reports: + print(report.as_json()) + for tag in report.tags(): + if tag.as_vec()[0] == "p": + if len(tag.as_vec()) > 2 and tag.as_vec()[2] in reasons or len(tag.as_vec()) <= 2: + ns.dic[tag.as_vec()[1]] += 1 + + + #result = {k for (k, v) in ns.dic.items() if v > 0} + #result = sorted(ns.dic.items(), key=lambda x: x[1], reverse=True) + finallist_sorted = sorted(ns.dic.items(), key=lambda x: x[1], reverse=True) + converted_dict = dict(finallist_sorted) + print(json.dumps(converted_dict)) + for k in converted_dict: + p_tag = Tag.parse(["p", k]) + bad_actors.append(p_tag.as_vec()) + + print(json.dumps(bad_actors)) + return json.dumps(bad_actors) + + def post_process(self, result, event): + """Overwrite the interface function to return a social client readable format, if requested""" + for tag in event.tags(): + if tag.as_vec()[0] == 'output': + format = tag.as_vec()[1] + if format == "text/plain": # check for output type + result = post_process_list_to_users(result) + + # if not text/plain, don't post-process + return result + + +# We build an example here that we can call by either calling this file directly from the main directory, +# or by adding it to our playground. You can call the example and adjust it to your needs or redefine it in the +# playground or elsewhere +def build_example(name, identifier, admin_config): + dvm_config = build_default_config(identifier) + dvm_config.USE_OWN_VENV = False + admin_config.LUD16 = dvm_config.LN_ADDRESS + # Add NIP89 + nip89info = { + "name": name, + "image": "https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg", + "about": "I discover users you follow, but that don't follow you back.", + "encryptionSupported": True, + "cashuAccepted": True, + "nip90Params": { + "user": { + "required": False, + "values": [], + "description": "Do the task for another user" + }, + "since_days": { + "required": False, + "values": [], + "description": "The number of days a user has not been active to be considered inactive" + } + } + } + nip89config = NIP89Config() + nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"]) + nip89config.CONTENT = json.dumps(nip89info) + + return DiscoverNonFollowers(name=name, dvm_config=dvm_config, nip89config=nip89config, + admin_config=admin_config) + + +if __name__ == '__main__': + process_venv(DiscoverNonFollowers) diff --git a/nostr_dvm/tasks/summarization_huggingchat.py b/nostr_dvm/tasks/summarization_huggingchat.py index dae742e..3a81c47 100644 --- a/nostr_dvm/tasks/summarization_huggingchat.py +++ b/nostr_dvm/tasks/summarization_huggingchat.py @@ -1,5 +1,6 @@ import json import os +import re from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface, process_venv from nostr_dvm.utils.admin_utils import AdminConfig @@ -106,7 +107,9 @@ class TextSummarizationHuggingChat(DVMTaskInterface): try: chatbot = hugchat.ChatBot(cookies=cookies.get_dict()) # or cookie_path="usercookies/.json" - query_result = chatbot.query("Summarize the following notes: " + options["prompt"]) + text = re.sub(r'^https?:\/\/.*[\r\n]*', '', str(options["prompt"]), flags=re.MULTILINE) + + query_result = chatbot.query("Summarize the following notes: " + text[:3500]) print(query_result["text"]) # or query_result.text or query_result["text"] return str(query_result["text"]).lstrip() diff --git a/nostr_dvm/tasks/summarization_unleashed_chat.py b/nostr_dvm/tasks/summarization_unleashed_chat.py index 2beb0d0..66247f5 100644 --- a/nostr_dvm/tasks/summarization_unleashed_chat.py +++ b/nostr_dvm/tasks/summarization_unleashed_chat.py @@ -108,8 +108,9 @@ class SummarizationUnleashedChat(DVMTaskInterface): for model in client.models.list(): print('- ' + model.id) + text = re.sub(r'^https?:\/\/.*[\r\n]*', '', str(options["prompt"]), flags=re.MULTILINE) - content = "Summarize the following notes: " + str(options["prompt"])[:3500] + content = "Summarize the following notes: " + text[:3500] normal_stream = client.chat.completions.create( messages=[ { diff --git a/tests/censor.py b/tests/censor.py new file mode 100644 index 0000000..7f7c18e --- /dev/null +++ b/tests/censor.py @@ -0,0 +1,46 @@ +import os +import threading +from pathlib import Path + +import dotenv +from nostr_sdk import Keys + +from nostr_dvm.subscription import Subscription +from nostr_dvm.tasks import content_discovery_currently_popular, discovery_censor_wot +from nostr_dvm.utils.admin_utils import AdminConfig +from nostr_dvm.utils.backend_utils import keep_alive +from nostr_dvm.utils.dvmconfig import DVMConfig +from nostr_dvm.utils.nostr_utils import check_and_set_private_key +from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys + + +def playground(): + # Generate an optional Admin Config, in this case, whenever we give our DVMs this config, they will (re)broadcast + # their NIP89 announcement + # You can create individual admins configs and hand them over when initializing the dvm, + # for example to whilelist users or add to their balance. + # If you use this global config, options will be set for all dvms that use it. + admin_config = AdminConfig() + admin_config.REBROADCAST_NIP89 = False + admin_config.UPDATE_PROFILE = False + + discovery_test_sub = discovery_censor_wot.build_example("Censorship", "discovery_censor", admin_config) + discovery_test_sub.run() + + + + #keep_alive() + + +if __name__ == '__main__': + env_path = Path('.env') + if not env_path.is_file(): + with open('.env', 'w') as f: + print("Writing new .env file") + f.write('') + if env_path.is_file(): + print(f'loading environment from {env_path.resolve()}') + dotenv.load_dotenv(env_path, verbose=True, override=True) + else: + raise FileNotFoundError(f'.env file not found at {env_path} ') + playground() diff --git a/tests/test_dvm_client.py b/tests/test_dvm_client.py index c1e8e4e..7f7eaa7 100644 --- a/tests/test_dvm_client.py +++ b/tests/test_dvm_client.py @@ -5,7 +5,7 @@ from threading import Thread import dotenv from nostr_sdk import Keys, Client, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt, \ - nip04_encrypt, NostrSigner + nip04_encrypt, NostrSigner, PublicKey, Event from nostr_dvm.utils.dvmconfig import DVMConfig from nostr_dvm.utils.nostr_utils import send_event, check_and_set_private_key @@ -40,13 +40,13 @@ def nostr_client_test_translation(input, kind, lang, sats, satsmax): config = DVMConfig send_event(event, client=client, dvm_config=config) return event.as_json() + + def nostr_client_test_search_profile(input): keys = Keys.parse(check_and_set_private_key("test_client")) iTag = Tag.parse(["i", input, "text"]) - - relaysTag = Tag.parse(['relays', "wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org", "wss://nostr-pub.wellorder.net"]) alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to translate a given Input"]) @@ -66,6 +66,7 @@ def nostr_client_test_search_profile(input): send_event(event, client=client, dvm_config=config) return event.as_json() + def nostr_client_test_image(prompt): keys = Keys.parse(check_and_set_private_key("test_client")) @@ -94,6 +95,33 @@ def nostr_client_test_image(prompt): return event.as_json() +def nostr_client_test_censor_filter(users): + keys = Keys.parse(check_and_set_private_key("test_client")) + + relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org", + ] + + relaysTag = Tag.parse(relay_list) + alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to find people to ignore based on people the user trusts"]) + # pTag = Tag.parse(["p", user, "text"]) + tags = [relaysTag, alttag] + for user in users: + iTag = Tag.parse(["i", user, "text"]) + tags.append(iTag) + + event = EventBuilder(EventDefinitions.KIND_NIP90_PEOPLE_DISCOVERY, str("Give me bad actors"), + tags).to_event(keys) + + signer = NostrSigner.keys(keys) + client = Client(signer) + for relay in relay_list: + client.add_relay(relay) + client.connect() + config = DVMConfig + send_event(event, client=client, dvm_config=config) + return event.as_json() + + def nostr_client_test_tts(prompt): keys = Keys.parse(check_and_set_private_key("test_client")) @@ -108,7 +136,7 @@ def nostr_client_test_tts(prompt): [iTag, paramTag1, bidTag, relaysTag, alttag]).to_event(keys) relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org", - "wss://nostr-pub.wellorder.net"] + ] signer = NostrSigner.keys(keys) client = Client(signer) @@ -124,8 +152,6 @@ def nostr_client_test_image_private(prompt, cashutoken): keys = Keys.parse(check_and_set_private_key("test_client")) receiver_keys = Keys.parse(check_and_set_private_key("replicate_sdxl")) - # TODO more advanced logic, more parsing, params etc, just very basic test functions for now - relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org", "wss://nostr-pub.wellorder.net"] i_tag = Tag.parse(["i", prompt, "text"]) @@ -177,31 +203,34 @@ def nostr_client(): Timestamp.now()) # events to us specific dvm_filter = (Filter().kinds([EventDefinitions.KIND_NIP90_RESULT_TRANSLATE_TEXT, EventDefinitions.KIND_FEEDBACK]).since(Timestamp.now())) # public events - client.subscribe([dm_zap_filter, dvm_filter]) + client.subscribe([dm_zap_filter, dvm_filter], None) # nostr_client_test_translation("This is the result of the DVM in spanish", "text", "es", 20, 20) # nostr_client_test_translation("note1p8cx2dz5ss5gnk7c59zjydcncx6a754c0hsyakjvnw8xwlm5hymsnc23rs", "event", "es", 20,20) # nostr_client_test_translation("44a0a8b395ade39d46b9d20038b3f0c8a11168e67c442e3ece95e4a1703e2beb", "event", "zh", 20, 20) - #nostr_client_test_image("a beautiful purple ostrich watching the sunset") - nostr_client_test_search_profile("dontbelievew") + # nostr_client_test_image("a beautiful purple ostrich watching the sunset") + # nostr_client_test_search_profile("dontbelieve") + wot = ["99bb5591c9116600f845107d31f9b59e2f7c7e09a1ff802e84f1d43da557ca64"] + nostr_client_test_censor_filter(wot) + # nostr_client_test_tts("Hello, this is a test. Mic check one, two.") # cashutoken = "cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6MSwiQyI6IjAyNWU3ODZhOGFkMmExYTg0N2YxMzNiNGRhM2VhMGIyYWRhZGFkOTRiYzA4M2E2NWJjYjFlOTgwYTE1NGIyMDA2NCIsInNlY3JldCI6InQ1WnphMTZKMGY4UElQZ2FKTEg4V3pPck5rUjhESWhGa291LzVzZFd4S0U9In0seyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6NCwiQyI6IjAyOTQxNmZmMTY2MzU5ZWY5ZDc3MDc2MGNjZmY0YzliNTMzMzVmZTA2ZGI5YjBiZDg2Njg5Y2ZiZTIzMjVhYWUwYiIsInNlY3JldCI6IlRPNHB5WE43WlZqaFRQbnBkQ1BldWhncm44UHdUdE5WRUNYWk9MTzZtQXM9In0seyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6MTYsIkMiOiIwMmRiZTA3ZjgwYmMzNzE0N2YyMDJkNTZiMGI3ZTIzZTdiNWNkYTBhNmI3Yjg3NDExZWYyOGRiZDg2NjAzNzBlMWIiLCJzZWNyZXQiOiJHYUNIdHhzeG9HM3J2WWNCc0N3V0YxbU1NVXczK0dDN1RKRnVwOHg1cURzPSJ9XSwibWludCI6Imh0dHBzOi8vbG5iaXRzLmJpdGNvaW5maXhlc3RoaXMub3JnL2Nhc2h1L2FwaS92MS9ScDlXZGdKZjlxck51a3M1eVQ2SG5rIn1dfQ==" # nostr_client_test_image_private("a beautiful ostrich watching the sunset") class NotificationHandler(HandleNotification): - def handle(self, relay_url, event): + def handle(self, relay_url, subscription_id, event: Event): print(f"Received new event from {relay_url}: {event.as_json()}") - if event.kind() == 7000: + if event.kind().as_u64() == 7000: print("[Nostr Client]: " + event.as_json()) - elif 6000 < event.kind() < 6999: + elif 6000 < event.kind().as_u64() < 6999: print("[Nostr Client]: " + event.as_json()) print("[Nostr Client]: " + event.content()) - elif event.kind() == 4: + elif event.kind().as_u64() == 4: dec_text = nip04_decrypt(sk, event.author(), event.content()) print("[Nostr Client]: " + f"Received new msg: {dec_text}") - elif event.kind() == 9735: + elif event.kind().as_u64() == 9735: print("[Nostr Client]: " + f"Received new zap:") print(event.as_json())