mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-06-21 20:40:50 +02:00
Merge pull request #10 from believethehype/better_info
added info command to bot, added fix_cost and per_unit_cost
This commit is contained in:
commit
5b3df117ae
@ -107,15 +107,9 @@ def check_nova_server_status(jobID, address) -> str | pd.DataFrame:
|
|||||||
columns = ['from', 'to', 'name', 'conf']
|
columns = ['from', 'to', 'name', 'conf']
|
||||||
result = pd.DataFrame([row.split(';') for row in anno_string.split('\n')],
|
result = pd.DataFrame([row.split(';') for row in anno_string.split('\n')],
|
||||||
columns=columns)
|
columns=columns)
|
||||||
#print(str(result))
|
|
||||||
return result
|
return result
|
||||||
#with open("response.zip", "wb") as f:
|
|
||||||
# f.write(response.content)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
#zf.extractall()
|
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Couldn't fetch result: " + str(e))
|
print("Couldn't fetch result: " + str(e))
|
||||||
|
|
||||||
|
148
bot.py
148
bot.py
@ -84,87 +84,117 @@ class Bot:
|
|||||||
if decrypted_text[0].isdigit():
|
if decrypted_text[0].isdigit():
|
||||||
index = int(decrypted_text.split(' ')[0]) - 1
|
index = int(decrypted_text.split(' ')[0]) - 1
|
||||||
task = self.dvm_config.SUPPORTED_DVMS[index].TASK
|
task = self.dvm_config.SUPPORTED_DVMS[index].TASK
|
||||||
print("[" + self.NAME + "] Request from " + str(user.name) + " (" + str(user.nip05) + ", Balance: "
|
|
||||||
+ str(user.balance) + " Sats) Task: " + str(task))
|
|
||||||
|
|
||||||
if user.isblacklisted:
|
|
||||||
# For some reason an admin might blacklist npubs, e.g. for abusing the service
|
|
||||||
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
|
|
||||||
"Your are currently blocked from all "
|
|
||||||
"services.", None).to_event(self.keys)
|
|
||||||
send_event(evt, client=self.client, dvm_config=dvm_config)
|
|
||||||
|
|
||||||
|
if decrypted_text.split(" ")[1].lower() == "info":
|
||||||
|
nip89 = self.dvm_config.SUPPORTED_DVMS[index].dvm_config.NIP89
|
||||||
|
nip89content = json.loads(nip89.content)
|
||||||
|
info = ""
|
||||||
|
if nip89content.get("name"):
|
||||||
|
info += "Name: " + nip89content.get("name") + "\n"
|
||||||
|
info += nip89content.get("image")+ "\n"
|
||||||
|
info += "About:\n" + nip89content.get("about") + "\n"
|
||||||
|
params = nip89content["nip90Params"]
|
||||||
|
print(params)
|
||||||
|
info += "\nParameters:\n"
|
||||||
|
for param in params:
|
||||||
|
info += "-" + param + '\n'
|
||||||
|
info += "Required: " + str(params[param]['required']) + '\n'
|
||||||
|
info += "Possible Values: " + json.dumps(params[param]['values']) + '\n\n'
|
||||||
|
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
|
||||||
|
info, None).to_event(self.keys)
|
||||||
|
send_event(evt, client=self.client, dvm_config=dvm_config)
|
||||||
|
|
||||||
|
print(nip89content)
|
||||||
else:
|
else:
|
||||||
command = decrypted_text.replace(decrypted_text.split(' ')[0] + " ", "")
|
print("[" + self.NAME + "] Request from " + str(user.name) + " (" + str(user.nip05) + ", Balance: "
|
||||||
input = command.split(" -")[0].rstrip()
|
+ str(user.balance) + " Sats) Task: " + str(task))
|
||||||
input_type = "text"
|
|
||||||
if input.startswith("http"):
|
|
||||||
input_type = "url"
|
|
||||||
|
|
||||||
i_tag = Tag.parse(["i", input, input_type])
|
if user.isblacklisted:
|
||||||
#bid = str(self.dvm_config.SUPPORTED_DVMS[index].COST * 1000)
|
# For some reason an admin might blacklist npubs, e.g. for abusing the service
|
||||||
#bid_tag = Tag.parse(['bid', bid, bid])
|
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
|
||||||
relays_tag = Tag.parse(["relays", json.dumps(self.dvm_config.RELAY_LIST)])
|
"Your are currently blocked from all "
|
||||||
alt_tag = Tag.parse(["alt", self.dvm_config.SUPPORTED_DVMS[index].TASK])
|
"services.", None).to_event(self.keys)
|
||||||
|
send_event(evt, client=self.client, dvm_config=dvm_config)
|
||||||
|
|
||||||
tags = [i_tag.as_vec(), relays_tag.as_vec(), alt_tag.as_vec()]
|
else:
|
||||||
|
command = decrypted_text.replace(decrypted_text.split(' ')[0] + " ", "")
|
||||||
|
input = command.split(" -")[0].rstrip()
|
||||||
|
input_type = "text"
|
||||||
|
if input.startswith("http"):
|
||||||
|
input_type = "url"
|
||||||
|
|
||||||
remaining_text = command.replace(input, "")
|
i_tag = Tag.parse(["i", input, input_type])
|
||||||
print(remaining_text)
|
#bid = str(self.dvm_config.SUPPORTED_DVMS[index].FIX_COST * 1000)
|
||||||
params = remaining_text.rstrip().split(" -")
|
#bid_tag = Tag.parse(['bid', bid, bid])
|
||||||
|
relays_tag = Tag.parse(["relays", json.dumps(self.dvm_config.RELAY_LIST)])
|
||||||
|
alt_tag = Tag.parse(["alt", self.dvm_config.SUPPORTED_DVMS[index].TASK])
|
||||||
|
|
||||||
for i in params:
|
tags = [i_tag.as_vec(), relays_tag.as_vec(), alt_tag.as_vec()]
|
||||||
print(i)
|
|
||||||
if i != " ":
|
|
||||||
try:
|
|
||||||
split = i.split(" ")
|
|
||||||
param = str(split[0])
|
|
||||||
print(str(param))
|
|
||||||
value = str(split[1])
|
|
||||||
print(str(value))
|
|
||||||
if param == "cashu":
|
|
||||||
tag = Tag.parse([param, value])
|
|
||||||
else:
|
|
||||||
tag = Tag.parse(["param", param, value])
|
|
||||||
tags.append(tag.as_vec())
|
|
||||||
print("Added params: " + str(tag.as_vec()))
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
print("Couldn't add " + str(i))
|
|
||||||
|
|
||||||
encrypted_params_string = json.dumps(tags)
|
remaining_text = command.replace(input, "")
|
||||||
|
print(remaining_text)
|
||||||
|
params = remaining_text.rstrip().split(" -")
|
||||||
|
|
||||||
print(encrypted_params_string)
|
for i in params:
|
||||||
|
print(i)
|
||||||
|
if i != " ":
|
||||||
|
try:
|
||||||
|
split = i.split(" ")
|
||||||
|
param = str(split[0])
|
||||||
|
print(str(param))
|
||||||
|
value = str(split[1])
|
||||||
|
print(str(value))
|
||||||
|
if param == "cashu":
|
||||||
|
tag = Tag.parse([param, value])
|
||||||
|
else:
|
||||||
|
tag = Tag.parse(["param", param, value])
|
||||||
|
tags.append(tag.as_vec())
|
||||||
|
print("Added params: " + str(tag.as_vec()))
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
print("Couldn't add " + str(i))
|
||||||
|
|
||||||
encrypted_params = nip04_encrypt(self.keys.secret_key(),
|
encrypted_params_string = json.dumps(tags)
|
||||||
PublicKey.from_hex(
|
|
||||||
self.dvm_config.SUPPORTED_DVMS[index].PUBLIC_KEY),
|
|
||||||
encrypted_params_string)
|
|
||||||
|
|
||||||
encrypted_tag = Tag.parse(['encrypted'])
|
print(encrypted_params_string)
|
||||||
p_tag = Tag.parse(['p', self.dvm_config.SUPPORTED_DVMS[index].PUBLIC_KEY])
|
|
||||||
encrypted_nip90request = (EventBuilder(self.dvm_config.SUPPORTED_DVMS[index].KIND,
|
|
||||||
encrypted_params, [p_tag, encrypted_tag]).
|
|
||||||
to_event(self.keys))
|
|
||||||
|
|
||||||
entry = {"npub": user.npub, "event_id": encrypted_nip90request.id().to_hex(),
|
encrypted_params = nip04_encrypt(self.keys.secret_key(),
|
||||||
"dvm_key": self.dvm_config.SUPPORTED_DVMS[index].PUBLIC_KEY, "is_paid": False}
|
PublicKey.from_hex(
|
||||||
self.job_list.append(entry)
|
self.dvm_config.SUPPORTED_DVMS[index].PUBLIC_KEY),
|
||||||
|
encrypted_params_string)
|
||||||
|
|
||||||
send_event(encrypted_nip90request, client=self.client, dvm_config=dvm_config)
|
encrypted_tag = Tag.parse(['encrypted'])
|
||||||
|
p_tag = Tag.parse(['p', self.dvm_config.SUPPORTED_DVMS[index].PUBLIC_KEY])
|
||||||
|
encrypted_nip90request = (EventBuilder(self.dvm_config.SUPPORTED_DVMS[index].KIND,
|
||||||
|
encrypted_params, [p_tag, encrypted_tag]).
|
||||||
|
to_event(self.keys))
|
||||||
|
|
||||||
|
entry = {"npub": user.npub, "event_id": encrypted_nip90request.id().to_hex(),
|
||||||
|
"dvm_key": self.dvm_config.SUPPORTED_DVMS[index].PUBLIC_KEY, "is_paid": False}
|
||||||
|
self.job_list.append(entry)
|
||||||
|
|
||||||
|
send_event(encrypted_nip90request, client=self.client, dvm_config=dvm_config)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("[" + self.NAME + "] Message from " + user.name + ": " + decrypted_text)
|
print("[" + self.NAME + "] Message from " + user.name + ": " + decrypted_text)
|
||||||
message = "DVMs that I support:\n\n"
|
message = "DVMs that I support:\n\n"
|
||||||
index = 1
|
index = 1
|
||||||
for p in self.dvm_config.SUPPORTED_DVMS:
|
for p in self.dvm_config.SUPPORTED_DVMS:
|
||||||
message += str(index) + " " + p.NAME + " " + p.TASK + " " + str(p.COST) + " Sats" + "\n"
|
if p.PER_UNIT_COST != 0 and p.PER_UNIT_COST is not None:
|
||||||
|
message += (str(index) + " " + p.NAME + " " + p.TASK + " " + str(p.FIX_COST) +
|
||||||
|
" Sats + " + str(p.PER_UNIT_COST) + " Sats per Second\n")
|
||||||
|
else:
|
||||||
|
message += (str(index) + " " + p.NAME + " " + p.TASK + " " + str(p.FIX_COST) +
|
||||||
|
" Sats\n")
|
||||||
index += 1
|
index += 1
|
||||||
|
|
||||||
time.sleep(1.0)
|
time.sleep(3.0)
|
||||||
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
|
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
|
||||||
message + "\nSelect an Index and provide an input ("
|
message + "\nSelect an Index and provide an input ("
|
||||||
"e.g. 1 A purple ostrich)",
|
"e.g. 2 A purple ostrich)\nType index info to learn more about each DVM. (e.g. 2 info)\n\n"
|
||||||
|
""
|
||||||
|
"Add -cashu cashutoken with the amount + mint fees (at least 3 sat) to pay via #cashu",
|
||||||
nostr_event.id()).to_event(self.keys)
|
nostr_event.id()).to_event(self.keys)
|
||||||
|
|
||||||
send_event(evt, client=self.client, dvm_config=dvm_config)
|
send_event(evt, client=self.client, dvm_config=dvm_config)
|
||||||
@ -259,7 +289,7 @@ class Bot:
|
|||||||
else:
|
else:
|
||||||
print("Bot payment-required")
|
print("Bot payment-required")
|
||||||
evt = EventBuilder.new_encrypted_direct_msg(self.keys, PublicKey.from_hex(entry["npub"]),
|
evt = EventBuilder.new_encrypted_direct_msg(self.keys, PublicKey.from_hex(entry["npub"]),
|
||||||
"Current balance: " + str(user.balance) + " Sats. Balance required, please zap me with at least " +
|
"Current balance: " + str(user.balance) + " Sats. Balance of "+ str(amount) +" Sats required. Please zap me with at least " +
|
||||||
str(int(amount - user.balance))
|
str(int(amount - user.balance))
|
||||||
+ " Sats, then try again.",
|
+ " Sats, then try again.",
|
||||||
None).to_event(self.keys)
|
None).to_event(self.keys)
|
||||||
|
2
dvm.py
2
dvm.py
@ -110,7 +110,7 @@ class DVM:
|
|||||||
|
|
||||||
task_is_free = False
|
task_is_free = False
|
||||||
for dvm in self.dvm_config.SUPPORTED_DVMS:
|
for dvm in self.dvm_config.SUPPORTED_DVMS:
|
||||||
if dvm.TASK == task and dvm.COST == 0:
|
if dvm.TASK == task and dvm.FIX_COST == 0 and dvm.PER_UNIT_COST == 0:
|
||||||
task_is_free = True
|
task_is_free = True
|
||||||
|
|
||||||
cashu_redeemed = False
|
cashu_redeemed = False
|
||||||
|
@ -13,7 +13,8 @@ class DVMTaskInterface:
|
|||||||
NAME: str
|
NAME: str
|
||||||
KIND: int
|
KIND: int
|
||||||
TASK: str
|
TASK: str
|
||||||
COST: float
|
FIX_COST: float = 0
|
||||||
|
PER_UNIT_COST: float = 0
|
||||||
PRIVATE_KEY: str
|
PRIVATE_KEY: str
|
||||||
PUBLIC_KEY: str
|
PUBLIC_KEY: str
|
||||||
DVM = DVM
|
DVM = DVM
|
||||||
@ -31,8 +32,10 @@ class DVMTaskInterface:
|
|||||||
if dvm_config.PUBLIC_KEY == "" or dvm_config.PUBLIC_KEY is None:
|
if dvm_config.PUBLIC_KEY == "" or dvm_config.PUBLIC_KEY is None:
|
||||||
dvm_config.PUBLIC_KEY = Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex()
|
dvm_config.PUBLIC_KEY = Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex()
|
||||||
self.PUBLIC_KEY = dvm_config.PUBLIC_KEY
|
self.PUBLIC_KEY = dvm_config.PUBLIC_KEY
|
||||||
if dvm_config.COST is not None:
|
if dvm_config.FIX_COST is not None:
|
||||||
self.COST = dvm_config.COST
|
self.FIX_COST = dvm_config.FIX_COST
|
||||||
|
if dvm_config.PER_UNIT_COST is not None:
|
||||||
|
self.PER_UNIT_COST = dvm_config.PER_UNIT_COST
|
||||||
|
|
||||||
dvm_config.SUPPORTED_DVMS = [self]
|
dvm_config.SUPPORTED_DVMS = [self]
|
||||||
dvm_config.DB = "db/" + self.NAME + ".db"
|
dvm_config.DB = "db/" + self.NAME + ".db"
|
||||||
|
@ -200,7 +200,7 @@ def build_dalle(name):
|
|||||||
dvm_config.LNBITS_INVOICE_KEY = os.getenv("LNBITS_INVOICE_KEY")
|
dvm_config.LNBITS_INVOICE_KEY = os.getenv("LNBITS_INVOICE_KEY")
|
||||||
dvm_config.LNBITS_URL = os.getenv("LNBITS_HOST")
|
dvm_config.LNBITS_URL = os.getenv("LNBITS_HOST")
|
||||||
profit_in_sats = 10
|
profit_in_sats = 10
|
||||||
dvm_config.COST = int(((4.0 / (get_price_per_sat("USD") * 100)) + profit_in_sats))
|
dvm_config.FIX_COST = int(((4.0 / (get_price_per_sat("USD") * 100)) + profit_in_sats))
|
||||||
|
|
||||||
nip90params = {
|
nip90params = {
|
||||||
"size": {
|
"size": {
|
||||||
|
@ -25,7 +25,7 @@ Params: -model # models: juggernaut, dynavision, colossusProject, newrea
|
|||||||
class ImageGenerationDALLE(DVMTaskInterface):
|
class ImageGenerationDALLE(DVMTaskInterface):
|
||||||
KIND: int = EventDefinitions.KIND_NIP90_GENERATE_IMAGE
|
KIND: int = EventDefinitions.KIND_NIP90_GENERATE_IMAGE
|
||||||
TASK: str = "text-to-image"
|
TASK: str = "text-to-image"
|
||||||
COST: float = 120
|
FIX_COST: float = 120
|
||||||
|
|
||||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
||||||
admin_config: AdminConfig = None, options=None):
|
admin_config: AdminConfig = None, options=None):
|
||||||
|
@ -21,7 +21,7 @@ Params: -model # models: juggernaut, dynavision, colossusProject, newrea
|
|||||||
class ImageGenerationSDXL(DVMTaskInterface):
|
class ImageGenerationSDXL(DVMTaskInterface):
|
||||||
KIND: int = EventDefinitions.KIND_NIP90_GENERATE_IMAGE
|
KIND: int = EventDefinitions.KIND_NIP90_GENERATE_IMAGE
|
||||||
TASK: str = "text-to-image"
|
TASK: str = "text-to-image"
|
||||||
COST: float = 50
|
FIX_COST: float = 50
|
||||||
|
|
||||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
||||||
admin_config: AdminConfig = None, options=None):
|
admin_config: AdminConfig = None, options=None):
|
||||||
|
@ -25,7 +25,8 @@ Params: -model # models: juggernaut, dynavision, colossusProject, newrea
|
|||||||
class SpeechToTextWhisperX(DVMTaskInterface):
|
class SpeechToTextWhisperX(DVMTaskInterface):
|
||||||
KIND: int = EventDefinitions.KIND_NIP90_EXTRACT_TEXT
|
KIND: int = EventDefinitions.KIND_NIP90_EXTRACT_TEXT
|
||||||
TASK: str = "speech-to-text"
|
TASK: str = "speech-to-text"
|
||||||
COST: float = 0.1
|
FIX_COST: float = 10
|
||||||
|
PER_UNIT_COST: float = 0.1
|
||||||
|
|
||||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
||||||
admin_config: AdminConfig = None, options=None):
|
admin_config: AdminConfig = None, options=None):
|
||||||
|
@ -23,7 +23,7 @@ Params: None
|
|||||||
class TextExtractionPDF(DVMTaskInterface):
|
class TextExtractionPDF(DVMTaskInterface):
|
||||||
KIND: int = EventDefinitions.KIND_NIP90_EXTRACT_TEXT
|
KIND: int = EventDefinitions.KIND_NIP90_EXTRACT_TEXT
|
||||||
TASK: str = "pdf-to-text"
|
TASK: str = "pdf-to-text"
|
||||||
COST: float = 0
|
FIX_COST: float = 0
|
||||||
|
|
||||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
||||||
admin_config: AdminConfig = None, options=None):
|
admin_config: AdminConfig = None, options=None):
|
||||||
|
@ -21,7 +21,7 @@ Params: -language The target language
|
|||||||
class Translation(DVMTaskInterface):
|
class Translation(DVMTaskInterface):
|
||||||
KIND: int = EventDefinitions.KIND_NIP90_TRANSLATE_TEXT
|
KIND: int = EventDefinitions.KIND_NIP90_TRANSLATE_TEXT
|
||||||
TASK: str = "translation"
|
TASK: str = "translation"
|
||||||
COST: float = 0
|
FIX_COST: float = 0
|
||||||
|
|
||||||
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
def __init__(self, name, dvm_config: DVMConfig, nip89config: NIP89Config,
|
||||||
admin_config: AdminConfig = None, options=None):
|
admin_config: AdminConfig = None, options=None):
|
||||||
|
@ -144,12 +144,10 @@ def check_url_is_readable(url):
|
|||||||
|
|
||||||
|
|
||||||
def get_amount_per_task(task, dvm_config, duration=1):
|
def get_amount_per_task(task, dvm_config, duration=1):
|
||||||
# duration is either static 1 (for images etc) or in seconds
|
# duration is either static 1 (for images etc) or in seconds by default (e.g. audio/video)
|
||||||
if duration == 0:
|
|
||||||
duration = 1
|
|
||||||
for dvm in dvm_config.SUPPORTED_DVMS: # this is currently just one
|
for dvm in dvm_config.SUPPORTED_DVMS: # this is currently just one
|
||||||
if dvm.TASK == task:
|
if dvm.TASK == task:
|
||||||
amount = dvm.COST * duration
|
amount = dvm.FIX_COST + (dvm.PER_UNIT_COST * duration)
|
||||||
return amount
|
return amount
|
||||||
else:
|
else:
|
||||||
print("[" + dvm_config.SUPPORTED_DVMS[
|
print("[" + dvm_config.SUPPORTED_DVMS[
|
||||||
|
@ -9,7 +9,8 @@ class DVMConfig:
|
|||||||
SUPPORTED_DVMS= []
|
SUPPORTED_DVMS= []
|
||||||
PRIVATE_KEY: str = ""
|
PRIVATE_KEY: str = ""
|
||||||
PUBLIC_KEY: str = ""
|
PUBLIC_KEY: str = ""
|
||||||
COST: int = None
|
FIX_COST: float = None
|
||||||
|
PER_UNIT_COST: float = None
|
||||||
|
|
||||||
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.nostfiles.dev", "wss://nostr.mom", "wss://nostr.oxtr.dev", "wss://relay.nostr.bg",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user