Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
from llama_index import GPTListIndex, Document
|
| 4 |
+
|
| 5 |
+
# OpenAI API Key (Replace with env variable in production)
|
| 6 |
+
OPENAI_API_KEY = "your_openai_api_key_here"
|
| 7 |
+
openai.api_key = OPENAI_API_KEY
|
| 8 |
+
|
| 9 |
+
def analyze_smart_contract(code_snippet):
|
| 10 |
+
"""Analyzes the smart contract code for vulnerabilities using OpenAI."""
|
| 11 |
+
prompt = f"""
|
| 12 |
+
You are a smart contract security auditor. Analyze the following Solidity code and identify potential vulnerabilities. Suggest fixes where necessary.
|
| 13 |
+
|
| 14 |
+
Code:
|
| 15 |
+
{code_snippet}
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
response = openai.ChatCompletion.create(
|
| 19 |
+
model="gpt-4o-mini",
|
| 20 |
+
messages=[{"role": "system", "content": "You are a Solidity security expert."},
|
| 21 |
+
{"role": "user", "content": prompt}]
|
| 22 |
+
)
|
| 23 |
+
return response["choices"][0]["message"]["content"]
|
| 24 |
+
|
| 25 |
+
def human_review(contract_code, ai_analysis):
|
| 26 |
+
"""Integrates human-in-the-loop via LlamaIndex."""
|
| 27 |
+
document = Document(text=f"Smart Contract Code:\n{contract_code}\n\nAI Analysis:\n{ai_analysis}")
|
| 28 |
+
index = GPTListIndex.from_documents([document])
|
| 29 |
+
return index.query("Is the AI analysis correct? Provide feedback if needed.")
|
| 30 |
+
|
| 31 |
+
# Gradio UI
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("## 🔍 Smart Contract Auditor Assistant")
|
| 34 |
+
code_input = gr.Textbox(label="Paste your Solidity smart contract code", lines=10)
|
| 35 |
+
analyze_btn = gr.Button("Analyze Code")
|
| 36 |
+
ai_output = gr.Textbox(label="AI Analysis & Fixes")
|
| 37 |
+
human_review_btn = gr.Button("Request Human Review")
|
| 38 |
+
human_feedback = gr.Textbox(label="Human Expert Feedback")
|
| 39 |
+
|
| 40 |
+
analyze_btn.click(analyze_smart_contract, inputs=code_input, outputs=ai_output)
|
| 41 |
+
human_review_btn.click(human_review, inputs=[code_input, ai_output], outputs=human_feedback)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|