Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,24 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import requests
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
-
# Hugging Face Inference API configuration
|
| 6 |
-
HF_API_KEY = "your_huggingface_api_key" # Replace with your Hugging Face API key
|
| 7 |
-
HF_API_URL = f"https://api-inference.huggingface.co/models/codeparrot/codeparrot-small"
|
| 8 |
|
| 9 |
# Groq API configuration
|
| 10 |
-
GROQ_API_KEY = "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"
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Function to query Hugging Face Inference API
|
| 14 |
-
def query_huggingface(prompt):
|
| 15 |
-
try:
|
| 16 |
-
headers = {
|
| 17 |
-
"Authorization": f"Bearer {HF_API_KEY}",
|
| 18 |
-
"Content-Type": "application/json"
|
| 19 |
-
}
|
| 20 |
-
data = {
|
| 21 |
-
"inputs": prompt,
|
| 22 |
-
"parameters": {
|
| 23 |
-
"max_length": 150 # Limit the length of the generated text
|
| 24 |
-
}
|
| 25 |
-
}
|
| 26 |
-
response = requests.post(HF_API_URL, headers=headers, json=data, timeout=30) # Add timeout
|
| 27 |
-
response.raise_for_status() # Raise an error for bad responses (4xx, 5xx)
|
| 28 |
-
return response.json()[0]["generated_text"]
|
| 29 |
-
except Exception as e:
|
| 30 |
-
return f"Error querying Hugging Face API: {str(e)}"
|
| 31 |
|
| 32 |
# Function to query Groq API
|
| 33 |
def query_groq(prompt):
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
return response.json()["choices"][0]["text"]
|
| 46 |
except Exception as e:
|
| 47 |
return f"Error querying Groq API: {str(e)}"
|
| 48 |
|
|
@@ -52,17 +28,14 @@ def generate_smart_contract(language, requirements):
|
|
| 52 |
# Create a prompt for the model
|
| 53 |
prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
|
| 54 |
|
| 55 |
-
# Use
|
| 56 |
-
generated_code =
|
| 57 |
|
| 58 |
-
|
| 59 |
-
enhanced_code = query_groq(generated_code)
|
| 60 |
-
|
| 61 |
-
return enhanced_code
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error generating smart contract: {str(e)}"
|
| 64 |
|
| 65 |
-
# Custom CSS for a
|
| 66 |
custom_css = """
|
| 67 |
body {
|
| 68 |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
|
@@ -145,6 +118,19 @@ h1 {
|
|
| 145 |
.gradio-container {
|
| 146 |
animation: float 4s ease-in-out infinite;
|
| 147 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
"""
|
| 149 |
|
| 150 |
# Gradio interface for the app
|
|
@@ -154,18 +140,26 @@ def generate_contract(language, requirements):
|
|
| 154 |
# Dropdown options for programming languages
|
| 155 |
languages = ["Solidity", "Vyper", "Rust", "JavaScript", "Python"]
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
# Launch the Gradio app
|
| 170 |
-
|
| 171 |
-
interface.launch()
|
|
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from groq import Groq
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Groq API configuration
|
| 6 |
+
GROQ_API_KEY = "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" # Replace with your Groq API key
|
| 7 |
+
client = Groq(api_key=GROQ_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|
|
| 28 |
# Create a prompt for the model
|
| 29 |
prompt = f"Generate a {language} smart contract with the following requirements: {requirements}"
|
| 30 |
|
| 31 |
+
# Use Groq API to generate code
|
| 32 |
+
generated_code = query_groq(prompt)
|
| 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;
|
|
|
|
| 118 |
.gradio-container {
|
| 119 |
animation: float 4s ease-in-out infinite;
|
| 120 |
}
|
| 121 |
+
|
| 122 |
+
/* Dark box with green text for output */
|
| 123 |
+
.output-box {
|
| 124 |
+
background: #1e1e1e; /* Dark background */
|
| 125 |
+
padding: 15px;
|
| 126 |
+
border-radius: 10px;
|
| 127 |
+
color: #00ff00; /* Green text */
|
| 128 |
+
font-family: 'Courier New', Courier, monospace;
|
| 129 |
+
font-size: 14px;
|
| 130 |
+
line-height: 1.5;
|
| 131 |
+
overflow-x: auto;
|
| 132 |
+
white-space: pre-wrap; /* Preserve formatting */
|
| 133 |
+
}
|
| 134 |
"""
|
| 135 |
|
| 136 |
# Gradio interface for the app
|
|
|
|
| 140 |
# Dropdown options for programming languages
|
| 141 |
languages = ["Solidity", "Vyper", "Rust", "JavaScript", "Python"]
|
| 142 |
|
| 143 |
+
# Create a custom layout
|
| 144 |
+
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 |
+
output_box = gr.HTML(label="Generated Smart Contract", elem_classes="output-box")
|
| 150 |
+
|
| 151 |
+
# Input row
|
| 152 |
+
with gr.Row(equal_height=True, variant="panel"):
|
| 153 |
+
language_dropdown = gr.Dropdown(label="Programming Language", choices=languages, value="Solidity")
|
| 154 |
+
requirements_input = gr.Textbox(label="Requirements", placeholder="e.g., ERC20 token with minting functionality")
|
| 155 |
+
submit_button = gr.Button("Generate", variant="primary")
|
| 156 |
+
|
| 157 |
+
# Link the function to the inputs and output
|
| 158 |
+
submit_button.click(
|
| 159 |
+
generate_contract,
|
| 160 |
+
inputs=[language_dropdown, requirements_input],
|
| 161 |
+
outputs=output_box
|
| 162 |
+
)
|
| 163 |
|
| 164 |
# Launch the Gradio app
|
| 165 |
+
demo.launch()
|
|
|