Skip to content

Commit

Permalink
add tutorials, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tintinweb committed Nov 12, 2021
1 parent 076718d commit f7d99a7
Show file tree
Hide file tree
Showing 3 changed files with 323 additions and 5 deletions.
86 changes: 81 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,90 @@
* And then "hallucinate" new contracts


## Contents

* `./solidity_model_text/` - contains a pre-trained keras model trained on 15mb solidity input, character based training (pretty dumb), with sampling sequence length of 250 chars. The model has an `embedding_dimension` of `256` with `1024` `rnn_units`. It was trained for `15 epochs` on google collab (hw-accelleration: `GPU`) which took somewhere between 1-1.5 hrs.
* [./tutorial_2_hallucinate_from_pretrained_model.ipynb](./tutorial_2_hallucinate_from_pretrained_model.ipynb) - loads the pre-trained model from `./solidity_model_text` to hallucinate more solidity.
* [./tutorial_1_train_and_hallucinate_save_restore_continue_training.ipynb](./tutorial_1_train_and_hallucinate_save_restore_continue_training.ipynb) - is the code that downloads samples from https://github.com/tintinweb/smart-contract-sanctuary, creates the model, trains it, hallucinates some text, and then continues to show how to save/restore/re-train the model. Ultimately, the model is exported and converted to [tensorflow.js](https://www.tensorflow.org/js) conform format so that it can be used with any javascript/web-front/backend.

## Demo

TBD
Copy the two tutorials to your google drive and run them.

```python
print(trainingData.predict(['contract '], 3000))
```

## Tour
```solidity
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
contract Parminicinvition is Ownable {
using SafeMath for uint256;
enum State { Approve = token.totalSupply();
require(tokens >= summaryTokens.add(bonus));
totalDailydested = totalEthInWei + msg.value;
totalSoldTokens = token.totalSupply();
emit Transfer(address(0), 0xCf49B9298aC4d4933a7D6984d89A49aDc84A6CA602BA513D872C3,21f36325D28718](0));
totalSupply = totalSupply.mul(totalValue.add(soldSignedMap[tokensBough.mul(1)));
restributedPluyRates[msg.sender] = true;
nonStokenSupplyFinallow
}
if(opits[msg.sender].amount <= totalSupply)) ether;
}
assignOpe(address(this).balance, weiAmount);
require(canTra_secrecover(_approved) >= rNo(_reward, _weight, _amount);
totalAmount = totalAmount.add(_amount);
Transfer(_addr, msg.sender, amount);
}
function checkRollUnerad(address _caller) external {
require(activeTransieronase / _values[i] >= eth[i] && _patent.beneficiary != address(0));
require(_amountOfTokens <= tokenBalanceLedger_[_customerAddress]);
uint256 _tokens = _amountOfTokens;
uint256 _ethereum = tokensToEthereum_(_tokens, _taxedEthereum);
uint256 _divTokens = getToken().balanceOf(_owner);
require(_tokenAddress >= _value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
}
function multiSigWallet(address[] _KeceiveAddress(address _whenAddress) onlyOwner public {
stats[msg.sender][_i] = _getUnlockedAmt;
}
function enableKYC() external onlyOwner beforeURe {
uint256 lastIndex = 0;
address _tokenAddress indexe msg.sender;
uint128 accountKey = uint128(_affidiets, userTrustedContribution);
assignUpgredateTokens(_address, _beneficiever, _tokens);
}
function setRestrictAddressLass() onlyOwnerships onlyWhitelisted r
uint256 _remainingTokens =
(_tokenPriceInitial * tokenPriceIncremental_ * (_tokenSupply/1e18);
uint depositAmount = deadline );
}
isReleaseAgent = addr;
}
function paymentSuspendedPaymentsale(uint256[] _users, uint256[] _scriptLogoAssess) public payable returns(uint256[12] _eth) {
k.cards[0x343C2593c3216d33433D43fBBF6cF7 = txp1 * item_iXCalculated[_userNumber].enity_inceent, 0.0000001 ether, other.umLock());
}
function buyTokens(address _address) external onlyWhileOter{
uint256 inReadyToBivs;
totalSupply = totalSupply.sub(_amount);
DistrustedContributionTime = _block.pixe
```

