Spaces:
Build error
Build error
Fixed the bugs
#2
by SatyamSinghal - opened
app.py
CHANGED
|
@@ -1,69 +1,119 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
| 4 |
print("Loading Ginie Solidity LLM...")
|
| 5 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 6 |
model = AutoModelForCausalLM.from_pretrained(
|
| 7 |
-
|
| 8 |
-
torch_dtype=torch.float32,
|
|
|
|
| 9 |
device_map="cpu"
|
| 10 |
)
|
|
|
|
| 11 |
print("Model ready!")
|
|
|
|
|
|
|
| 12 |
def generate_contract(instruction, max_tokens=600, temperature=0.7):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
""
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
with torch.no_grad():
|
| 19 |
outputs = model.generate(
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
examples = [
|
| 32 |
-
["Write a Solidity ERC20 token
|
| 33 |
["Write a Solidity multisig wallet requiring 2 of 3 signatures."],
|
| 34 |
["Write a Solidity staking contract with 10% APY rewards."],
|
| 35 |
-
["Write a Solidity DAO governance contract with voting."],
|
| 36 |
-
["Write a Solidity NFT contract with whitelist minting."],
|
| 37 |
]
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
gr.Markdown("""
|
| 40 |
-
|
| 41 |
-
**The AI built for Web3 developers** · [ginie.xyz](https://ginie.xyz)
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
""")
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
with gr.Column(scale=1):
|
| 48 |
instruction = gr.Textbox(
|
| 49 |
-
label="
|
| 50 |
placeholder="e.g. Write a Solidity ERC20 token with minting, burning, and owner controls.",
|
| 51 |
-
lines=
|
| 52 |
)
|
| 53 |
with gr.Row():
|
| 54 |
-
max_tokens = gr.Slider(
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
with gr.Column(scale=2):
|
| 59 |
-
output = gr.Code(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
gr.Markdown("""
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
""")
|
| 69 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
MODEL_ID = "GinieAI/Solidity-LLM"
|
| 6 |
+
|
| 7 |
print("Loading Ginie Solidity LLM...")
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
torch_dtype=torch.float32,
|
| 12 |
+
low_cpu_mem_usage=True,
|
| 13 |
device_map="cpu"
|
| 14 |
)
|
| 15 |
+
model.eval()
|
| 16 |
print("Model ready!")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
def generate_contract(instruction, max_tokens=600, temperature=0.7):
|
| 20 |
+
if not instruction.strip():
|
| 21 |
+
return "Please enter a contract description."
|
| 22 |
+
|
| 23 |
+
prompt = f"### Instruction:\n{instruction.strip()}\n\n### Response:\n"
|
| 24 |
+
|
| 25 |
+
inputs = tokenizer(
|
| 26 |
+
prompt,
|
| 27 |
+
return_tensors="pt",
|
| 28 |
+
truncation=True,
|
| 29 |
+
max_length=512
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
with torch.no_grad():
|
| 33 |
outputs = model.generate(
|
| 34 |
+
input_ids=inputs["input_ids"],
|
| 35 |
+
attention_mask=inputs["attention_mask"],
|
| 36 |
+
max_new_tokens=int(max_tokens),
|
| 37 |
+
temperature=float(temperature),
|
| 38 |
+
do_sample=temperature > 0,
|
| 39 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 40 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
generated = tokenizer.decode(
|
| 44 |
+
outputs[0][inputs["input_ids"].shape[1]:],
|
| 45 |
+
skip_special_tokens=True
|
| 46 |
+
).strip()
|
| 47 |
+
|
| 48 |
+
return generated
|
| 49 |
+
|
| 50 |
+
|
| 51 |
examples = [
|
| 52 |
+
["Write a Solidity ERC20 token with minting, burning, and owner controls."],
|
| 53 |
["Write a Solidity multisig wallet requiring 2 of 3 signatures."],
|
| 54 |
["Write a Solidity staking contract with 10% APY rewards."],
|
| 55 |
+
["Write a Solidity DAO governance contract with proposal voting."],
|
| 56 |
+
["Write a Solidity NFT contract with whitelist minting and reveal."],
|
| 57 |
]
|
| 58 |
+
|
| 59 |
+
css = """
|
| 60 |
+
.output-code { font-family: monospace; font-size: 13px; }
|
| 61 |
+
footer { display: none !important; }
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Ginie AI — Smart Contract Generator", css=css) as demo:
|
| 65 |
+
|
| 66 |
gr.Markdown("""
|
| 67 |
+
# Ginie AI — Solidity Smart Contract Generator
|
|
|
|
| 68 |
|
| 69 |
+
The AI built for Web3 developers · [ginie.xyz](https://ginie.xyz) · [npm SDK](https://npmjs.com/package/ginie-sdk) — 30k+ weekly downloads
|
| 70 |
+
|
| 71 |
+
Describe the contract you need in plain English. Ginie generates production-ready Solidity.
|
| 72 |
""")
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
with gr.Column(scale=1):
|
| 76 |
instruction = gr.Textbox(
|
| 77 |
+
label="Describe your contract",
|
| 78 |
placeholder="e.g. Write a Solidity ERC20 token with minting, burning, and owner controls.",
|
| 79 |
+
lines=5
|
| 80 |
)
|
| 81 |
with gr.Row():
|
| 82 |
+
max_tokens = gr.Slider(
|
| 83 |
+
minimum=100, maximum=800, value=600, step=50,
|
| 84 |
+
label="Max tokens"
|
| 85 |
+
)
|
| 86 |
+
temperature = gr.Slider(
|
| 87 |
+
minimum=0.1, maximum=1.0, value=0.7, step=0.1,
|
| 88 |
+
label="Temperature"
|
| 89 |
+
)
|
| 90 |
+
btn = gr.Button("Generate Contract", variant="primary", size="lg")
|
| 91 |
|
| 92 |
with gr.Column(scale=2):
|
| 93 |
+
output = gr.Code(
|
| 94 |
+
label="Generated Solidity",
|
| 95 |
+
language="javascript",
|
| 96 |
+
lines=30,
|
| 97 |
+
elem_classes=["output-code"]
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
gr.Examples(
|
| 101 |
+
examples=examples,
|
| 102 |
+
inputs=instruction,
|
| 103 |
+
label="Example prompts"
|
| 104 |
+
)
|
| 105 |
|
| 106 |
+
btn.click(
|
| 107 |
+
fn=generate_contract,
|
| 108 |
+
inputs=[instruction, max_tokens, temperature],
|
| 109 |
+
outputs=output
|
| 110 |
+
)
|
| 111 |
|
| 112 |
gr.Markdown("""
|
| 113 |
+
---
|
| 114 |
+
Model: [GinieAI/Solidity-LLM](https://huggingface.co/GinieAI/Solidity-LLM) · Base: Chain-GPT/Solidity-LLM · License: MIT
|
| 115 |
+
|
| 116 |
+
> Always review AI-generated contracts before deploying to mainnet.
|
| 117 |
""")
|
| 118 |
+
|
| 119 |
+
demo.launch()
|