File size: 9,258 Bytes
15e1317
 
 
 
 
 
 
 
 
f95b69e
 
 
15e1317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d435b4
f95b69e
 
 
 
 
 
 
 
 
 
 
 
5d435b4
f95b69e
 
 
 
 
5d435b4
15e1317
 
 
5d435b4
 
 
15e1317
 
5d435b4
 
 
15e1317
 
b3e5de1
 
 
15e1317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3e5de1
 
 
15e1317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import gradio as gr
import plotly.express as px
from cryptoindex import *
import pandas as pd
from updater import *
from time import sleep
from functools import partial
import argparse
import os
from supabase import Client
from supabase.client import ClientOptions
import httpx
from datetime import datetime
import hashlib
import json
import uuid
from dotenv import load_dotenv

load_dotenv()

supabase_url = os.getenv("SUPABASE_URL", "")
supabase_key = os.getenv("SUPABASE_KEY", "")

print(f"Supabase URL: {supabase_url[:30]}..." if supabase_url else "No Supabase URL")
print(f"Supabase Key: {supabase_key[:20]}..." if supabase_key else "No Supabase Key")

try:
    if supabase_url and supabase_key:
        # Create client manually to avoid proxy issues in HF Spaces
        options = ClientOptions(
            headers={
                "apikey": supabase_key,
                "Authorization": f"Bearer {supabase_key}"
            },
            schema="public",
            auto_refresh_token=True,
            persist_session=True,
            storage_options={},
            realtime_options={}
        )
        
        # Create HTTP client without proxy
        http_client = httpx.Client()
        
        # Initialize Supabase client
        supabase = Client(supabase_url, supabase_key, options)
        
        # Test the connection
        test_response = supabase.table('index_cache').select('id').limit(1).execute()
        print("Supabase connection test successful")
    else:
        print("Missing Supabase credentials")
        supabase = None
except Exception as e:
    print(f"Supabase initialization error: {e}")
    print(f"Error details: {type(e).__name__}")
    import traceback
    traceback.print_exc()
    supabase = None

# Don't update weights at startup - it might fail without proper env vars
# update_weights1() is called later when needed


def get_cache_key(start_date: str, end_date: str, locale: str, market_type: str) -> str:
    """Generate a unique cache key for the given parameters."""
    key_string = f"{start_date}_{end_date}_{locale}_{market_type}"
    return hashlib.md5(key_string.encode()).hexdigest()


def get_or_create_session(request: gr.Request) -> str:
    """Get or create a session ID from the Gradio request."""
    if hasattr(request, 'session_hash'):
        return request.session_hash
    return str(uuid.uuid4())


def fetch_from_cache(cache_key: str):
    """Fetch index data from Supabase cache."""
    if not supabase:
        print("Supabase client not initialized")
        return None
    
    try:
        print(f"Fetching cache for key: {cache_key}")
        response = supabase.table('index_cache').select('*').eq('cache_key', cache_key).execute()
        if response.data and len(response.data) > 0:
            data = response.data[0]
            v_data = pd.read_json(data['index_data'])
            v_data.index = pd.to_datetime(v_data.index)
            print(f"Cache hit for key: {cache_key}")
            return v_data
        else:
            print(f"Cache miss for key: {cache_key}")
    except Exception as e:
        print(f"Cache fetch error: {e}")
        print(f"Error type: {type(e)}")
        if hasattr(e, 'response'):
            print(f"Response: {e.response}")
    
    return None


def save_to_cache(cache_key: str, v_data: pd.DataFrame, start_date: str, end_date: str, 
                  locale: str, market_type: str):
    """Save index data to Supabase cache."""
    if not supabase:
        return
    
    try:
        cache_data = {
            'cache_key': cache_key,
            'start_date': start_date,
            'end_date': end_date,
            'locale': locale,
            'market_type': market_type,
            'index_data': v_data.to_json(),
            'created_at': datetime.now().isoformat()
        }
        
        supabase.table('index_cache').upsert(cache_data).execute()
    except Exception as e:
        print(f"Cache save error: {e}")


def get_user_weights(session_id: str, locale: str, market_type: str):
    """Get user-specific weights from Supabase."""
    if not supabase:
        return None
    
    try:
        response = supabase.table('user_weights').select('*').eq('session_id', session_id).eq('locale', locale).eq('market_type', market_type).order('created_at', desc=True).limit(1).execute()
        
        if response.data and len(response.data) > 0:
            weights_data = response.data[0]['weights_data']
            return pd.read_json(weights_data)
    except Exception as e:
        print(f"Error fetching user weights: {e}")
    
    return None


