Files changed (1) hide show
  1. app.py +87 -37
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("GinieAI/Solidity-LLM")
6
  model = AutoModelForCausalLM.from_pretrained(
7
- "GinieAI/Solidity-LLM",
8
- torch_dtype=torch.float32, # CPU-safe for Space free tier
 
9
  device_map="cpu"
10
  )
 
11
  print("Model ready!")
 
 
12
  def generate_contract(instruction, max_tokens=600, temperature=0.7):
13
- prompt = f"""### Instruction:
14
- {instruction}
15
- ### Response:
16
- """
17
- inputs = tokenizer(prompt, return_tensors="pt")
 
 
 
 
 
 
 
18
  with torch.no_grad():
19
  outputs = model.generate(
20
- **inputs,
21
- max_new_tokens=max_tokens,
22
- temperature=temperature,
23
- do_sample=True,
24
- pad_token_id=tokenizer.eos_token_id
 
 
25
  )
26
- result = tokenizer.decode(outputs, skip_special_tokens=True)
27
- # Return only the response part
28
- if "### Response:" in result:
29
- result = result.split("### Response:")[-1].strip()
30
- return result
 
 
 
 
31
  examples = [
32
- ["Write a Solidity ERC20 token contract with minting and burning."],
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
- with gr.Blocks(theme=gr.themes.Soft(), title="Ginie AI") as demo:
 
 
 
 
 
 
 
39
  gr.Markdown("""
40
- # 🧞 Ginie AI — Solidity Smart Contract Generator
41
- **The AI built for Web3 developers** · [ginie.xyz](https://ginie.xyz)
42
 
43
- Describe the smart contract you need in plain English. Ginie generates production-ready Solidity.
 
 
44
  """)
45
 
46
  with gr.Row():
47
  with gr.Column(scale=1):
48
  instruction = gr.Textbox(
49
- label="What contract do you need?",
50
  placeholder="e.g. Write a Solidity ERC20 token with minting, burning, and owner controls.",
51
- lines=4
52
  )
53
  with gr.Row():
54
- max_tokens = gr.Slider(100, 1000, value=600, label="Max tokens")
55
- temperature = gr.Slider(0.1, 1.0, value=0.7, label="Creativity")
56
- btn = gr.Button("🧞 Generate Contract", variant="primary")
 
 
 
 
 
 
57
 
58
  with gr.Column(scale=2):
59
- output = gr.Code(label="Generated Solidity", language="javascript")
 
 
 
 
 
 
 
 
 
 
 
60
 
61
- gr.Examples(examples=examples, inputs=instruction)
62
- btn.click(generate_contract, inputs=[instruction, max_tokens, temperature], outputs=output)
 
 
 
63
 
64
  gr.Markdown("""
65
- ---
66
- **⚠️ Always review AI-generated code before deploying to mainnet.**
67
- Model: [GinieAI/Solidity-LLM](https://huggingface.co/GinieAI/Solidity-LLM) · License: MIT
 
68
  """)
69
- demo.launch()
 
 
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()