TBD

## Develop
## Credits

TBD
Based on the [TensorFlow Text Generation Tutorial](https://www.tensorflow.org/text/tutorials/text_generation)
File renamed without changes.
242 changes: 242 additions & 0 deletions tutorial_2_hallucinate_from_pretrained_model.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "tutorial-2-hallucinate-from-pretrained-model.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Vycsyjqdsk6D"
},
"source": [
"[<img width=\"200\" alt=\"get in touch with Consensys Diligence\" src=\"https://user-images.githubusercontent.com/2865694/56826101-91dcf380-685b-11e9-937c-af49c2510aa0.png\">](https://diligence.consensys.net)<br/>\n",
"<sup>\n",
"[[ 🌐 ](https://diligence.consensys.net) [ 📩 ](https://github.com/ConsenSys/vscode-solidity-doppelganger/blob/master/mailto:[email protected]) [ 🔥 ](https://consensys.github.io/diligence/)]\n",
"</sup><br/><br/>\n",
"\n",
"\n",
"# Hallucinate.sol - from pre-trained model"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qccfvi1fkbNK"
},
"source": [
"## Initialization magic\n",
"\n",
"We're training on google collab because it is much faster than on a normal pc. For this, we'll want to make the modules that download the source-samples and thrain the RNN available on google drive.\n",
"\n",
"1. copy the files from https://github.com/tintinweb/hallucinate.sol to your personal google drive into `/MyDrive/collab/solidity-gen`\n",
"2. run the next two steps and provide your authenticator token \n"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OpWgMJEZ9wz7"
},
"source": [
"%tensorflow_version 2.x"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "H7ZRJKel9LaW",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "7c69d1a2-d50f-4375-e331-369ea5e01dbc"
},
"source": [
"import os\n",
"\n",
"# (a) mount the google drive in order for the code to find the soliditygen module\n",
"\n",
"from google.colab import drive\n",
"drive.mount('/content/drive', force_remount=True)\n",
"os.chdir('/content/drive/MyDrive/collab/solidity-gen')\n",
"!ls -lsat '/content/drive/MyDrive/collab/solidity-gen'\n",
"\n",
"\"\"\"\n",
"# (b) checkout the repo instead\n",
"\n",
"!git clone https://github.com/tintinweb/hallucinate.sol.git\n",
"os.chdir(\"hallucinate.sol\")\n",
"\"\"\"\n",
"\n",
"### import everything because we're lazy\n",
"from soliditygen import *"
],
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n",
"total 87\n",
" 3 -rw------- 1 root root 2868 Nov 12 10:59 tutorial-2-hallucinate-from-pretrained-model.ipynb\n",
" 1 -rw------- 1 root root 103 Nov 12 10:41 README.md\n",
"57 -rw------- 1 root root 57752 Nov 12 10:38 tutorial-1-train_and_predict.ipynb\n",
" 4 drwx------ 2 root root 4096 Nov 12 10:33 solidity_model_text\n",
" 9 -rw------- 1 root root 8299 Nov 12 10:27 soliditygen.py\n",
" 2 -rw------- 1 root root 1610 Nov 12 09:48 .gitignore\n",
" 4 drwx------ 2 root root 4096 Nov 12 09:24 .git\n",
" 4 drwx------ 2 root root 4096 Nov 10 10:40 training_checkpoints\n",
" 4 drwx------ 2 root root 4096 Nov 10 09:32 __pycache__\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vEAIKCt3naTI"
},
"source": [
"## Load an existing model\n",
"\n",
"Restore the saved state from local folder `solidity_model`, then, hallucinate more solidity code 🙌"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Elf61diwlIPn"
},
"source": [
"### creat dummy trainingData object and load existing state from the local dir\n",
"trainingData = TrainingData()\n",
"trainingData.load_model(\"solidity_model_text\") # location of model\n"
],
"execution_count": 17,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "dfzpTwkTsUk-"
},
"source": [
"## Predict \n",
"\n",
"Hallucinate more solidity 🙌"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NzWXr0nXsTuI",
"outputId": "ce20954a-067a-46de-c204-ef2bafee84fa"
},
"source": [
"start = time.time()\n",
"textOut = trainingData.predict(['contract '], 3000)\n",
"print(textOut, '\\n\\n' + '_'*80)\n",
"end = time.time()\n",
"print('\\nRun time:', end - start)"
],
"execution_count": 18,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"contract Ownable {\n",
" address public owner;\n",
" event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);\n",
" function Ownable() public {\n",
" owner = msg.sender;\n",
" }\n",
" modifier onlyOwner() {\n",
" require(msg.sender == owner);\n",
" _;\n",
" }\n",
" function transferOwnership(address newOwner) public onlyOwner {\n",
" require(newOwner != address(0));\n",
" emit OwnershipTransferred(owner, newOwner);\n",
" owner = newOwner;\n",
" }\n",
"}\n",
"contract Parminicinvition is Ownable {\n",
" using SafeMath for uint256;\n",
" enum State { Approve = token.totalSupply();\n",
" require(tokens >= summaryTokens.add(bonus));\n",
" totalDailydested = totalEthInWei + msg.value;\n",
" totalSoldTokens = token.totalSupply();\n",
" emit Transfer(address(0), 0xCf49B9298aC4d4933a7D6984d89A49aDc84A6CA602BA513D872C3,21f36325D28718](0));\n",
" totalSupply = totalSupply.mul(totalValue.add(soldSignedMap[tokensBough.mul(1)));\n",
" restributedPluyRates[msg.sender] = true;\n",
" nonStokenSupplyFinallow\n",
" }\n",
" if(opits[msg.sender].amount <= totalSupply)) ether;\n",
"\t\t\t}\n",
"\t\tassignOpe(address(this).balance, weiAmount);\n",
"\t\trequire(canTra_secrecover(_approved) >= rNo(_reward, _weight, _amount);\n",
"\t totalAmount = totalAmount.add(_amount);\n",
" Transfer(_addr, msg.sender, amount);\n",
" }\n",
" function checkRollUnerad(address _caller) external {\n",
" require(activeTransieronase / _values[i] >= eth[i] && _patent.beneficiary != address(0));\n",
" require(_amountOfTokens <= tokenBalanceLedger_[_customerAddress]);\n",
" uint256 _tokens = _amountOfTokens;\n",
" uint256 _ethereum = tokensToEthereum_(_tokens, _taxedEthereum);\n",
" uint256 _divTokens = getToken().balanceOf(_owner);\n",
" require(_tokenAddress >= _value);\n",
" allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);\n",
" }\n",
" function multiSigWallet(address[] _KeceiveAddress(address _whenAddress) onlyOwner public {\n",
" stats[msg.sender][_i] = _getUnlockedAmt;\n",
" }\n",
" function enableKYC() external onlyOwner beforeURe {\n",
" uint256 lastIndex = 0;\n",
" address _tokenAddress indexe msg.sender;\n",
" uint128 accountKey = uint128(_affidiets, userTrustedContribution);\n",
" assignUpgredateTokens(_address, _beneficiever, _tokens);\n",
" }\n",
" function setRestrictAddressLass() onlyOwnerships onlyWhitelisted r\n",
" uint256 _remainingTokens = \n",
" (_tokenPriceInitial * tokenPriceIncremental_ * (_tokenSupply/1e18);\n",
" uint depositAmount = deadline );\n",
" }\n",
" isReleaseAgent = addr;\n",
" }\n",
" function paymentSuspendedPaymentsale(uint256[] _users, uint256[] _scriptLogoAssess) public payable returns(uint256[12] _eth) {\n",
" k.cards[0x343C2593c3216d33433D43fBBF6cF7 = txp1 * item_iXCalculated[_userNumber].enity_inceent, 0.0000001 ether, other.umLock());\n",
" }\n",
" function buyTokens(address _address) external onlyWhileOter{\n",
" uint256 inReadyToBivs;\n",
" totalSupply = totalSupply.sub(_amount);\n",
" DistrustedContributionTime = _block.pixe \n",
"\n",
"________________________________________________________________________________\n",
"\n",
"Run time: 16.05377769470215\n"
]
}
]
}
]
}

0 comments on commit f7d99a7

Please sign in to comment.