def save_user_weights(session_id: str, weights_df: pd.DataFrame, locale: str, market_type: str):
    """Save user-specific weights to Supabase."""
    if not supabase:
        return
    
    try:
        weights_data = {
            'session_id': session_id,
            'locale': locale,
            'market_type': market_type,
            'weights_data': weights_df.to_json(),
            'created_at': datetime.now().isoformat()
        }
        
        supabase.table('user_weights').insert(weights_data).execute()
    except Exception as e:
        print(f"Error saving user weights: {e}")


def plot_index_prices(start_date, end_date, request: gr.Request, **kwargs):
    """Plot historical index prices with caching."""
    session_id = get_or_create_session(request)
    locale = kwargs.get('locale', 'global')
    market_type = kwargs.get('market_type', 'crypto')
    
    cache_key = get_cache_key(start_date, end_date, locale, market_type)
    
    v = fetch_from_cache(cache_key)
    
    if v is None:
        cryptodf = fetch_crypto_data(start_date=start_date, end_date=end_date, **kwargs)
        v, _ = get_crypto_index(cryptodf, func=np.sqrt)
        save_to_cache(cache_key, v, start_date, end_date, locale, market_type)
    
    _, _, _, output = do_sharpe(v.close)
    fig = px.line(v, x=v.index, y='close', title='Index Prices')
    fig.update_xaxes(rangeslider_visible=True)
    return fig, output


def realtime_update_weighted_prices(request: gr.Request, locale='global', market_type='crypto'):
    """Update real-time prices with user-specific weights."""
    session_id = get_or_create_session(request)
    
    if should_update_weights():
        weights_df = update_weights(locale=locale, market_type=market_type)
        save_user_weights(session_id, weights_df, locale, market_type)
    
    last_day = get_user_weights(session_id, locale, market_type)
    
    if last_day is None:
        weights_df = update_weights(locale=locale, market_type=market_type)
        save_user_weights(session_id, weights_df, locale, market_type)
        last_day = weights_df
    
    prices = update_day(last_day)
    _, _, _, output = do_sharpe(prices, days=False)
    fig = px.line(prices, x=prices.index, y=prices.values, title='Index Today')
    return fig, output


def make_graph(choice, start_date=None, end_date=None, request: gr.Request = None, **kwargs):
    """Create graph based on user choice."""
    if choice == "Historical":
        fig, stats = plot_index_prices(start_date, end_date, request, **kwargs)
    else:
        fig, stats = realtime_update_weighted_prices(request, **kwargs)
    
    return gr.Plot(fig), gr.Markdown(stats)


def initialize_database():
    """Initialize Supabase tables if they don't exist."""
    if not supabase:
        print("Supabase not configured. Running in local mode.")
        return
    
    print("Supabase connected successfully!")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--locale", default='global', help="the locale")
    parser.add_argument("--market_type", default='crypto', help="the market type")
    parser.add_argument("--share", action="store_true", help="share the interface")
    args = parser.parse_args()
    
    initialize_database()
    
    # Don't call update_weights at startup - let it happen on first use
    # update_weights1(locale=args.locale, market_type=args.market_type)
    
    with gr.Blocks() as iface:
        gr.Markdown("# Crypto Index Tracker (Supabase Edition)")
        gr.Markdown("Each user session has isolated data and computations are cached.")
        
        startdatebox = gr.Textbox(label="Start Date", placeholder="YYYY-MM-DD")
        enddatebox = gr.Textbox(label="End Date", placeholder="YYYY-MM-DD")
        radio = gr.Radio(choices=["Historical", "Real-time"], label="Graph Type", value="Historical")
        update_button = gr.Button("Update Graph")
        
        theplot = gr.Plot()
        thestats = gr.Markdown()
        
        make_graph_partial = partial(make_graph, locale=args.locale, market_type=args.market_type)
        
        radio.change(
            fn=make_graph_partial,
            inputs=[radio, startdatebox, enddatebox],
            outputs=[theplot, thestats]
        )
        
        update_button.click(
            fn=make_graph_partial,
            inputs=[radio, startdatebox, enddatebox],
            outputs=[theplot, thestats]
        )
    
    # Detect if running on Hugging Face Spaces
    if os.getenv("SPACE_ID"):
        iface.launch()
    else:
        iface.launch(server_port=7860, server_name="0.0.0.0", share=args.share)