arpit13 commited on
Commit
acc292a
·
verified ·
1 Parent(s): 063c7ed

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +119 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """
68
+ # 🔷 Ginie AI — Solidity Smart Contract Generator
69
+ Describe the contract you need in plain English. Ginie generates production-ready Solidity.
70
+ > ⚠️ Always review AI-generated contracts before deploying to mainnet.
71
+ """
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", # Gradio has no Solidity lexer; JS gives syntax highlighting
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
+ ---
115
+ Model: [GinieAI/Solidity-LLM](https://huggingface.co/GinieAI/Solidity-LLM) · Base: Chain-GPT/Solidity-LLM · License: MIT
116
+ """
117
+ )
118
+
119
+ demo.launch()