File size: 4,865 Bytes
aae34a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import streamlit as st
import requests
from typing import List, Dict
import os

API_URL = os.getenv('POND_API_URL')
API_TOKEN = os.getenv('POND_API_TOKEN')

st.title('🌊 Pond Model Demo')

st.sidebar.header('Model Selection')
model_info = {
    1: {
        'name': 'Security Model',
        'description': 'Analyze whether an account is secure by detecting and examining malicious activities within complex blockchain data structures.'
    },
    2: {
        'name': 'Sybil Model',
        'description': 'An model aims to detect on-chain "Sybil Attacks"'
    },
    3: {
        'name': 'ZORA NFT Recommendation',
        'description': 'On-Chain Recommendation System: Making Discoveries & Spread Easier for Everyone'
    }
}

selected_model = st.sidebar.selectbox(
    'Choose a model',
    list(model_info.keys()),
    format_func=lambda x: model_info[x]['name']
)

# Show model description
st.markdown(f"### {model_info[selected_model]['name']}")
st.markdown(model_info[selected_model]['description'])

# Input section
st.header('Input Wallet Addresses')
wallet_input = st.text_area(
    'Enter wallet addresses (one per line)',
    height=100,
    help='Enter wallet addresses, one per line'
)

def predict(addresses: List[str], model_id: int) -> Dict:
    """
    Make prediction using the Pond API
    """
    if not API_URL:
        st.error('API URL is not configured. Please set POND_API_URL environment variable.')
        return None
    
    headers = {
        "Content-Type": "application/json"
    }
    
    try:
        payload = {
            "req_type": "1",
            "access_token": API_TOKEN,
            "input_keys": addresses,
            "model_id": model_id
        }
        
        headers = {
            "Content-Type": "application/json"
        }
        
        # Make the API call with explicit method
        session = requests.Session()
        req = requests.Request('POST', 
                             API_URL,
                             json=payload,
                             headers=headers)
        prepped = req.prepare()
            
        response = session.send(prepped,
                              allow_redirects=True)
        
        # Check response
        if response.status_code != 200:
            st.error(f"API Error: {response.status_code}")
            st.error(f"Response: {response.text}")
            return None
            
        return response.json()
    except Exception as e:
        st.error(f"Error making prediction: {str(e)}")
        return None

def display_results(response: Dict, model_id: int):
    """
    Display the results based on model type
    """
    if not response or 'resp_items' not in response:
        return
    
    if model_id in [1, 2]:  # Security and Sybil models
        st.header('Results')
        for item in response['resp_items']:
            score = item['score']
            address = item['input_key']
            
            # Create color coding based on score
            if score < 0.3:
                color = 'green'
            elif score < 0.7:
                color = 'orange'
            else:
                color = 'red'
                
            st.markdown(f"""
            **Address**: `{address}`  
            **Score**: <span style='color: {color}'>{score:.4f}</span>
            """, unsafe_allow_html=True)
            st.markdown("")

    elif model_id == 3:  # NFT Recommendation model
        st.header('NFT Recommendations')
        for item in response['resp_items']:
            address = item['input_key']
            st.subheader(f'Recommendations for: `{address}`')
            
            if 'candidates' in item:
                for idx, candidate in enumerate(item['candidates'][:5], 1):
                    st.markdown(f"""
                    **{idx}. NFT ID**: `{candidate['item_id']}`  
                    **Score**: {candidate['score']:.4f}
                    """)
            st.markdown("")

# Process button
if st.button('Get Predictions'):
    if wallet_input:
        # Process input addresses
        addresses = [addr.strip() for addr in wallet_input.split('\n') if addr.strip()]
        
        # Validate addresses
        valid_addresses = [addr for addr in addresses if addr.startswith('0x')]
        
        if not valid_addresses:
            st.error('Please enter valid Ethereum addresses (starting with 0x)')
        else:
            with st.spinner('Making predictions...'):
                response = predict(valid_addresses, selected_model)
                if response:
                    display_results(response, selected_model)
    else:
        st.warning('Please enter at least one wallet address')

# Footer
st.markdown("---")
st.markdown("ℹ️ This is a demo interface for the Pond Model API. For production use, please refer to the official documentation.")