Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,12 @@
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
3 |
-
from
|
|
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
# Function to query Groq API
|
10 |
-
def query_groq(prompt):
|
11 |
-
try:
|
12 |
-
chat_completion = client.chat.completions.create(
|
13 |
-
messages=[
|
14 |
-
{
|
15 |
-
"role": "user",
|
16 |
-
"content": prompt,
|
17 |
-
}
|
18 |
-
],
|
19 |
-
model="llama-3.3-70b-versatile", # Use the correct model
|
20 |
-
)
|
21 |
-
return chat_completion.choices[0].message.content
|
22 |
-
except Exception as e:
|
23 |
-
return f"Error querying Groq API: {str(e)}"
|
24 |
|
25 |
# Function to generate smart contract code
|
26 |
def generate_smart_contract(language, requirements):
|
@@ -28,14 +14,18 @@ def generate_smart_contract(language, requirements):
|
|
28 |
# Create a prompt for the model
|
29 |
prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
|
30 |
|
31 |
-
#
|
32 |
-
|
|
|
|
|
|
|
|
|
33 |
|
34 |
return generated_code
|
35 |
except Exception as e:
|
36 |
return f"Error generating smart contract: {str(e)}"
|
37 |
|
38 |
-
# Custom CSS for a dark box with green text
|
39 |
custom_css = """
|
40 |
body {
|
41 |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
@@ -130,6 +120,37 @@ h1 {
|
|
130 |
line-height: 1.5;
|
131 |
overflow-x: auto;
|
132 |
white-space: pre-wrap; /* Preserve formatting */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
}
|
134 |
"""
|
135 |
|
@@ -145,8 +166,10 @@ with gr.Blocks(css=custom_css) as demo:
|
|
145 |
gr.Markdown("# Smart Contract Generator")
|
146 |
gr.Markdown("Generate smart contracts using AI.")
|
147 |
|
148 |
-
# Output box
|
149 |
-
|
|
|
|
|
150 |
|
151 |
# Input row
|
152 |
with gr.Row(equal_height=True, variant="panel"):
|
@@ -160,6 +183,9 @@ with gr.Blocks(css=custom_css) as demo:
|
|
160 |
inputs=[language_dropdown, requirements_input],
|
161 |
outputs=output_box
|
162 |
)
|
|
|
|
|
|
|
163 |
|
164 |
# Launch the Gradio app
|
165 |
demo.launch()
|
|
|
1 |
# app.py
|
2 |
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
import torch
|
5 |
|
6 |
+
# Load the CodeParrot model and tokenizer
|
7 |
+
model_name = "codeparrot/codeparrot-small" # Replace with your desired model
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
# Function to generate smart contract code
|
12 |
def generate_smart_contract(language, requirements):
|
|
|
14 |
# Create a prompt for the model
|
15 |
prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
|
16 |
|
17 |
+
# Tokenize the input
|
18 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
19 |
+
|
20 |
+
# Generate code
|
21 |
+
outputs = model.generate(**inputs, max_length=500) # Adjust max_length as needed
|
22 |
+
generated_code = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
|
24 |
return generated_code
|
25 |
except Exception as e:
|
26 |
return f"Error generating smart contract: {str(e)}"
|
27 |
|
28 |
+
# Custom CSS for a dark box with green text and copy button
|
29 |
custom_css = """
|
30 |
body {
|
31 |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
|
|
120 |
line-height: 1.5;
|
121 |
overflow-x: auto;
|
122 |
white-space: pre-wrap; /* Preserve formatting */
|
123 |
+
position: relative;
|
124 |
+
}
|
125 |
+
|
126 |
+
/* Copy button */
|
127 |
+
.copy-button {
|
128 |
+
position: absolute;
|
129 |
+
top: 10px;
|
130 |
+
right: 10px;
|
131 |
+
background: #2575fc;
|
132 |
+
border: none;
|
133 |
+
border-radius: 5px;
|
134 |
+
color: #fff;
|
135 |
+
padding: 5px 10px;
|
136 |
+
font-size: 12px;
|
137 |
+
cursor: pointer;
|
138 |
+
transition: background 0.3s ease;
|
139 |
+
}
|
140 |
+
|
141 |
+
.copy-button:hover {
|
142 |
+
background: #6a11cb;
|
143 |
+
}
|
144 |
+
"""
|
145 |
+
|
146 |
+
# JavaScript for the copy button
|
147 |
+
copy_js = """
|
148 |
+
function copyCode() {
|
149 |
+
const codeElement = document.querySelector('.output-box pre');
|
150 |
+
const codeText = codeElement.innerText;
|
151 |
+
navigator.clipboard.writeText(codeText).then(() => {
|
152 |
+
alert('Code copied to clipboard!');
|
153 |
+
});
|
154 |
}
|
155 |
"""
|
156 |
|
|
|
166 |
gr.Markdown("# Smart Contract Generator")
|
167 |
gr.Markdown("Generate smart contracts using AI.")
|
168 |
|
169 |
+
# Output box with copy button
|
170 |
+
with gr.Column():
|
171 |
+
output_box = gr.HTML(label="Generated Smart Contract", elem_classes="output-box")
|
172 |
+
copy_button = gr.Button("Copy Code", elem_classes="copy-button")
|
173 |
|
174 |
# Input row
|
175 |
with gr.Row(equal_height=True, variant="panel"):
|
|
|
183 |
inputs=[language_dropdown, requirements_input],
|
184 |
outputs=output_box
|
185 |
)
|
186 |
+
|
187 |
+
# Add JavaScript for the copy button
|
188 |
+
demo.load(fn=None, inputs=None, outputs=None, _js=copy_js)
|
189 |
|
190 |
# Launch the Gradio app
|
191 |
demo.launch()
|