import gradio as gr import openai from llama_index import GPTListIndex, Document # OpenAI API Key (Replace with env variable in production) OPENAI_API_KEY = "your_openai_api_key_here" openai.api_key = OPENAI_API_KEY def analyze_smart_contract(code_snippet): """Analyzes the smart contract code for vulnerabilities using OpenAI.""" prompt = f""" You are a smart contract security auditor. Analyze the following Solidity code and identify potential vulnerabilities. Suggest fixes where necessary. Code: {code_snippet} """ response = openai.ChatCompletion.create( model="gpt-4o-mini", messages=[{"role": "system", "content": "You are a Solidity security expert."}, {"role": "user", "content": prompt}] ) return response["choices"][0]["message"]["content"] def human_review(contract_code, ai_analysis): """Integrates human-in-the-loop via LlamaIndex.""" document = Document(text=f"Smart Contract Code:\n{contract_code}\n\nAI Analysis:\n{ai_analysis}") index = GPTListIndex.from_documents([document]) return index.query("Is the AI analysis correct? Provide feedback if needed.") # Gradio UI with gr.Blocks() as demo: gr.Markdown("## 🔍 Smart Contract Auditor Assistant") code_input = gr.Textbox(label="Paste your Solidity smart contract code", lines=10) analyze_btn = gr.Button("Analyze Code") ai_output = gr.Textbox(label="AI Analysis & Fixes") human_review_btn = gr.Button("Request Human Review") human_feedback = gr.Textbox(label="Human Expert Feedback") analyze_btn.click(analyze_smart_contract, inputs=code_input, outputs=ai_output) human_review_btn.click(human_review, inputs=[code_input, ai_output], outputs=human_feedback) demo.launch()