forked from Lilypad-Tech/lilypad-v0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStableDiffusionCaller.sol
77 lines (64 loc) · 2.58 KB
/
StableDiffusionCaller.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.4;
import "hardhat/console.sol";
import "lilypad/contracts/LilypadEvents.sol";
import "lilypad/contracts/LilypadCallerInterface.sol";
/**
@notice An experimental contract for POC work to call Bacalhau jobs from FVM smart contracts
*/
contract StableDiffusionCaller is LilypadCallerInterface {
LilypadEvents public bridge;
struct StableDiffusionImage {
string prompt;
string ipfsResult;
}
StableDiffusionImage[] public images;
mapping (uint => string) prompts;
event NewImageGenerated(StableDiffusionImage image);
constructor(address bridgeContract) {
console.log("Deploying StableDiffusion contract");
setLPEventsAddress(bridgeContract);
}
function setLPEventsAddress(address _eventsAddress) public {
bridge = LilypadEvents(_eventsAddress);
}
string constant specStart = '{'
'"Engine": "docker",'
'"Verifier": "noop",'
'"Publisher": "estuary",'
'"Docker": {'
'"Image": "ghcr.io/bacalhau-project/examples/stable-diffusion-gpu:0.0.1",'
'"Entrypoint": ["python", "main.py", "--o", "./outputs", "--p", "';
string constant specEnd =
'"]},'
'"Resources": {"GPU": "1"},'
'"Outputs": [{"Name": "outputs", "Path": "/outputs"}],'
'"Deal": {"Concurrency": 1}'
'}';
function StableDiffusion(string calldata _prompt) external {
// TODO: do proper json encoding, look out for quotes in _prompt
string memory spec = string.concat(specStart, _prompt, specEnd);
uint id = bridge.runBacalhauJob(address(this), spec, LilypadResultType.CID);
prompts[id] = _prompt;
}
function allImages() public view returns (StableDiffusionImage[] memory) {
return images;
}
function lilypadFulfilled(address _from, uint _jobId, LilypadResultType _resultType, string calldata _result) external override {
//need some checks here that it a legitimate result
require(_from == address(bridge)); //really not secure
require(_resultType == LilypadResultType.CID);
StableDiffusionImage memory image = StableDiffusionImage({
ipfsResult: _result,
prompt: prompts[_jobId]
});
images.push(image);
emit NewImageGenerated(image);
delete prompts[_jobId];
}
function lilypadCancelled(address _from, uint _jobId, string calldata _errorMsg) external override {
require(_from == address(bridge)); //really not secure
console.log(_errorMsg);
delete prompts[_jobId];
}
}