import os import gradio as gr from groq import Groq # Load API key from Hugging Face Secrets GROQ_API_KEY = os.environ.get("blockchain") # Initialize Groq client client = Groq(api_key=GROQ_API_KEY) # ------------------------------- # Blockchain Query Function # ------------------------------- def blockchain_query(user_input): try: response = client.chat.completions.create( messages=[{"role": "user", "content": user_input}], model="llama-3.3-70b-versatile" ) return response.choices[0].message.content except Exception as e: return f"❌ Error: {e}" # ------------------------------- # Smart Contract Analyzer # ------------------------------- def analyze_contract(file_obj, analysis_type): if file_obj is None: return "Please upload a Solidity (.sol) file." try: content = file_obj.decode("utf-8") prompt = f""" You are a blockchain smart-contract auditor. Task: {analysis_type} Here is the Solidity contract: {content} Provide a detailed and expert-level response. """ response = client.chat.completions.create( messages=[{"role": "user", "content": prompt}], model="llama-3.3-70b-versatile" ) return response.choices[0].message.content except Exception as e: return f"❌ Error processing file: {e}" # ------------------------------- # Gradio UI (no theme -> HF safe) # ------------------------------- with gr.Blocks(title="CryptoSphere AI Agent") as interface: gr.Markdown( """ # 🔮 CryptoSphere AI ### Your AI Agent for Blockchain, Crypto & Smart Contract Insights Powered by **Groq + Llama 3.3 70B** """ ) with gr.Tab("💬 Blockchain Knowledge Assistant"): with gr.Row(): user_input = gr.Textbox( label="Ask anything about Blockchain, Crypto, Protocols, Web3...", placeholder="Example: What is slashing in Proof of Stake?" ) query_btn = gr.Button("Ask") output = gr.Markdown() query_btn.click(blockchain_query, inputs=user_input, outputs=output) with gr.Tab("📝 Smart Contract Analyzer"): with gr.Row(): upload_file = gr.File( label="Upload a .sol smart contract file", file_count="single", type="binary" ) analysis_mode = gr.Dropdown( choices=[ "Explain line-by-line", "Security audit", "Gas optimizations", "Summarize contract functionality" ], value="Security audit", label="Choose analysis type" ) analyze_btn = gr.Button("Analyze Contract") analysis_output = gr.Markdown() analyze_btn.click( analyze_contract, inputs=[upload_file, analysis_mode], outputs=analysis_output ) interface.launch()