Compare commits
11 Commits
sigit-issu
...
master
Author | SHA1 | Date | |
---|---|---|---|
8d335b275e | |||
19288853e5 | |||
79e338958a | |||
69a2cf4eac | |||
19e03cf360 | |||
102ab08f74 | |||
a5cea42254 | |||
421527242e | |||
3bd70e1281 | |||
|
90cdcf8745 | ||
|
772e73f48d |
@ -6,11 +6,11 @@ Building on the great work of Pablo: https://github.com/pablof7z/nostr-data-vend
|
|||||||
|
|
||||||
The following Jobs are supported by this DVM:
|
The following Jobs are supported by this DVM:
|
||||||
|
|
||||||
- Get OnNostrSince Block Height
|
- Get OnNostrSince Block Height
|
||||||
|
|
||||||
- Send nPub get Block Height (from blockchain.info)
|
- Send nPub get Block Height (from blockchain.info)
|
||||||
|
|
||||||
- More job types will be added in the future
|
- More job types will be added in the future
|
||||||
|
|
||||||
# Demo video
|
# Demo video
|
||||||
|
|
||||||
@ -39,7 +39,7 @@ pnpm run build
|
|||||||
```
|
```
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pm2 start --name "Sigit-DVM" -- start
|
pm2 start --name "Sigit-DVM" npm -- start
|
||||||
```
|
```
|
||||||
|
|
||||||
## Make PM2 auto start on reboot
|
## Make PM2 auto start on reboot
|
||||||
|
@ -18,7 +18,8 @@ export async function blockChainBlockNumberJob(
|
|||||||
return closestObject.block_index.toString()
|
return closestObject.block_index.toString()
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.log('err in blockChain request :>> ', err)
|
log('err in blockChain request :>> ', err)
|
||||||
|
|
||||||
return Promise.reject(err)
|
return Promise.reject(err)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
import { NDKEvent, type NostrEvent } from '@nostr-dev-kit/ndk'
|
import { NDKEvent, type NostrEvent } from '@nostr-dev-kit/ndk'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import FormData from 'form-data'
|
import FormData from 'form-data'
|
||||||
import { log } from '../main.js'
|
|
||||||
import { fetchFileFromInput } from '../utils/fetch-file-from-input.js'
|
import { fetchFileFromInput } from '../utils/fetch-file-from-input.js'
|
||||||
import { ndk } from '../main.js'
|
import { ndk, log } from '../main.js'
|
||||||
// import { fileTypeFromFile, type FileTypeResult } from 'file-type';
|
// import { fileTypeFromFile, type FileTypeResult } from 'file-type';
|
||||||
import { exec } from 'child_process'
|
import { exec } from 'child_process'
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
@ -43,7 +42,8 @@ export async function speechToTextJob(event: NDKEvent): Promise<string> {
|
|||||||
}
|
}
|
||||||
const formData = new FormData()
|
const formData = new FormData()
|
||||||
const a = fs.createReadStream(file)
|
const a = fs.createReadStream(file)
|
||||||
console.log({ a })
|
|
||||||
|
log({ a })
|
||||||
|
|
||||||
formData.append('file', a)
|
formData.append('file', a)
|
||||||
formData.append('model', 'whisper-1')
|
formData.append('model', 'whisper-1')
|
||||||
@ -57,12 +57,14 @@ export async function speechToTextJob(event: NDKEvent): Promise<string> {
|
|||||||
})
|
})
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
const { text } = response.data
|
const { text } = response.data
|
||||||
console.log({ text })
|
|
||||||
|
log({ text })
|
||||||
|
|
||||||
resolve(text)
|
resolve(text)
|
||||||
// Handle response
|
// Handle response
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log({ error })
|
log({ error })
|
||||||
// Handle error
|
// Handle error
|
||||||
})
|
})
|
||||||
}, 1000)
|
}, 1000)
|
||||||
@ -78,9 +80,11 @@ export async function speechToTextJob(event: NDKEvent): Promise<string> {
|
|||||||
const endTime = range[3]
|
const endTime = range[3]
|
||||||
const randomName = Math.random().toString(36).substring(7) + '.mp3'
|
const randomName = Math.random().toString(36).substring(7) + '.mp3'
|
||||||
const command = `ffmpeg -ss ${startTime} -to ${endTime} -i ${file} -vn -acodec copy ${randomName}`
|
const command = `ffmpeg -ss ${startTime} -to ${endTime} -i ${file} -vn -acodec copy ${randomName}`
|
||||||
console.log({ startTime, endTime, randomName, command })
|
|
||||||
|
log({ startTime, endTime, randomName, command })
|
||||||
|
|
||||||
exec(command, (error, stderr, stdout) => {
|
exec(command, (error, stderr, stdout) => {
|
||||||
console.log('ffmpeg', { error, stderr, stdout })
|
log('ffmpeg', { error, stderr, stdout })
|
||||||
|
|
||||||
whisperCommand(resolve, randomName)
|
whisperCommand(resolve, randomName)
|
||||||
})
|
})
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
import { NDKEvent } from '@nostr-dev-kit/ndk'
|
import { NDKEvent } from '@nostr-dev-kit/ndk'
|
||||||
import { validateJobRequest } from '../validations/index.js'
|
import { validateJobRequest } from '../validations/index.js'
|
||||||
import { inProgress } from '../jobs/reactions/in-progress.js'
|
import { inProgress } from '../jobs/reactions/in-progress.js'
|
||||||
|
import { log } from '../main.js'
|
||||||
|
|
||||||
export async function onNewSummarizationJob(event: NDKEvent): Promise<void> {
|
export async function onNewSummarizationJob(event: NDKEvent): Promise<void> {
|
||||||
console.log('New summarization job')
|
log('New summarization job')
|
||||||
|
|
||||||
await validateJobRequest(event)
|
await validateJobRequest(event).catch((err) =>
|
||||||
|
log(`Job validation error: `, err)
|
||||||
|
)
|
||||||
|
|
||||||
inProgress(event)
|
inProgress(event).catch((err) => log('Processing event error: ', err))
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { NDKEvent } from '@nostr-dev-kit/ndk'
|
import { NDKEvent } from '@nostr-dev-kit/ndk'
|
||||||
import { log, ndk } from '../../main.js'
|
import { ndk, log } from '../../main.js'
|
||||||
|
|
||||||
export async function inProgress(event: NDKEvent): Promise<NDKEvent> {
|
export async function inProgress(event: NDKEvent): Promise<NDKEvent> {
|
||||||
log('marking job as in progress')
|
log('marking job as in progress')
|
||||||
@ -11,7 +11,13 @@ export async function inProgress(event: NDKEvent): Promise<NDKEvent> {
|
|||||||
})
|
})
|
||||||
|
|
||||||
reactEvent.tag(event, 'job')
|
reactEvent.tag(event, 'job')
|
||||||
await reactEvent.sign()
|
|
||||||
await reactEvent.publish()
|
await reactEvent.sign().catch((err) => {
|
||||||
|
throw `Event signing error: ${err}`
|
||||||
|
})
|
||||||
|
await reactEvent.publish().catch((err) => {
|
||||||
|
throw `Event publishing error: ${err}`
|
||||||
|
})
|
||||||
|
|
||||||
return reactEvent
|
return reactEvent
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ export async function publishStatus(
|
|||||||
tags: [['status', status], ...extraTags]
|
tags: [['status', status], ...extraTags]
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log({ extraTags })
|
log({ extraTags })
|
||||||
|
|
||||||
reactEvent.tag(event, 'job')
|
reactEvent.tag(event, 'job')
|
||||||
await reactEvent.sign()
|
await reactEvent.sign()
|
||||||
|
41
src/main.ts
41
src/main.ts
@ -28,7 +28,9 @@ export const ndk = new NDK({
|
|||||||
],
|
],
|
||||||
signer: getSigner()
|
signer: getSigner()
|
||||||
})
|
})
|
||||||
await ndk.connect(2000)
|
|
||||||
|
await ndk.connect(2000).catch((err) => log('NDK connection error: ', err))
|
||||||
|
|
||||||
log('connected')
|
log('connected')
|
||||||
|
|
||||||
const subs = ndk.subscribe(
|
const subs = ndk.subscribe(
|
||||||
@ -196,25 +198,44 @@ async function processJobEvent(event: NDKEvent, type: JobType): Promise<void> {
|
|||||||
return promise
|
return promise
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const logJobError = (job: JobType, err: any) =>
|
||||||
|
log(`Error thrown by ${job}: `, err)
|
||||||
|
|
||||||
const startProcessing = async () => {
|
const startProcessing = async () => {
|
||||||
log('startProcessing')
|
log('startProcessing')
|
||||||
await inProgress(event)
|
|
||||||
|
await inProgress(event).catch((err) => log('Processing event error: ', err))
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case JobTypes.Summarize: {
|
case JobTypes.Summarize: {
|
||||||
output = await onNewSummarizationJob(event)
|
output = await onNewSummarizationJob(event).catch((err) =>
|
||||||
|
logJobError(JobTypes.Summarize, err)
|
||||||
|
)
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case JobTypes.SpeechToText: {
|
case JobTypes.SpeechToText: {
|
||||||
output = await speechToTextJob(event)
|
output = await speechToTextJob(event).catch((err) =>
|
||||||
|
logJobError(JobTypes.SpeechToText, err)
|
||||||
|
)
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case JobTypes.BlockChainBlockNumber: {
|
case JobTypes.BlockChainBlockNumber: {
|
||||||
output = await blockChainBlockNumberJob(event)
|
output = await blockChainBlockNumberJob(event).catch((err) =>
|
||||||
|
logJobError(JobTypes.BlockChainBlockNumber, err)
|
||||||
|
)
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
case JobTypes.RelayInfo: {
|
case JobTypes.RelayInfo: {
|
||||||
output = await relayInfoJob(event)
|
output = await relayInfoJob(event).catch((err) =>
|
||||||
|
logJobError(JobTypes.RelayInfo, err)
|
||||||
|
)
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -225,7 +246,9 @@ async function processJobEvent(event: NDKEvent, type: JobType): Promise<void> {
|
|||||||
complete(event, missingAmount(), { output })
|
complete(event, missingAmount(), { output })
|
||||||
}
|
}
|
||||||
|
|
||||||
await validateJobRequest(event)
|
await validateJobRequest(event).catch((err) =>
|
||||||
|
log(`Job validation error: `, err)
|
||||||
|
)
|
||||||
|
|
||||||
if (jobAmount > paidAmount && waitForPaymentBeforeProcessing()) {
|
if (jobAmount > paidAmount && waitForPaymentBeforeProcessing()) {
|
||||||
await reqPayment()
|
await reqPayment()
|
||||||
@ -234,9 +257,7 @@ async function processJobEvent(event: NDKEvent, type: JobType): Promise<void> {
|
|||||||
|
|
||||||
await startProcessing()
|
await startProcessing()
|
||||||
|
|
||||||
await publishStatus(event, 'finished').catch((err) =>
|
await publishStatus(event, 'finished').catch((err) => log('err :>> ', err))
|
||||||
console.log('err :>> ', err)
|
|
||||||
)
|
|
||||||
|
|
||||||
if (jobAmount > paidAmount && waitForPaymentBeforePublishingResult()) {
|
if (jobAmount > paidAmount && waitForPaymentBeforePublishingResult()) {
|
||||||
await reqPayment()
|
await reqPayment()
|
||||||
|
@ -2,13 +2,19 @@ import { NDKEvent, type NostrEvent } from '@nostr-dev-kit/ndk'
|
|||||||
import validateExpiration from './expiration.js'
|
import validateExpiration from './expiration.js'
|
||||||
import validateRequester from './requester.js'
|
import validateRequester from './requester.js'
|
||||||
import validateNoRecentResults from './no-recent-results.js'
|
import validateNoRecentResults from './no-recent-results.js'
|
||||||
import { ndk } from '../main.js'
|
import { ndk, log } from '../main.js'
|
||||||
import { addAmount } from '../job-types/speech-to-text.js'
|
import { addAmount } from '../job-types/speech-to-text.js'
|
||||||
|
|
||||||
export async function validateJobRequest(event: NDKEvent): Promise<void> {
|
export async function validateJobRequest(event: NDKEvent): Promise<void> {
|
||||||
await validateExpiration(event)
|
await validateExpiration(event).catch((err) => {
|
||||||
await validateRequester(event)
|
throw `Expiration error: ${err}`
|
||||||
await validateNoRecentResults(event)
|
})
|
||||||
|
await validateRequester(event).catch((err) => {
|
||||||
|
throw `Requester error: ${err}`
|
||||||
|
})
|
||||||
|
await validateNoRecentResults(event).catch((err) => {
|
||||||
|
throw `No recent results error: ${err}`
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function requirePayment(
|
export async function requirePayment(
|
||||||
@ -34,7 +40,8 @@ export async function requirePayment(
|
|||||||
payReq.tag(event, 'job')
|
payReq.tag(event, 'job')
|
||||||
|
|
||||||
await payReq.sign()
|
await payReq.sign()
|
||||||
console.log(payReq.rawEvent())
|
|
||||||
|
log(payReq.rawEvent())
|
||||||
|
|
||||||
if (publish !== false) await payReq.publish()
|
if (publish !== false) await payReq.publish()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user