xyizko commited on
Commit
453e6b0
·
verified ·
1 Parent(s): b8db9ab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -0
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ from smolagents import CodeAgent, Tool, HfApiModel
4
+
5
+ # Fetch the API data once at startup
6
+ API_URL = "https://dt074px2e9qbh.cloudfront.net/exploit-api.json"
7
+ try:
8
+ response = requests.get(API_URL)
9
+ flagged_addresses = response.json()
10
+ except Exception as e:
11
+ flagged_addresses = []
12
+ print(f"Failed to load API data: {e}")
13
+
14
+ # Function to infer chain from address (basic heuristics)
15
+ def infer_chain(address):
16
+ address = address.lower()
17
+ if address.startswith("0x") and len(address) == 42:
18
+ return "Ethereum"
19
+ elif address.startswith("bc1") or address.startswith("1") or address.startswith("3"):
20
+ return "Bitcoin"
21
+ elif address.startswith("T") and len(address) == 34:
22
+ return "Tron"
23
+ else:
24
+ return "Unknown"
25
+
26
+ # Tool to check if an address is flagged
27
+ def check_flagged_address(address: str) -> str:
28
+ """Checks if a cryptocurrency address is flagged in the Bybit hack database."""
29
+ chain = infer_chain(address)
30
+ for entry in flagged_addresses:
31
+ if isinstance(entry, dict) and "address" in entry:
32
+ if entry["address"].lower() == address.lower():
33
+ chain = entry.get("chain", chain) # Use API chain if available
34
+ return f"Address {address} is flagged as part of the Bybit hack on {chain}."
35
+ return f"Address {address} is not flagged as part of the Bybit hack (inferred chain: {chain})."
36
+
37
+ # Define the tool for the agent
38
+ flagged_address_tool = Tool(
39
+ name="check_flagged_address",
40
+ description="Checks if a cryptocurrency address is flagged in the Bybit hack database.",
41
+ fn=check_flagged_address
42
+ )
43
+
44
+ # Gradio chat function
45
+ def chat_with_agent(user_input, chat_history, hf_token):
46
+ if not chat_history:
47
+ chat_history = []
48
+
49
+ # Check if token is provided
50
+ if not hf_token or hf_token.strip() == "":
51
+ return chat_history + [[user_input, "Please enter a valid Hugging Face API token."]], ""
52
+
53
+ # Initialize the CodeAgent with the user-provided HF token
54
+ agent = CodeAgent(
55
+ model=HfApiModel(model_id="mistralai/Mixtral-8x7B-Instruct-v0.1", token=hf_token),
56
+ tools=[flagged_address_tool],
57
+ system_prompt="You are an agent that checks if cryptocurrency addresses are flagged as part of the Bybit hack. Given an address, use the check_flagged_address tool to verify its status and report the result, including the blockchain it's on."
58
+ )
59
+
60
+ # Run the agent with the user's input
61
+ try:
62
+ response = agent.run(user_input)
63
+ except Exception as e:
64
+ response = f"Error: {str(e)}. Check your HF token or try again."
65
+
66
+ # Append as a [user, bot] pair for Chatbot
67
+ chat_history.append([user_input, response])
68
+
69
+ # Return updated history and clear the input box
70
+ return chat_history, ""
71
+
72
+ # Create the Gradio interface with tabs
73
+ with gr.Blocks(title="Bybit Hack Address Checker") as demo:
74
+ gr.Markdown("# Bybit Hack Address Checker")
75
+
76
+ with gr.Tabs():
77
+ # Tab 1: Address Checker UI
78
+ with gr.Tab(label="Check Address"):
79
+ gr.Markdown("Enter a cryptocurrency address to check if it's flagged in the Bybit hack database and identify its blockchain.")
80
+ gr.Markdown("Provide your Hugging Face API token below (get it from [huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)).")
81
+
82
+ # HF Token input
83
+ hf_token_input = gr.Textbox(
84
+ placeholder="Enter your Hugging Face API token here",
85
+ label="Hugging Face API Token",
86
+ type="password",
87
+ lines=1
88
+ )
89
+
90
+ # Chatbot component
91
+ chatbot = gr.Chatbot(label="Conversation")
92
+
93
+ # Address input
94
+ msg = gr.Textbox(
95
+ placeholder="Enter address here (e.g., 0x123..., bc1q...)",
96
+ label="Address",
97
+ lines=1
98
+ )
99
+
100
+ # Clear button
101
+ clear = gr.Button("Clear")
102
+
103
+ # Submit event
104
+ msg.submit(
105
+ fn=chat_with_agent,
106
+ inputs=[msg, chatbot, hf_token_input],
107
+ outputs=[chatbot, msg]
108
+ )
109
+
110
+ # Clear event
111
+ clear.click(
112
+ fn=lambda: ([], ""),
113
+ inputs=None,
114
+ outputs=[chatbot, msg]
115
+ )
116
+
117
+ # Tab 2: Explanation
118
+ with gr.Tab(label="About This App"):
119
+ gr.Markdown("""
120
+ ## What This App Does
121
+ This application helps you verify if a cryptocurrency address is associated with the Bybit hack, as tracked by the Elliptic dataset. Here’s how it works:
122
+
123
+ 1. **Input an Address**: Enter a wallet address (e.g., Ethereum `0x...`, Bitcoin `bc1q...`, Tron `T...`) in the 'Check Address' tab.
124
+ 2. **Provide HF Token**: Supply a Hugging Face API token to access the AI model powering the agent.
125
+ 3. **Agent Processing**: The app uses a `CodeAgent` from the `smolagents` library, powered by the free `Mixtral-8x7B-Instruct-v0.1` model via the Hugging Face Inference API.
126
+ 4. **Tool Usage**: The agent calls a custom tool (`check_flagged_address`) to compare your input against a list of flagged addresses from the Elliptic Bybit hack dataset (`https://dt074px2e9qbh.cloudfront.net/exploit-api.json`).
127
+ 5. **Chain Identification**: If the address isn’t explicitly tagged with a blockchain in the dataset, the app infers it (Ethereum, Bitcoin, Tron, or Unknown) based on common address formats.
128
+ 6. **Result**: You’ll see whether the address is flagged and its inferred or confirmed blockchain in the chat interface.
129
+
130
+ ### Why It’s Useful
131
+ - **Security**: Quickly check if an address you’re dealing with is linked to a known exploit.
132
+ - **Free & Open**: Uses free-tier AI and public data, making it accessible to anyone with an HF account.
133
+ - **Educational**: Demonstrates AI-driven blockchain analysis with `smolagents`.
134
+
135
+ Get started by entering your HF token and an address in the 'Check Address' tab!
136
+ """)
137
+
138
+ # Launch the app
139
+ demo.launch()