File size: 3,646 Bytes
a209574
 
 
0eaa5e8
 
 
a209574
0eaa5e8
a209574
0eaa5e8
 
 
a209574
 
0eaa5e8
a209574
0eaa5e8
 
a209574
0eaa5e8
 
 
 
 
 
 
 
 
 
 
 
a209574
 
0eaa5e8
 
 
 
 
 
 
a209574
0eaa5e8
 
 
 
 
 
 
 
 
a209574
0eaa5e8
a209574
 
0eaa5e8
 
a209574
0eaa5e8
 
 
 
 
 
 
 
a209574
0eaa5e8
a209574
0eaa5e8
 
 
a209574
 
 
 
 
0eaa5e8
a209574
0eaa5e8
a209574
 
0eaa5e8
 
 
 
 
 
 
 
 
a209574
 
0eaa5e8
 
 
 
 
 
 
 
 
 
 
 
a209574
0eaa5e8
 
 
 
 
a209574
 
0eaa5e8
 
 
 
a209574
0eaa5e8
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

MODEL_ID = "GinieAI/Solidity-LLM"

print("Loading Ginie Solidity LLM...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.float32,
    low_cpu_mem_usage=True,
    device_map="cpu"
)
model.eval()
print("Model ready!")


def generate_contract(instruction, max_tokens=600, temperature=0.7):
    if not instruction.strip():
        return "Please enter a contract description."

    prompt = f"### Instruction:\n{instruction.strip()}\n\n### Response:\n"

    inputs = tokenizer(
        prompt,
        return_tensors="pt",
        truncation=True,
        max_length=512
    )

    with torch.no_grad():
        outputs = model.generate(
            input_ids=inputs["input_ids"],
            attention_mask=inputs["attention_mask"],
            max_new_tokens=int(max_tokens),
            temperature=float(temperature),
            do_sample=temperature > 0,
            pad_token_id=tokenizer.eos_token_id,
            eos_token_id=tokenizer.eos_token_id,
        )

    generated = tokenizer.decode(
        outputs[0][inputs["input_ids"].shape[1]:],
        skip_special_tokens=True
    ).strip()

    return generated


examples = [
    ["Write a Solidity ERC20 token with minting, burning, and owner controls."],
    ["Write a Solidity multisig wallet requiring 2 of 3 signatures."],
    ["Write a Solidity staking contract with 10% APY rewards."],
    ["Write a Solidity DAO governance contract with proposal voting."],
    ["Write a Solidity NFT contract with whitelist minting and reveal."],
]

css = """
.output-code { font-family: monospace; font-size: 13px; }
footer { display: none !important; }
"""

with gr.Blocks(theme=gr.themes.Soft(), title="Ginie AI — Smart Contract Generator", css=css) as demo:

    gr.Markdown("""
# Ginie AI — Solidity Smart Contract Generator

The AI built for Web3 developers · [ginie.xyz](https://ginie.xyz) · [npm SDK](https://npmjs.com/package/ginie-sdk) — 30k+ weekly downloads

Describe the contract you need in plain English. Ginie generates production-ready Solidity.
    """)

    with gr.Row():
        with gr.Column(scale=1):
            instruction = gr.Textbox(
                label="Describe your contract",
                placeholder="e.g. Write a Solidity ERC20 token with minting, burning, and owner controls.",
                lines=5
            )
            with gr.Row():
                max_tokens = gr.Slider(
                    minimum=100, maximum=800, value=600, step=50,
                    label="Max tokens"
                )
                temperature = gr.Slider(
                    minimum=0.1, maximum=1.0, value=0.7, step=0.1,
                    label="Temperature"
                )
            btn = gr.Button("Generate Contract", variant="primary", size="lg")

        with gr.Column(scale=2):
            output = gr.Code(
                label="Generated Solidity",
                language="javascript",
                lines=30,
                elem_classes=["output-code"]
            )

    gr.Examples(
        examples=examples,
        inputs=instruction,
        label="Example prompts"
    )

    btn.click(
        fn=generate_contract,
        inputs=[instruction, max_tokens, temperature],
        outputs=output
    )

    gr.Markdown("""
---
Model: [GinieAI/Solidity-LLM](https://huggingface.co/GinieAI/Solidity-LLM) · Base: Chain-GPT/Solidity-LLM · License: MIT

> Always review AI-generated contracts before deploying to mainnet.
    """)

demo.launch()