mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-06-17 01:20:47 +02:00
refcactor
This commit is contained in:
parent
659067551b
commit
1a5b4bf024
92
dvm.py
92
dvm.py
@ -30,7 +30,7 @@ def dvm(config):
|
|||||||
pk = keys.public_key()
|
pk = keys.public_key()
|
||||||
|
|
||||||
print(f"Nostr DVM public key: {pk.to_bech32()}, Hex: {pk.to_hex()} ")
|
print(f"Nostr DVM public key: {pk.to_bech32()}, Hex: {pk.to_hex()} ")
|
||||||
print(f"Supported DVM tasks: {dvm_config.SUPPORTED_TASKS}")
|
print('Supported DVM tasks: ' + ', '.join(p.TASK for p in dvm_config.SUPPORTED_TASKS))
|
||||||
|
|
||||||
client = Client(keys)
|
client = Client(keys)
|
||||||
for relay in dvm_config.RELAY_LIST:
|
for relay in dvm_config.RELAY_LIST:
|
||||||
@ -38,10 +38,12 @@ def dvm(config):
|
|||||||
client.connect()
|
client.connect()
|
||||||
|
|
||||||
dm_zap_filter = Filter().pubkey(pk).kinds([EventDefinitions.KIND_ZAP]).since(Timestamp.now())
|
dm_zap_filter = Filter().pubkey(pk).kinds([EventDefinitions.KIND_ZAP]).since(Timestamp.now())
|
||||||
dvm_filter = (Filter().kinds([EventDefinitions.KIND_NIP90_GENERIC,
|
|
||||||
EventDefinitions.KIND_NIP90_EXTRACT_TEXT,
|
kinds = [EventDefinitions.KIND_NIP90_GENERIC]
|
||||||
EventDefinitions.KIND_NIP90_TRANSLATE_TEXT,
|
for dvm in dvm_config.SUPPORTED_TASKS:
|
||||||
]).since(Timestamp.now()))
|
if dvm.KIND not in kinds:
|
||||||
|
kinds.append(dvm.KIND)
|
||||||
|
dvm_filter = (Filter().kinds(kinds).since(Timestamp.now()))
|
||||||
client.subscribe([dm_zap_filter, dvm_filter])
|
client.subscribe([dm_zap_filter, dvm_filter])
|
||||||
|
|
||||||
create_sql_table()
|
create_sql_table()
|
||||||
@ -53,7 +55,7 @@ def dvm(config):
|
|||||||
print(f"[Nostr] Received new NIP90 Job Request from {relay_url}: {nostr_event.as_json()}")
|
print(f"[Nostr] Received new NIP90 Job Request from {relay_url}: {nostr_event.as_json()}")
|
||||||
handle_nip90_job_event(nostr_event, dvm_config)
|
handle_nip90_job_event(nostr_event, dvm_config)
|
||||||
elif nostr_event.kind() == EventDefinitions.KIND_ZAP:
|
elif nostr_event.kind() == EventDefinitions.KIND_ZAP:
|
||||||
handle_zap(nostr_event)
|
handle_zap(nostr_event, dvm_config)
|
||||||
|
|
||||||
def handle_msg(self, relay_url, msg):
|
def handle_msg(self, relay_url, msg):
|
||||||
return
|
return
|
||||||
@ -75,12 +77,17 @@ def dvm(config):
|
|||||||
elif task_supported:
|
elif task_supported:
|
||||||
print("Received new Task: " + task)
|
print("Received new Task: " + task)
|
||||||
print(duration)
|
print(duration)
|
||||||
amount = get_amount_per_task(task, duration, config=dvm_config)
|
amount = get_amount_per_task(task, dvm_config, duration)
|
||||||
if amount is None:
|
if amount is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
if is_whitelisted:
|
task_is_free = False
|
||||||
print("[Nostr] Whitelisted for task " + task + ". Starting processing..")
|
for dvm in dvm_config.SUPPORTED_TASKS:
|
||||||
|
if dvm.TASK == task and dvm.COST == 0:
|
||||||
|
task_is_free = True
|
||||||
|
|
||||||
|
if is_whitelisted or task_is_free:
|
||||||
|
print("[Nostr] Free or Whitelisted for task " + task + ". Starting processing..")
|
||||||
send_job_status_reaction(event, "processing", True, 0, client=client, config=dvm_config)
|
send_job_status_reaction(event, "processing", True, 0, client=client, config=dvm_config)
|
||||||
do_work(event, is_from_bot=False)
|
do_work(event, is_from_bot=False)
|
||||||
# otherwise send payment request
|
# otherwise send payment request
|
||||||
@ -94,9 +101,9 @@ def dvm(config):
|
|||||||
if bid > 0:
|
if bid > 0:
|
||||||
bid_offer = int(bid / 1000)
|
bid_offer = int(bid / 1000)
|
||||||
if bid_offer >= amount:
|
if bid_offer >= amount:
|
||||||
send_job_status_reaction(event, "payment-required", False,
|
send_job_status_reaction(event, "payment-required", False,
|
||||||
amount, # bid_offer
|
amount, # bid_offer
|
||||||
client=client, config=dvm_config)
|
client=client, config=dvm_config)
|
||||||
|
|
||||||
else: # If there is no bid, just request server rate from user
|
else: # If there is no bid, just request server rate from user
|
||||||
print("[Nostr] Requesting payment for Event: " + event.id().to_hex())
|
print("[Nostr] Requesting payment for Event: " + event.id().to_hex())
|
||||||
@ -105,7 +112,7 @@ def dvm(config):
|
|||||||
else:
|
else:
|
||||||
print("Task not supported on this DVM, skipping..")
|
print("Task not supported on this DVM, skipping..")
|
||||||
|
|
||||||
def handle_zap(event):
|
def handle_zap(event, dvm_config):
|
||||||
zapped_event = None
|
zapped_event = None
|
||||||
invoice_amount = 0
|
invoice_amount = 0
|
||||||
anon = False
|
anon = False
|
||||||
@ -205,38 +212,17 @@ def dvm(config):
|
|||||||
or job_event.kind() == EventDefinitions.KIND_DM):
|
or job_event.kind() == EventDefinitions.KIND_DM):
|
||||||
|
|
||||||
task = get_task(job_event, client=client, dvmconfig=dvm_config)
|
task = get_task(job_event, client=client, dvmconfig=dvm_config)
|
||||||
result = ""
|
for dvm in dvm_config.SUPPORTED_TASKS:
|
||||||
try:
|
try:
|
||||||
if task == Translation.TASK:
|
if task == dvm.TASK:
|
||||||
request_form = Translation.create_requestform_from_nostr_event(job_event,client,dvm_config)
|
request_form = dvm.create_request_form_from_nostr_event(job_event, client, dvm_config)
|
||||||
options = setOptions(request_form)
|
result = dvm.process(request_form)
|
||||||
result = Translation.process(options)
|
check_and_return_event(result, str(job_event.as_json()), dvm_key=dvm_config.PRIVATE_KEY)
|
||||||
|
|
||||||
elif task == TextExtractionPDF.TASK:
|
except Exception as e:
|
||||||
request_form = TextExtractionPDF.create_requestform_from_nostr_event(job_event, client, dvm_config)
|
respond_to_error(e, job_event.as_json(), is_from_bot, dvm_config.PRIVATE_KEY)
|
||||||
options = setOptions(request_form)
|
|
||||||
result = TextExtractionPDF.process(options)
|
|
||||||
|
|
||||||
#TODO Add more tasks here
|
def check_event_has_not_unfinished_job_input(nevent, append, client, dvmconfig):
|
||||||
|
|
||||||
check_and_return_event(result, str(job_event.as_json()), dvm_key=dvm_config.PRIVATE_KEY)
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
respond_to_error(e, job_event.as_json(), is_from_bot, dvm_config.PRIVATE_KEY)
|
|
||||||
|
|
||||||
|
|
||||||
def setOptions(request_form):
|
|
||||||
print("Setting options...")
|
|
||||||
opts = []
|
|
||||||
if request_form.get("optStr"):
|
|
||||||
for k, v in [option.split("=") for option in request_form["optStr"].split(";")]:
|
|
||||||
t = (k, v)
|
|
||||||
opts.append(t)
|
|
||||||
print(k + "=" + v)
|
|
||||||
print("...done.")
|
|
||||||
return dict(opts)
|
|
||||||
|
|
||||||
def check_event_has_not_unifinished_job_input(nevent, append, client, dvmconfig):
|
|
||||||
task_supported, task, duration = check_task_is_supported(nevent, client, False, config=dvmconfig)
|
task_supported, task, duration = check_task_is_supported(nevent, client, False, config=dvmconfig)
|
||||||
if not task_supported:
|
if not task_supported:
|
||||||
return False
|
return False
|
||||||
@ -400,6 +386,7 @@ def dvm(config):
|
|||||||
send_event(event, key=key)
|
send_event(event, key=key)
|
||||||
print("[Nostr] " + str(response_kind) + " Job Response event sent: " + event.as_json())
|
print("[Nostr] " + str(response_kind) + " Job Response event sent: " + event.as_json())
|
||||||
return event.as_json()
|
return event.as_json()
|
||||||
|
|
||||||
client.handle_notifications(NotificationHandler())
|
client.handle_notifications(NotificationHandler())
|
||||||
|
|
||||||
def respond_to_error(content, originaleventstr, is_from_bot=False, dvm_key=None):
|
def respond_to_error(content, originaleventstr, is_from_bot=False, dvm_key=None):
|
||||||
@ -425,7 +412,7 @@ def dvm(config):
|
|||||||
user = get_from_sql_table(sender)
|
user = get_from_sql_table(sender)
|
||||||
is_whitelisted = user[2]
|
is_whitelisted = user[2]
|
||||||
if not is_whitelisted:
|
if not is_whitelisted:
|
||||||
amount = int(user[1]) + get_amount_per_task(task)
|
amount = int(user[1]) + get_amount_per_task(task, dvm_config)
|
||||||
update_sql_table(sender, amount, user[2], user[3], user[4], user[5], user[6],
|
update_sql_table(sender, amount, user[2], user[3], user[4], user[5], user[6],
|
||||||
Timestamp.now().as_secs())
|
Timestamp.now().as_secs())
|
||||||
message = "There was the following error : " + content + ". Credits have been reimbursed"
|
message = "There was the following error : " + content + ". Credits have been reimbursed"
|
||||||
@ -454,7 +441,7 @@ def dvm(config):
|
|||||||
job_list.remove(job)
|
job_list.remove(job)
|
||||||
|
|
||||||
for job in jobs_on_hold_list:
|
for job in jobs_on_hold_list:
|
||||||
if check_event_has_not_unifinished_job_input(job.event, False, client=client, dvmconfig=dvm_config):
|
if check_event_has_not_unfinished_job_input(job.event, False, client=client, dvmconfig=dvm_config):
|
||||||
handle_nip90_job_event(job.event)
|
handle_nip90_job_event(job.event)
|
||||||
jobs_on_hold_list.remove(job)
|
jobs_on_hold_list.remove(job)
|
||||||
|
|
||||||
@ -462,18 +449,3 @@ def dvm(config):
|
|||||||
jobs_on_hold_list.remove(job)
|
jobs_on_hold_list.remove(job)
|
||||||
|
|
||||||
time.sleep(1.0)
|
time.sleep(1.0)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
32
interfaces/dvmtaskinterface.py
Normal file
32
interfaces/dvmtaskinterface.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
class DVMTaskInterface:
|
||||||
|
TASK: str
|
||||||
|
KIND: int
|
||||||
|
COST: int
|
||||||
|
|
||||||
|
def NIP89_announcement(self):
|
||||||
|
"""Define the NIP89 Announcement"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def is_input_supported(self, input_type, input_content) -> bool:
|
||||||
|
"""Check if input is supported for current Task."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def create_request_form_from_nostr_event(self, event, client=None, dvm_config=None) -> dict:
|
||||||
|
"""Parse input into a request form that will be given to the process method"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
def process(self, request_form):
|
||||||
|
"Process the data and return the result"
|
||||||
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def setOptions(request_form):
|
||||||
|
print("Setting options...")
|
||||||
|
opts = []
|
||||||
|
if request_form.get("optStr"):
|
||||||
|
for k, v in [option.split("=") for option in request_form["optStr"].split(";")]:
|
||||||
|
t = (k, v)
|
||||||
|
opts.append(t)
|
||||||
|
print(k + "=" + v)
|
||||||
|
print("...done.")
|
||||||
|
return dict(opts)
|
9
main.py
9
main.py
@ -12,16 +12,19 @@ from utils.definitions import EventDefinitions
|
|||||||
def run_nostr_dvm_with_local_config():
|
def run_nostr_dvm_with_local_config():
|
||||||
from dvm import dvm, DVMConfig
|
from dvm import dvm, DVMConfig
|
||||||
|
|
||||||
|
PDFextactor = TextExtractionPDF("PDF Extractor", env.NOSTR_PRIVATE_KEY)
|
||||||
|
Translator = Translation("Translator", env.NOSTR_PRIVATE_KEY)
|
||||||
|
|
||||||
dvmconfig = DVMConfig()
|
dvmconfig = DVMConfig()
|
||||||
dvmconfig.PRIVATE_KEY = os.getenv(env.NOSTR_PRIVATE_KEY)
|
dvmconfig.PRIVATE_KEY = os.getenv(env.NOSTR_PRIVATE_KEY)
|
||||||
dvmconfig.SUPPORTED_TASKS = [Translation.TASK, TextExtractionPDF.TASK]
|
dvmconfig.SUPPORTED_TASKS = [PDFextactor, Translator]
|
||||||
dvmconfig.LNBITS_INVOICE_KEY = os.getenv(env.LNBITS_INVOICE_KEY)
|
dvmconfig.LNBITS_INVOICE_KEY = os.getenv(env.LNBITS_INVOICE_KEY)
|
||||||
dvmconfig.LNBITS_URL = os.getenv(env.LNBITS_HOST)
|
dvmconfig.LNBITS_URL = os.getenv(env.LNBITS_HOST)
|
||||||
|
|
||||||
# In admin_utils, set rebroadcast_nip89 to true to (re)broadcast your DVM. You can create a valid dtag and the content on vendata.io
|
# In admin_utils, set rebroadcast_nip89 to true to (re)broadcast your DVM. You can create a valid dtag and the content on vendata.io
|
||||||
# Add the dtag in your .env file so you can update your dvm later and change the content in the module file as needed.
|
# Add the dtag in your .env file so you can update your dvm later and change the content in the module file as needed.
|
||||||
dvmconfig.NIP89s.append(TextExtractionPDF.NIP89_announcement())
|
dvmconfig.NIP89s.append(PDFextactor.NIP89_announcement())
|
||||||
dvmconfig.NIP89s.append(Translation.NIP89_announcement())
|
dvmconfig.NIP89s.append(Translator.NIP89_announcement())
|
||||||
|
|
||||||
nostr_dvm_thread = Thread(target=dvm, args=[dvmconfig])
|
nostr_dvm_thread = Thread(target=dvm, args=[dvmconfig])
|
||||||
nostr_dvm_thread.start()
|
nostr_dvm_thread.start()
|
||||||
|
@ -1,34 +1,38 @@
|
|||||||
import os
|
import os
|
||||||
from typing import re
|
import re
|
||||||
|
|
||||||
|
from interfaces.dvmtaskinterface import DVMTaskInterface
|
||||||
from utils import env
|
from utils import env
|
||||||
from utils.definitions import EventDefinitions
|
from utils.definitions import EventDefinitions
|
||||||
from utils.nip89_utils import NIP89Announcement
|
from utils.nip89_utils import NIP89Announcement
|
||||||
from utils.nostr_utils import get_event_by_id, get_referenced_event_by_id
|
from utils.nostr_utils import get_event_by_id, get_referenced_event_by_id
|
||||||
|
|
||||||
|
|
||||||
class TextExtractionPDF:
|
class TextExtractionPDF(DVMTaskInterface):
|
||||||
TASK: str = "pdf-to-text"
|
TASK: str = "pdf-to-text"
|
||||||
|
KIND: int = EventDefinitions.KIND_NIP90_EXTRACT_TEXT
|
||||||
COST: int = 20
|
COST: int = 20
|
||||||
|
|
||||||
@staticmethod
|
def __init__(self, name, pk):
|
||||||
def NIP89_announcement():
|
self.NAME = name
|
||||||
|
self.PK = pk
|
||||||
|
|
||||||
|
def NIP89_announcement(self):
|
||||||
nip89 = NIP89Announcement()
|
nip89 = NIP89Announcement()
|
||||||
nip89.kind = EventDefinitions.KIND_NIP90_TRANSLATE_TEXT
|
nip89.kind = self.KIND
|
||||||
nip89.dtag = os.getenv(env.TASK_TRANSLATION_NIP89_DTAG)
|
nip89.pk = self.PK
|
||||||
nip89.pk = os.getenv(env.NOSTR_PRIVATE_KEY)
|
nip89.dtag = os.getenv(env.TASK_TEXTEXTRACTION_NIP89_DTAG)
|
||||||
nip89.content = "{\"name\":\"Translator\",\"image\":\"https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg\",\"about\":\"I translate text from given text/event/job, currently using Google Translation Services into language defined in param. \",\"nip90Params\":{\"language\":{\"required\":true,\"values\":[\"af\",\"am\",\"ar\",\"az\",\"be\",\"bg\",\"bn\",\"bs\",\"ca\",\"ceb\",\"co\",\"cs\",\"cy\",\"da\",\"de\",\"el\",\"eo\",\"es\",\"et\",\"eu\",\"fa\",\"fi\",\"fr\",\"fy\",\"ga\",\"gd\",\"gl\",\"gu\",\"ha\",\"haw\",\"hi\",\"hmn\",\"hr\",\"ht\",\"hu\",\"hy\",\"id\",\"ig\",\"is\",\"it\",\"he\",\"ja\",\"jv\",\"ka\",\"kk\",\"km\",\"kn\",\"ko\",\"ku\",\"ky\",\"la\",\"lb\",\"lo\",\"lt\",\"lv\",\"mg\",\"mi\",\"mk\",\"ml\",\"mn\",\"mr\",\"ms\",\"mt\",\"my\",\"ne\",\"nl\",\"no\",\"ny\",\"or\",\"pa\",\"pl\",\"ps\",\"pt\",\"ro\",\"ru\",\"sd\",\"si\",\"sk\",\"sl\",\"sm\",\"sn\",\"so\",\"sq\",\"sr\",\"st\",\"su\",\"sv\",\"sw\",\"ta\",\"te\",\"tg\",\"th\",\"tl\",\"tr\",\"ug\",\"uk\",\"ur\",\"uz\",\"vi\",\"xh\",\"yi\",\"yo\",\"zh\",\"zu\"]}}}"
|
nip89.content = "{\"name\":\"" + self.NAME + "\",\"image\":\"https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg\",\"about\":\"I extract Text from pdf documents\",\"nip90Params\":{}}"
|
||||||
return nip89
|
return nip89
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
def is_input_supported(self, input_type, input_content):
|
||||||
def is_input_supported(input_type, input_content):
|
|
||||||
if input_type != "url":
|
if input_type != "url":
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def create_requestform_from_nostr_event(event, client=None, dvmconfig=None):
|
def create_request_form_from_nostr_event(self, event, client=None, dvm_config=None):
|
||||||
request_form = {"jobID": event.id().to_hex()}
|
request_form = {"jobID": event.id().to_hex()}
|
||||||
|
|
||||||
# default values
|
# default values
|
||||||
@ -44,31 +48,35 @@ class TextExtractionPDF:
|
|||||||
if input_type == "url":
|
if input_type == "url":
|
||||||
url = input_content
|
url = input_content
|
||||||
elif input_type == "event":
|
elif input_type == "event":
|
||||||
evt = get_event_by_id(input_content, config=dvmconfig)
|
evt = get_event_by_id(input_content, config=dvm_config)
|
||||||
url = re.search("(?P<url>https?://[^\s]+)", evt.content()).group("url")
|
url = re.search("(?P<url>https?://[^\s]+)", evt.content()).group("url")
|
||||||
elif input_type == "job":
|
elif input_type == "job":
|
||||||
evt = get_referenced_event_by_id(input_content, [EventDefinitions.KIND_NIP90_RESULT_GENERATE_IMAGE],
|
evt = get_referenced_event_by_id(input_content, [EventDefinitions.KIND_NIP90_RESULT_GENERATE_IMAGE],
|
||||||
client, config=dvmconfig)
|
client, config=dvm_config)
|
||||||
|
|
||||||
url = re.search("(?P<url>https?://[^\s]+)", evt.content()).group("url")
|
url = re.search("(?P<url>https?://[^\s]+)", evt.content()).group("url")
|
||||||
|
|
||||||
request_form["optStr"] = 'url=' + url
|
request_form["optStr"] = 'url=' + url
|
||||||
return request_form
|
return request_form
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def process(options):
|
def process(self, request_form):
|
||||||
|
options = DVMTaskInterface.setOptions(request_form)
|
||||||
from pypdf import PdfReader
|
from pypdf import PdfReader
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import requests
|
import requests
|
||||||
file_path = Path('temp.pdf')
|
try:
|
||||||
response = requests.get(options["url"])
|
file_path = Path('temp.pdf')
|
||||||
file_path.write_bytes(response.content)
|
response = requests.get(options["url"])
|
||||||
reader = PdfReader(file_path)
|
file_path.write_bytes(response.content)
|
||||||
number_of_pages = len(reader.pages)
|
reader = PdfReader(file_path)
|
||||||
text = ""
|
number_of_pages = len(reader.pages)
|
||||||
for page_num in range(number_of_pages):
|
text = ""
|
||||||
page = reader.pages[page_num]
|
for page_num in range(number_of_pages):
|
||||||
text = text + page.extract_text()
|
page = reader.pages[page_num]
|
||||||
|
text = text + page.extract_text()
|
||||||
|
|
||||||
os.remove('temp.pdf')
|
os.remove('temp.pdf')
|
||||||
return text
|
return text
|
||||||
|
except Exception as e:
|
||||||
|
raise Exception(e)
|
||||||
|
@ -1,35 +1,37 @@
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
|
from interfaces.dvmtaskinterface import DVMTaskInterface
|
||||||
from utils import env
|
from utils import env
|
||||||
from utils.definitions import EventDefinitions
|
from utils.definitions import EventDefinitions
|
||||||
from utils.nip89_utils import NIP89Announcement
|
from utils.nip89_utils import NIP89Announcement
|
||||||
from utils.nostr_utils import get_referenced_event_by_id, get_event_by_id
|
from utils.nostr_utils import get_referenced_event_by_id, get_event_by_id
|
||||||
|
|
||||||
|
|
||||||
class Translation:
|
class Translation(DVMTaskInterface):
|
||||||
TASK: str = "translation"
|
TASK: str = "translation"
|
||||||
COST: int = 20
|
KIND: int = EventDefinitions.KIND_NIP90_TRANSLATE_TEXT
|
||||||
|
COST: int = 0
|
||||||
|
|
||||||
@staticmethod
|
def __init__(self, name, pk):
|
||||||
def NIP89_announcement():
|
self.NAME = name
|
||||||
|
self.PK = pk
|
||||||
|
|
||||||
|
def NIP89_announcement(self):
|
||||||
nip89 = NIP89Announcement()
|
nip89 = NIP89Announcement()
|
||||||
nip89.kind = EventDefinitions.KIND_NIP90_TRANSLATE_TEXT
|
nip89.kind = self.KIND
|
||||||
|
nip89.pk = self.PK
|
||||||
nip89.dtag = os.getenv(env.TASK_TRANSLATION_NIP89_DTAG)
|
nip89.dtag = os.getenv(env.TASK_TRANSLATION_NIP89_DTAG)
|
||||||
nip89.pk = os.getenv(env.NOSTR_PRIVATE_KEY)
|
nip89.content = "{\"name\":\"" + self.NAME + "\",\"image\":\"https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg\",\"about\":\"I translate text from given text/event/job, currently using Google Translation Services into language defined in param. \",\"nip90Params\":{\"language\":{\"required\":true,\"values\":[\"af\",\"am\",\"ar\",\"az\",\"be\",\"bg\",\"bn\",\"bs\",\"ca\",\"ceb\",\"co\",\"cs\",\"cy\",\"da\",\"de\",\"el\",\"eo\",\"es\",\"et\",\"eu\",\"fa\",\"fi\",\"fr\",\"fy\",\"ga\",\"gd\",\"gl\",\"gu\",\"ha\",\"haw\",\"hi\",\"hmn\",\"hr\",\"ht\",\"hu\",\"hy\",\"id\",\"ig\",\"is\",\"it\",\"he\",\"ja\",\"jv\",\"ka\",\"kk\",\"km\",\"kn\",\"ko\",\"ku\",\"ky\",\"la\",\"lb\",\"lo\",\"lt\",\"lv\",\"mg\",\"mi\",\"mk\",\"ml\",\"mn\",\"mr\",\"ms\",\"mt\",\"my\",\"ne\",\"nl\",\"no\",\"ny\",\"or\",\"pa\",\"pl\",\"ps\",\"pt\",\"ro\",\"ru\",\"sd\",\"si\",\"sk\",\"sl\",\"sm\",\"sn\",\"so\",\"sq\",\"sr\",\"st\",\"su\",\"sv\",\"sw\",\"ta\",\"te\",\"tg\",\"th\",\"tl\",\"tr\",\"ug\",\"uk\",\"ur\",\"uz\",\"vi\",\"xh\",\"yi\",\"yo\",\"zh\",\"zu\"]}}}"
|
||||||
nip89.content = "{\"name\":\"Translator\",\"image\":\"https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg\",\"about\":\"I translate text from given text/event/job, currently using Google Translation Services into language defined in param. \",\"nip90Params\":{\"language\":{\"required\":true,\"values\":[\"af\",\"am\",\"ar\",\"az\",\"be\",\"bg\",\"bn\",\"bs\",\"ca\",\"ceb\",\"co\",\"cs\",\"cy\",\"da\",\"de\",\"el\",\"eo\",\"es\",\"et\",\"eu\",\"fa\",\"fi\",\"fr\",\"fy\",\"ga\",\"gd\",\"gl\",\"gu\",\"ha\",\"haw\",\"hi\",\"hmn\",\"hr\",\"ht\",\"hu\",\"hy\",\"id\",\"ig\",\"is\",\"it\",\"he\",\"ja\",\"jv\",\"ka\",\"kk\",\"km\",\"kn\",\"ko\",\"ku\",\"ky\",\"la\",\"lb\",\"lo\",\"lt\",\"lv\",\"mg\",\"mi\",\"mk\",\"ml\",\"mn\",\"mr\",\"ms\",\"mt\",\"my\",\"ne\",\"nl\",\"no\",\"ny\",\"or\",\"pa\",\"pl\",\"ps\",\"pt\",\"ro\",\"ru\",\"sd\",\"si\",\"sk\",\"sl\",\"sm\",\"sn\",\"so\",\"sq\",\"sr\",\"st\",\"su\",\"sv\",\"sw\",\"ta\",\"te\",\"tg\",\"th\",\"tl\",\"tr\",\"ug\",\"uk\",\"ur\",\"uz\",\"vi\",\"xh\",\"yi\",\"yo\",\"zh\",\"zu\"]}}}"
|
|
||||||
return nip89
|
return nip89
|
||||||
|
|
||||||
|
def is_input_supported(self, input_type, input_content):
|
||||||
@staticmethod
|
|
||||||
def is_input_supported(input_type, input_content):
|
|
||||||
if input_type != "event" and input_type != "job" and input_type != "text":
|
if input_type != "event" and input_type != "job" and input_type != "text":
|
||||||
return False
|
return False
|
||||||
if input_type != "text" and len(input_content) > 4999:
|
if input_type != "text" and len(input_content) > 4999:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@staticmethod
|
def create_request_form_from_nostr_event(self, event, client=None, dvm_config=None):
|
||||||
def create_requestform_from_nostr_event(event, client=None, dvmconfig=None):
|
|
||||||
request_form = {"jobID": event.id().to_hex()}
|
request_form = {"jobID": event.id().to_hex()}
|
||||||
|
|
||||||
#default values
|
#default values
|
||||||
@ -50,7 +52,7 @@ class Translation:
|
|||||||
if input_type == "event":
|
if input_type == "event":
|
||||||
for tag in event.tags():
|
for tag in event.tags():
|
||||||
if tag.as_vec()[0] == 'i':
|
if tag.as_vec()[0] == 'i':
|
||||||
evt = get_event_by_id(tag.as_vec()[1], config=dvmconfig)
|
evt = get_event_by_id(tag.as_vec()[1], config=dvm_config)
|
||||||
text = evt.content()
|
text = evt.content()
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -67,7 +69,7 @@ class Translation:
|
|||||||
[EventDefinitions.KIND_NIP90_RESULT_EXTRACT_TEXT,
|
[EventDefinitions.KIND_NIP90_RESULT_EXTRACT_TEXT,
|
||||||
EventDefinitions.KIND_NIP90_RESULT_SUMMARIZE_TEXT],
|
EventDefinitions.KIND_NIP90_RESULT_SUMMARIZE_TEXT],
|
||||||
client,
|
client,
|
||||||
config=dvmconfig)
|
config=dvm_config)
|
||||||
text = evt.content()
|
text = evt.content()
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -76,8 +78,8 @@ class Translation:
|
|||||||
replace(";", ","))
|
replace(";", ","))
|
||||||
return request_form
|
return request_form
|
||||||
|
|
||||||
@staticmethod
|
def process(self, request_form):
|
||||||
def process(options):
|
options = DVMTaskInterface.setOptions(request_form)
|
||||||
from translatepy.translators.google import GoogleTranslate
|
from translatepy.translators.google import GoogleTranslate
|
||||||
gtranslate = GoogleTranslate()
|
gtranslate = GoogleTranslate()
|
||||||
length = len(options["text"])
|
length = len(options["text"])
|
||||||
@ -101,8 +103,8 @@ class Translation:
|
|||||||
try:
|
try:
|
||||||
translated_text_part = str(gtranslate.translate(textpart, options["translation_lang"]))
|
translated_text_part = str(gtranslate.translate(textpart, options["translation_lang"]))
|
||||||
print("Translated Text part:\n\n " + translated_text_part)
|
print("Translated Text part:\n\n " + translated_text_part)
|
||||||
except:
|
except Exception as e:
|
||||||
translated_text_part = "An error occured"
|
raise Exception(e)
|
||||||
|
|
||||||
translated_text = translated_text + translated_text_part
|
translated_text = translated_text + translated_text_part
|
||||||
|
|
||||||
|
@ -82,18 +82,17 @@ def check_task_is_supported(event, client, get_duration=False, config=None):
|
|||||||
|
|
||||||
task = get_task(event, client=client, dvmconfig=dvm_config)
|
task = get_task(event, client=client, dvmconfig=dvm_config)
|
||||||
|
|
||||||
if task not in dvm_config.SUPPORTED_TASKS: # The Tasks this DVM supports (can be extended)
|
|
||||||
return False, task, duration
|
|
||||||
|
|
||||||
if input_type == 'url' and check_url_is_readable(input_value) is None:
|
if input_type == 'url' and check_url_is_readable(input_value) is None:
|
||||||
print("url not readable")
|
print("url not readable")
|
||||||
return False, task, duration
|
return False, task, duration
|
||||||
|
|
||||||
if task == Translation.TASK:
|
if task not in (x.TASK for x in dvm_config.SUPPORTED_TASKS):
|
||||||
return Translation.is_input_supported(input_type, event.content()), task, duration
|
return False, task, duration
|
||||||
|
|
||||||
elif task == TextExtractionPDF.TASK:
|
for dvm in dvm_config.SUPPORTED_TASKS:
|
||||||
return TextExtractionPDF.is_input_supported(input_type, event.content()), task, duration
|
if dvm.TASK == task:
|
||||||
|
if not dvm.is_input_supported(input_type, event.content()):
|
||||||
|
return False, task, duration
|
||||||
|
|
||||||
return True, task, duration
|
return True, task, duration
|
||||||
|
|
||||||
@ -101,7 +100,6 @@ def check_task_is_supported(event, client, get_duration=False, config=None):
|
|||||||
def check_url_is_readable(url):
|
def check_url_is_readable(url):
|
||||||
if not str(url).startswith("http"):
|
if not str(url).startswith("http"):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# If link is comaptible with one of these file formats, move on.
|
# If link is comaptible with one of these file formats, move on.
|
||||||
req = requests.get(url)
|
req = requests.get(url)
|
||||||
content_type = req.headers['content-type']
|
content_type = req.headers['content-type']
|
||||||
@ -122,13 +120,12 @@ def check_url_is_readable(url):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_amount_per_task(task, duration=0, config=None):
|
def get_amount_per_task(task, dvm_config, duration=1):
|
||||||
if task == Translation.TASK:
|
print(dvm_config.SUPPORTED_TASKS)
|
||||||
amount = Translation.COST
|
for dvm in dvm_config.SUPPORTED_TASKS:
|
||||||
elif task == TextExtractionPDF.TASK:
|
if dvm.TASK == task:
|
||||||
amount = TextExtractionPDF.COST
|
amount = dvm.COST * duration
|
||||||
|
return amount
|
||||||
else:
|
else:
|
||||||
print("[Nostr] Task " + task + " is currently not supported by this instance, skipping")
|
print("[Nostr] Task " + task + " is currently not supported by this instance, skipping")
|
||||||
return None
|
return None
|
||||||
return amount
|
|
||||||
|
@ -1,10 +1,14 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
from nostr_sdk import Event
|
from nostr_sdk import Event
|
||||||
NEW_USER_BALANCE = 250
|
|
||||||
|
NEW_USER_BALANCE: int = 250 # Free credits for new users
|
||||||
|
|
||||||
RELAY_LIST = ["wss://relay.damus.io", "wss://nostr-pub.wellorder.net", "wss://nos.lol", "wss://nostr.wine",
|
RELAY_LIST = ["wss://relay.damus.io", "wss://nostr-pub.wellorder.net", "wss://nos.lol", "wss://nostr.wine",
|
||||||
"wss://relay.nostfiles.dev", "wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg", "wss://relay.f7z.io"]
|
"wss://relay.nostfiles.dev", "wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg",
|
||||||
|
"wss://relay.f7z.io"]
|
||||||
|
|
||||||
|
|
||||||
class EventDefinitions:
|
class EventDefinitions:
|
||||||
KIND_DM: int = 4
|
KIND_DM: int = 4
|
||||||
KIND_ZAP: int = 9735
|
KIND_ZAP: int = 9735
|
||||||
@ -41,22 +45,19 @@ class DVMConfig:
|
|||||||
PRIVATE_KEY: str
|
PRIVATE_KEY: str
|
||||||
|
|
||||||
RELAY_LIST = ["wss://relay.damus.io", "wss://nostr-pub.wellorder.net", "wss://nos.lol", "wss://nostr.wine",
|
RELAY_LIST = ["wss://relay.damus.io", "wss://nostr-pub.wellorder.net", "wss://nos.lol", "wss://nostr.wine",
|
||||||
"wss://relay.nostfiles.dev", "wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg", "wss://relay.f7z.io"]
|
"wss://relay.nostfiles.dev", "wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg",
|
||||||
|
"wss://relay.f7z.io"]
|
||||||
RELAY_TIMEOUT = 5
|
RELAY_TIMEOUT = 5
|
||||||
LNBITS_INVOICE_KEY = ''
|
LNBITS_INVOICE_KEY = ''
|
||||||
LNBITS_URL = 'https://lnbits.com'
|
LNBITS_URL = 'https://lnbits.com'
|
||||||
REQUIRES_NIP05: bool = False
|
REQUIRES_NIP05: bool = False
|
||||||
|
|
||||||
|
|
||||||
SHOWRESULTBEFOREPAYMENT: bool = True # if this is true show results even when not paid right after autoprocess
|
SHOWRESULTBEFOREPAYMENT: bool = True # if this is true show results even when not paid right after autoprocess
|
||||||
NEW_USER_BALANCE: int = 250 # Free credits for new users
|
|
||||||
|
|
||||||
|
|
||||||
NIP89s: list = []
|
NIP89s: list = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class JobToWatch:
|
class JobToWatch:
|
||||||
event_id: str
|
event_id: str
|
||||||
@ -71,7 +72,8 @@ class JobToWatch:
|
|||||||
expires: int
|
expires: int
|
||||||
from_bot: bool
|
from_bot: bool
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class RequiredJobToWatch:
|
class RequiredJobToWatch:
|
||||||
event: Event
|
event: Event
|
||||||
timestamp: int
|
timestamp: int
|
||||||
|
Loading…
x
Reference in New Issue
Block a user