mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-12-01 00:47:39 +01:00
added generate image via nserver, refactor
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import requests
|
||||
|
||||
from tasks.textextractionPDF import TextExtractionPDF
|
||||
from utils.definitions import EventDefinitions
|
||||
from utils.nostr_utils import get_event_by_id
|
||||
|
||||
from tasks.textextractionpdf import TextExtractionPDF
|
||||
from tasks.translation import Translation
|
||||
from tasks.imagegenerationsdxl import ImageGenerationSDXL
|
||||
|
||||
|
||||
def get_task(event, client, dvmconfig):
|
||||
@@ -35,9 +35,9 @@ def get_task(event, client, dvmconfig):
|
||||
evt = get_event_by_id(tag.as_vec()[1], config=dvmconfig)
|
||||
if evt is not None:
|
||||
if evt.kind() == 1063:
|
||||
for tag in evt.tags():
|
||||
if tag.as_vec()[0] == 'url':
|
||||
file_type = check_url_is_readable(tag.as_vec()[1])
|
||||
for tg in evt.tags():
|
||||
if tg.as_vec()[0] == 'url':
|
||||
file_type = check_url_is_readable(tg.as_vec()[1])
|
||||
if file_type == "pdf":
|
||||
return "pdf-to-text"
|
||||
else:
|
||||
@@ -45,9 +45,10 @@ def get_task(event, client, dvmconfig):
|
||||
else:
|
||||
return "unknown type"
|
||||
|
||||
|
||||
elif event.kind() == EventDefinitions.KIND_NIP90_TRANSLATE_TEXT:
|
||||
return Translation.TASK
|
||||
elif event.kind() == EventDefinitions.KIND_NIP90_GENERATE_IMAGE:
|
||||
return ImageGenerationSDXL.TASK
|
||||
|
||||
else:
|
||||
return "unknown type"
|
||||
@@ -121,7 +122,6 @@ def check_url_is_readable(url):
|
||||
|
||||
|
||||
def get_amount_per_task(task, dvm_config, duration=1):
|
||||
print(dvm_config.SUPPORTED_TASKS)
|
||||
for dvm in dvm_config.SUPPORTED_TASKS:
|
||||
if dvm.TASK == task:
|
||||
amount = dvm.COST * duration
|
||||
|
||||
@@ -8,5 +8,6 @@ LNBITS_HOST = "LNBITS_HOST"
|
||||
|
||||
TASK_TRANSLATION_NIP89_DTAG = "TASK_TRANSLATION_NIP89_DTAG"
|
||||
TASK_TEXTEXTRACTION_NIP89_DTAG = "TASK_TEXTEXTRACTION_NIP89_DTAG"
|
||||
TASK_IMAGEGENERATION_NIP89_DTAG = "TASK_IMAGEGENERATION_NIP89_DTAG"
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,15 @@
|
||||
import json
|
||||
import datetime as datetime
|
||||
import os
|
||||
from types import NoneType
|
||||
import requests
|
||||
from pyupload.uploader import CatboxUploader
|
||||
|
||||
import pandas
|
||||
|
||||
|
||||
'''
|
||||
Post process results to either given output format or a Nostr readable plain text.
|
||||
'''
|
||||
def post_process_result(anno, original_event):
|
||||
print("post-processing...")
|
||||
if isinstance(anno, pandas.DataFrame): # if input is an anno we parse it to required output format
|
||||
@@ -84,7 +89,52 @@ def post_process_result(anno, original_event):
|
||||
return result
|
||||
|
||||
|
||||
'''
|
||||
Convenience function to replace words like Noster with Nostr
|
||||
'''
|
||||
def replace_broken_words(text):
|
||||
result = (text.replace("Noster", "Nostr").replace("Nostra", "Nostr").replace("no stir", "Nostr").
|
||||
replace("Nostro", "Nostr").replace("Impub", "npub").replace("sets", "Sats"))
|
||||
return result
|
||||
|
||||
|
||||
'''
|
||||
Function to upload to Nostr.build and if it fails to Nostrfiles.dev
|
||||
Larger files than these hosters allow and fallback is catbox currently.
|
||||
Will probably need to switch to another system in the future.
|
||||
'''
|
||||
def uploadMediaToHoster(filepath):
|
||||
print("Uploading image: " + filepath)
|
||||
try:
|
||||
files = {'file': open(filepath, 'rb')}
|
||||
file_stats = os.stat(filepath)
|
||||
sizeinmb = file_stats.st_size / (1024*1024)
|
||||
print("Filesize of Uploaded media: " + str(sizeinmb) + " Mb.")
|
||||
if sizeinmb > 25:
|
||||
uploader = CatboxUploader(filepath)
|
||||
result = uploader.execute()
|
||||
return result
|
||||
else:
|
||||
url = 'https://nostr.build/api/v2/upload/files'
|
||||
response = requests.post(url, files=files)
|
||||
json_object = json.loads(response.text)
|
||||
result = json_object["data"][0]["url"]
|
||||
return result
|
||||
except:
|
||||
try:
|
||||
file = {'file': open(filepath, 'rb')}
|
||||
url = 'https://nostrfiles.dev/upload_image'
|
||||
response = requests.post(url, files=file)
|
||||
json_object = json.loads(response.text)
|
||||
print(json_object["url"])
|
||||
return json_object["url"]
|
||||
# fallback filehoster
|
||||
except:
|
||||
|
||||
try:
|
||||
uploader = CatboxUploader(filepath)
|
||||
result = uploader.execute()
|
||||
print(result)
|
||||
return result
|
||||
except:
|
||||
return "Upload not possible, all hosters didn't work"
|
||||
|
||||
Reference in New Issue
Block a user