File size: 3,054 Bytes
c51ab5a
 
 
 
 
0d0b1e6
c51ab5a
 
863f80a
c51ab5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11a1f9e
c51ab5a
11a1f9e
c51ab5a
 
 
 
 
 
 
 
 
 
 
11a1f9e
 
 
 
c51ab5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
214562b
 
11a1f9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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()