File size: 30,021 Bytes
caedbee
 
 
 
 
097dcdc
caedbee
df993b7
 
 
caedbee
d3e9cd1
9433589
caedbee
d3e9cd1
5b39c29
df993b7
 
 
 
 
 
caedbee
df993b7
5b39c29
caedbee
 
 
5b39c29
 
58bbab8
 
5b39c29
a50912b
58bbab8
1d02fc4
df993b7
 
caedbee
 
 
 
1d02fc4
df993b7
 
caedbee
 
 
 
 
1d02fc4
 
 
 
df993b7
 
caedbee
df993b7
66c6745
df993b7
 
 
c3c65c0
df993b7
 
 
1d02fc4
 
c3c65c0
df993b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caedbee
5b39c29
a50912b
 
caedbee
5b39c29
caedbee
 
66c6745
df993b7
caedbee
5b39c29
df993b7
caedbee
ecb3e47
df993b7
 
58bbab8
 
caedbee
 
 
df993b7
097dcdc
df993b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bb1ea5
 
1d02fc4
 
 
 
 
 
 
 
df993b7
 
 
1d02fc4
df993b7
 
 
 
 
1d02fc4
 
df993b7
1d02fc4
 
 
 
 
df993b7
 
1d02fc4
 
df993b7
 
 
1d02fc4
 
 
 
 
 
df993b7
 
 
 
 
 
 
 
 
1d02fc4
df993b7
1d02fc4
df993b7
 
1d02fc4
df993b7
 
 
 
 
 
 
 
 
 
1d02fc4
df993b7
ecb3e47
1d02fc4
 
 
df993b7
1d02fc4
df993b7
 
 
 
 
 
 
 
 
 
 
1d02fc4
df993b7
 
 
1d02fc4
df993b7
 
 
 
 
 
 
 
1d02fc4
df993b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d02fc4
 
df993b7
 
f395a8f
df993b7
 
 
f395a8f
df993b7
 
1d02fc4
 
df993b7
1d02fc4
df993b7
 
 
1d02fc4
 
 
df993b7
1d02fc4
df993b7
 
1d02fc4
 
 
 
 
 
 
 
 
 
 
 
df993b7
1d02fc4
df993b7
1d02fc4
 
 
df993b7
 
 
 
 
1d02fc4
 
df993b7
1d02fc4
 
df993b7
1d02fc4
 
df993b7
 
1d02fc4
 
 
 
df993b7
 
 
 
1d02fc4
 
df993b7
1d02fc4
df993b7
 
1d02fc4
 
df993b7
 
1d02fc4
df993b7
1d02fc4
 
df993b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d02fc4
df993b7
1d02fc4
ecb3e47
df993b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caedbee
df993b7
1d02fc4
df993b7
 
 
 
 
 
1d02fc4
 
 
df993b7
1d02fc4
df993b7
 
 
 
 
 
 
1d02fc4
 
 
df993b7
 
1d02fc4
 
 
df993b7
1d02fc4
 
 
 
df993b7
 
 
 
 
 
 
 
 
1d02fc4
df993b7
 
 
 
 
 
 
 
 
 
1d02fc4
df993b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ef3673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import os
import sys
import subprocess
import re
import multiprocessing
import atexit
from collections.abc import Iterator
from functools import lru_cache
import datetime
import time
import gradio as gr
import gradio.themes as themes
from huggingface_hub import hf_hub_download, login
import logging
import pandas as pd
import torch

# Set up logging with more detail
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Install llama-cpp-python with appropriate backend
try:
    from llama_cpp import Llama
except ModuleNotFoundError:
    if torch.cuda.is_available():
        logger.info("Installing llama-cpp-python with CUDA support.")
        os.environ['CMAKE_ARGS'] = "-DLLAMA_CUDA=ON"
        subprocess.check_call([sys.executable, "-m", "pip", "install", "llama-cpp-python", "--force-reinstall", "--upgrade", "--no-cache-dir"])
    else:
        logger.info("Installing llama-cpp-python without additional flags.")
        subprocess.check_call([sys.executable, "-m", "pip", "install", "llama-cpp-python", "--force-reinstall", "--upgrade", "--no-cache-dir"])
from llama_cpp import Llama

# Install yfinance if not present
try:
    import yfinance as yf
except ModuleNotFoundError:
    subprocess.check_call([sys.executable, "-m", "pip", "install", "yfinance"])
import yfinance as yf

# Additional imports for visualization
try:
    import matplotlib.pyplot as plt
    from PIL import Image
    import io
except ModuleNotFoundError:
    subprocess.check_call([sys.executable, "-m", "pip", "install", "matplotlib", "pillow"])
import matplotlib.pyplot as plt
from PIL import Image
import io

# Constants
MAX_MAX_NEW_TOKENS = 512
DEFAULT_MAX_NEW_TOKENS = 256
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "1024"))
YFINANCE_TIMEOUT = 10
CACHE_EXPIRY_HOURS = 1

DESCRIPTION = """# FinChat: Investing Q&A (Optimized for Speed)
This application delivers an interactive chat interface powered by a highly efficient, small AI model adapted for addressing investing and finance inquiries through specialized prompt engineering. It ensures rapid, reasoned responses to user queries.
<p>Running on CPU or GPU if available. Using Phi-2 model for faster inference. Includes performance optimizations with caching and improved error handling.</p>"""

LICENSE = """<p/>
---
This application employs the Phi-2 model, governed by Microsoft's Terms of Use. Refer to the [model card](https://huggingface.co/TheBloke/phi-2-GGUF) for details."""

DEFAULT_SYSTEM_PROMPT = """You are FinChat, a knowledgeable AI assistant specializing in investing and finance. Provide accurate, helpful, reasoned, and concise answers to investing questions. Always base responses on reliable information and advise users to consult professionals for personalized advice.
Always respond exclusively in English. Use bullet points for clarity.
Example:
User: average return for TSLA between 2010 and 2020
Assistant:
- TSLA CAGR (2010-2020): ~63.01%
- Represents average annual return with compounding
- Past performance not indicative of future results
- Consult a financial advisor"""

# Company name to ticker mapping
COMPANY_TO_TICKER = {
    "opendoor": "OPEN",
    "tesla": "TSLA",
    "apple": "AAPL",
    "amazon": "AMZN",
    "microsoft": "MSFT",
    "google": "GOOGL",
    "facebook": "META",
    "meta": "META",
    "nvidia": "NVDA",
    "netflix": "NFLX",
}

# Compiled regex patterns for better performance
CAGR_PATTERN = re.compile(
    r'(?:average\s+return|cagr)\s+(?:for\s+)?([\w\s,]+(?:and\s+[\w\s,]+)?)\s+(?:between|from)\s+(\d{4})\s+(?:and|to)\s+(\d{4})',
    re.IGNORECASE
)

COMPOUND_INTEREST_PATTERN = re.compile(
    r'(?:save|invest|deposit)\s*\$?([\d,]+(?:\.\d+)?)\s*(?:right now|today)?\s*(?:under|at)\s*([\d.]+)%\s*(?:interest|rate)?\s*(?:annually|per year)?\s*(?:over|for)\s*(\d+)\s*years?',
    re.IGNORECASE
)

# Load the model
try:
    model_path = hf_hub_download(
        repo_id="TheBloke/phi-2-GGUF",
        filename="phi-2.Q4_K_M.gguf"
    )
    n_gpu_layers = -1 if torch.cuda.is_available() else 0
    llm = Llama(
        model_path=model_path,
        n_ctx=1024,
        n_batch=1024,
        n_threads=multiprocessing.cpu_count(),
        n_gpu_layers=n_gpu_layers,
        chat_format="chatml"
    )
    logger.info(f"Model loaded successfully with n_gpu_layers={n_gpu_layers}.")
    
    # Warm up the model
    llm("Warm-up prompt", max_tokens=1, echo=False)
    logger.info("Model warm-up completed.")
except Exception as e:
    logger.error(f"Error loading model: {str(e)}")
    raise

atexit.register(llm.close)

# Cache for stock data with timestamp
_stock_cache = {}
_cache_timestamps = {}

def sanitize_ticker(ticker):
    """Sanitize ticker input to prevent injection and validate format"""
    if not ticker or not isinstance(ticker, str):
        return None
    cleaned = re.sub(r'[^A-Z0-9.-]', '', ticker.upper().strip())
    if len(cleaned) > 10 or len(cleaned) == 0:
        return None
    return cleaned

def validate_year_range(start_year, end_year):
    """Validate year inputs"""
    try:
        start = int(start_year)
        end = int(end_year)
        current_year = datetime.datetime.now().year
        
        if not (1900 <= start <= current_year):
            return False, f"Start year must be between 1900 and {current_year}"
        if not (1900 <= end <= current_year + 1):
            return False, f"End year must be between 1900 and {current_year + 1}"
        if end <= start:
            return False, "End year must be after start year"
        
        return True, "Valid"
    except ValueError:
        return False, "Years must be valid integers"

@lru_cache(maxsize=100)
def get_stock_data_cached(ticker, start_date, end_date, cache_key):
    """Cache stock data to avoid repeated API calls. cache_key includes hour for expiry."""
    try:
        logger.info(f"Fetching data for {ticker} from {start_date} to {end_date}")
        data = yf.download(
            ticker, 
            start=start_date, 
            end=end_date, 
            progress=False, 
            auto_adjust=False,
            timeout=YFINANCE_TIMEOUT
        )
        return data
    except Exception as e:
        logger.error(f"Error fetching data for {ticker}: {str(e)}")
        return None

def get_current_cache_key():
    """Generate cache key that expires every hour"""
    now = datetime.datetime.now()
    return f"{now.year}{now.month}{now.day}{now.hour}"

def calculate_cagr(ticker, start_year, end_year):
    """Calculate CAGR for a ticker with error handling"""
    try:
        # Sanitize ticker
        clean_ticker = sanitize_ticker(ticker)
        if not clean_ticker:
            return f"- {ticker}: Invalid ticker format"
        
        # Validate years
        valid, msg = validate_year_range(start_year, end_year)
        if not valid:
            return f"- {clean_ticker}: {msg}"
        
        # Get cached data
        cache_key = get_current_cache_key()
        data = get_stock_data_cached(
            clean_ticker,
            f"{start_year}-01-01",
            f"{end_year}-12-31",
            cache_key
        )
        
        if data is None:
            return f"- {clean_ticker}: Error fetching data (API timeout or network issue)"
        
        if data.empty:
            return f"- {clean_ticker}: No historical data available between {start_year} and {end_year}"
        
        # Handle MultiIndex columns
        if isinstance(data.columns, pd.MultiIndex):
            data.columns = data.columns.droplevel(1)
        
        # Check for Adj Close column
        if 'Adj Close' not in data.columns:
            return f"- {clean_ticker}: Adjusted Close price data not available"
        
        # Calculate CAGR
        initial = data['Adj Close'].iloc[0]
        final = data['Adj Close'].iloc[-1]
        
        if pd.isna(initial) or pd.isna(final):
            return f"- {clean_ticker}: Missing price data"
        
        if initial <= 0 or final <= 0:
            return f"- {clean_ticker}: Invalid price data (negative or zero values)"
        
        start_date = data.index[0]
        end_date = data.index[-1]
        days = (end_date - start_date).days
        years = days / 365.25
        
        if years <= 0:
            return f"- {clean_ticker}: Invalid time period"
        
        cagr = ((final / initial) ** (1 / years) - 1) * 100
        
        # Add context about data quality
        actual_start = start_date.strftime('%Y-%m-%d')
        actual_end = end_date.strftime('%Y-%m-%d')
        date_note = f" (data: {actual_start} to {actual_end})" if actual_start != f"{start_year}-01-01" else ""
        
        return f"- {clean_ticker}: ~{cagr:.2f}%{date_note}"
        
    except Exception as e:
        logger.error(f"Unexpected error calculating CAGR for {ticker}: {str(e)}")
        return f"- {ticker}: Calculation error - {str(e)}"

def generate(
    message: str,
    chat_history: list[dict],
    system_prompt: str = DEFAULT_SYSTEM_PROMPT,
    max_new_tokens: int = DEFAULT_MAX_NEW_TOKENS,
    temperature: float = 0.6,
    top_p: float = 0.9,
    top_k: int = 50,
    repetition_penalty: float = 1.2,
) -> Iterator[str]:
    start_time = time.time()
    logger.info(f"Generating response for message: {message[:100]}...")
    
    lower_message = message.lower().strip()
    
    # Quick responses for common queries
    if lower_message in ["hi", "hello", "hey"]:
        response = "Hello! I'm FinChat, your financial advisor. Ask me anything about investing, stocks, CAGR, compound interest, or portfolio analysis!"
        logger.info(f"Quick response generated in {time.time() - start_time:.2f}s")
        yield response
        return
    
    if "what is cagr" in lower_message:
        response = """- CAGR stands for Compound Annual Growth Rate.
- It measures the mean annual growth rate of an investment over a specified period longer than one year, accounting for compounding.
- Formula: CAGR = (Ending Value / Beginning Value)^(1 / Number of Years) - 1
- Useful for comparing investments over time.
- Past performance is not indicative of future results. Consult a financial advisor."""
        logger.info(f"Quick response generated in {time.time() - start_time:.2f}s")
        yield response
        return
    
    # Compound interest calculation
    compound_match = COMPOUND_INTEREST_PATTERN.search(lower_message)
    if compound_match:
        try:
            principal_str = compound_match.group(1).replace(',', '')
            principal = float(principal_str)
            rate = float(compound_match.group(2)) / 100
            years = int(compound_match.group(3))
            
            if principal <= 0:
                yield "Error: Principal amount must be positive."
                return
            if rate < 0 or rate > 1:
                yield "Error: Interest rate must be between 0% and 100%."
                return
            if years <= 0 or years > 100:
                yield "Error: Years must be between 1 and 100."
                return
            
            balance = principal * (1 + rate) ** years
            total_interest = balance - principal
            
            response = (
                f"**Compound Interest Calculation**\n\n"
                f"- Starting Principal: ${principal:,.2f}\n"
                f"- Annual Interest Rate: {rate*100:.2f}%\n"
                f"- Time Period: {years} years\n"
                f"- Compounding: Annually\n\n"
                f"**Results:**\n"
                f"- Final Balance (Year {years}): ${balance:,.2f}\n"
                f"- Total Interest Earned: ${total_interest:,.2f}\n"
                f"- Total Growth: {((balance/principal - 1) * 100):.2f}%\n\n"
                f"*Note: This assumes annual compounding with no additional deposits or withdrawals. Actual results may vary. Consult a financial advisor.*"
            )
            logger.info(f"Compound interest calculated in {time.time() - start_time:.2f}s")
            yield response
            return
        except ValueError as ve:
            logger.error(f"Error parsing compound interest query: {str(ve)}")
            yield "Error: Please ensure amount, rate, and years are valid numbers. Example: 'If I save $10000 at 5% interest over 10 years'"
            return
        except Exception as e:
            logger.error(f"Unexpected error in compound interest: {str(e)}")
            yield f"Error calculating compound interest: {str(e)}"
            return
    
    # CAGR calculation with improved pattern matching
    cagr_match = CAGR_PATTERN.search(lower_message)
    if cagr_match:
        tickers_str, start_year, end_year = cagr_match.groups()
        tickers = [t.strip() for t in re.split(r',|\band\b', tickers_str) if t.strip()]
        
        # Apply company-to-ticker mapping
        mapped_tickers = []
        for ticker in tickers:
            lower_ticker = ticker.lower()
            if lower_ticker in COMPANY_TO_TICKER:
                mapped_tickers.append(COMPANY_TO_TICKER[lower_ticker])
            else:
                mapped_tickers.append(ticker.upper())
        
        # Validate year range first
        valid, msg = validate_year_range(start_year, end_year)
        if not valid:
            yield f"Error: {msg}"
            return
        
        if len(mapped_tickers) > 10:
            yield "Error: Too many tickers requested. Please limit to 10 tickers per query."
            return
        
        responses = []
        for ticker in mapped_tickers:
            result = calculate_cagr(ticker, start_year, end_year)
            responses.append(result)
        
        full_response = (
            f"**CAGR Analysis ({start_year} - {end_year})**\n\n"
            + "\n".join(responses) + 
            "\n\n*Notes:*\n"
            "- CAGR represents average annual returns with compounding\n"
            "- Based on adjusted closing prices\n"
            "- Past performance is not indicative of future results\n"
            "- Please consult a financial advisor for investment decisions"
        )
        
        logger.info(f"CAGR response generated in {time.time() - start_time:.2f}s")
        yield full_response
        return
    
    # Build conversation for LLM
    conversation = [{"role": "system", "content": system_prompt}]
    for msg in chat_history[-3:]:
        if msg["role"] in ["user", "assistant"]:
            conversation.append({"role": msg["role"], "content": msg["content"]})
    conversation.append({"role": "user", "content": message})
    
    # Token length check with truncation
    prompt_text = "\n".join(d["content"] for d in conversation)
    input_tokens = llm.tokenize(prompt_text.encode("utf-8"), add_bos=False)
    
    while len(input_tokens) > MAX_INPUT_TOKEN_LENGTH:
        logger.warning(f"Input tokens ({len(input_tokens)}) exceed limit. Truncating history.")
        if len(conversation) > 2:
            conversation.pop(1)
            prompt_text = "\n".join(d["content"] for d in conversation)
            input_tokens = llm.tokenize(prompt_text.encode("utf-8"), add_bos=False)
        else:
            yield "Error: Input is too long. Please shorten your query or start a new conversation."
            return
    
    # Generate response
    try:
        response = ""
        sentence_buffer = ""
        stream = llm.create_chat_completion(
            messages=conversation,
            max_tokens=max_new_tokens,
            temperature=temperature,
            top_p=top_p,
            top_k=top_k,
            repeat_penalty=repetition_penalty,
            stream=True
        )
        
        sentence_endings = ['.', '!', '?']
        
        for chunk in stream:
            delta = chunk["choices"][0]["delta"]
            if "content" in delta and delta["content"] is not None:
                cleaned_chunk = re.sub(
                    r'<\|(?:im_start|im_end|system|user|assistant)\|>|</s>|\[END\]', 
                    '', 
                    delta["content"]
                )
                if not cleaned_chunk:
                    continue
                
                sentence_buffer += cleaned_chunk
                response += cleaned_chunk
                
                if any(sentence_buffer.strip().endswith(ending) for ending in sentence_endings):
                    yield response
                    sentence_buffer = ""
            
            if chunk["choices"][0]["finish_reason"] is not None:
                if sentence_buffer.strip():
                    yield response
                break
        
        duration = time.time() - start_time
        logger.info(f"LLM response generated in {duration:.2f}s")
        
    except ValueError as ve:
        if "exceed context window" in str(ve):
            yield "Error: Input exceeds context window. Please try a shorter query."
        else:
            logger.error(f"ValueError during generation: {str(ve)}")
            yield f"Error: {str(ve)}"
    except Exception as e:
        logger.error(f"Error during response generation: {str(e)}")
        yield f"Error generating response. Please try again or rephrase your question."

def process_portfolio(df, growth_rate):
    """Process portfolio with enhanced error handling and validation"""
    if df is None or len(df) == 0:
        return "", None
    
    try:
        if not isinstance(df, pd.DataFrame):
            df = pd.DataFrame(df, columns=["Ticker", "Shares", "Avg Cost", "Current Price"])
        
        # Validate and convert numeric columns
        for col in ["Shares", "Avg Cost", "Current Price"]:
            df[col] = pd.to_numeric(df[col], errors='coerce')
        
        df = df.dropna(subset=["Ticker"])
        
        portfolio = {}
        errors = []
        
        for idx, row in df.iterrows():
            ticker = sanitize_ticker(row["Ticker"]) if pd.notna(row["Ticker"]) else None
            if not ticker:
                continue
            
            shares = float(row["Shares"]) if pd.notna(row["Shares"]) else 0
            cost = float(row["Avg Cost"]) if pd.notna(row["Avg Cost"]) else 0
            price = float(row["Current Price"]) if pd.notna(row["Current Price"]) else 0
            
            if shares <= 0:
                errors.append(f"{ticker}: Invalid shares count")
                continue
            if price < 0 or cost < 0:
                errors.append(f"{ticker}: Negative prices not allowed")
                continue
            
            value = shares * price
            cost_basis = shares * cost
            gain_loss = value - cost_basis
            gain_loss_pct = (gain_loss / cost_basis * 100) if cost_basis > 0 else 0
            
            portfolio[ticker] = {
                'shares': shares,
                'cost': cost,
                'price': price,
                'value': value,
                'cost_basis': cost_basis,
                'gain_loss': gain_loss,
                'gain_loss_pct': gain_loss_pct
            }
        
        if not portfolio:
            return "No valid portfolio entries found. Please check your data.", None
        
        total_value_now = sum(v['value'] for v in portfolio.values())
        total_cost_basis = sum(v['cost_basis'] for v in portfolio.values())
        total_gain_loss = total_value_now - total_cost_basis
        total_gain_loss_pct = (total_gain_loss / total_cost_basis * 100) if total_cost_basis > 0 else 0
        
        allocations = {k: v['value'] / total_value_now for k, v in portfolio.items()} if total_value_now > 0 else {}
        
        # Create allocation pie chart
        fig_alloc, ax_alloc = plt.subplots(figsize=(8, 6))
        colors = plt.cm.Set3(range(len(allocations)))
        ax_alloc.pie(
            allocations.values(), 
            labels=allocations.keys(), 
            autopct='%1.1f%%',
            colors=colors,
            startangle=90
        )
        ax_alloc.set_title('Portfolio Allocation by Value', fontsize=14, fontweight='bold')
        
        buf_alloc = io.BytesIO()
        fig_alloc.savefig(buf_alloc, format='png', bbox_inches='tight', dpi=100)
        buf_alloc.seek(0)
        chart_alloc = Image.open(buf_alloc)
        plt.close(fig_alloc)
        
        # Project future values
        def project_value(value, years, rate):
            return value * (1 + rate / 100) ** years
        
        projections = {
            '1 year': sum(project_value(v['value'], 1, growth_rate) for v in portfolio.values()),
            '2 years': sum(project_value(v['value'], 2, growth_rate) for v in portfolio.values()),
            '5 years': sum(project_value(v['value'], 5, growth_rate) for v in portfolio.values()),
            '10 years': sum(project_value(v['value'], 10, growth_rate) for v in portfolio.values())
        }
        
        # Build detailed report
        data_str = "**πŸ“Š Portfolio Analysis**\n\n"
        data_str += "**Current Holdings:**\n"
        for ticker, data in portfolio.items():
            data_str += (
                f"- {ticker}: {data['shares']:.2f} shares @ ${data['price']:.2f} "
                f"(Cost: ${data['cost']:.2f}) = ${data['value']:,.2f} "
                f"[{data['gain_loss_pct']:+.2f}%]\n"
            )
        
        data_str += f"\n**Portfolio Summary:**\n"
        data_str += f"- Total Value: ${total_value_now:,.2f}\n"
        data_str += f"- Total Cost Basis: ${total_cost_basis:,.2f}\n"
        data_str += f"- Total Gain/Loss: ${total_gain_loss:+,.2f} ({total_gain_loss_pct:+.2f}%)\n"
        
        data_str += f"\n**Projected Values (at {growth_rate}% annual growth):**\n"
        for period, value in projections.items():
            gain = value - total_value_now
            data_str += f"- {period}: ${value:,.2f} (+${gain:,.2f})\n"
        
        if errors:
            data_str += f"\n**⚠️ Warnings:**\n"
            for error in errors:
                data_str += f"- {error}\n"
        
        data_str += "\n*Note: Projections assume constant growth rate and no additional contributions. Actual results will vary. Consult a financial advisor.*"
        
        return data_str, chart_alloc
        
    except Exception as e:
        logger.error(f"Error processing portfolio: {str(e)}")
        return f"Error processing portfolio: {str(e)}", None

def fetch_current_prices(df):
    """Fetch current prices with timeout and error handling"""
    if df is None or len(df) == 0:
        return df
    
    try:
        if not isinstance(df, pd.DataFrame):
            df = pd.DataFrame(df, columns=["Ticker", "Shares", "Avg Cost", "Current Price"])
        
        updated_count = 0
        failed_tickers = []
        
        for i in df.index:
            ticker = df.at[i, "Ticker"]
            if pd.notna(ticker) and ticker.strip():
                clean_ticker = sanitize_ticker(ticker)
                if not clean_ticker:
                    failed_tickers.append(f"{ticker} (invalid format)")
                    continue
                
                try:
                    ticker_obj = yf.Ticker(clean_ticker)
                    info = ticker_obj.info
                    price = info.get('currentPrice') or info.get('regularMarketPrice')
                    
                    if price is not None and price > 0:
                        df.at[i, "Current Price"] = price
                        updated_count += 1
                    else:
                        failed_tickers.append(f"{clean_ticker} (no price data)")
                except Exception as e:
                    logger.warning(f"Failed to fetch price for {clean_ticker}: {str(e)}")
                    failed_tickers.append(f"{clean_ticker} ({str(e)[:30]})")
        
        if updated_count > 0:
            logger.info(f"Successfully updated {updated_count} prices")
        if failed_tickers:
            logger.warning(f"Failed to fetch: {', '.join(failed_tickers)}")
        
        return df
        
    except Exception as e:
        logger.error(f"Error in fetch_current_prices: {str(e)}")
        return df

# Gradio interface
with gr.Blocks(theme=themes.Soft(), css="""
    #chatbot {height: 800px; overflow: auto;}
    .performance-note {color: #666; font-size: 0.9em; font-style: italic;}
""") as demo:
    gr.Markdown(DESCRIPTION)
    
    chatbot = gr.Chatbot(label="FinChat", type="messages")
    msg = gr.Textbox(
        label="Ask a finance question", 
        placeholder="e.g., 'What is CAGR?' or 'Average return for AAPL between 2010 and 2020'",
        info="Enter your query. Responses are cached for better performance."
    )
    
    with gr.Row():
        submit = gr.Button("Submit", variant="primary")
        clear = gr.Button("Clear")
    
    gr.Examples(
        examples=[
            "What is CAGR?",
            "Average return for AAPL between 2015 and 2023",
            "Average return for TSLA and NVDA between 2018 and 2023",
            "If I save $10000 at 5% interest over 10 years",
            "Explain compound interest"
        ],
        inputs=msg,
        label="Example Queries"
    )
    
    with gr.Accordion("πŸ“ˆ Enter Portfolio for Projections", open=False):
        portfolio_df = gr.Dataframe(
            headers=["Ticker", "Shares", "Avg Cost", "Current Price"],
            datatype=["str", "number", "number", "number"],
            row_count=5,
            col_count=(4, "fixed"),
            label="Portfolio Data",
            interactive=True
        )
        gr.Markdown("""
        **Instructions:** 
        - Enter stock tickers (e.g., AAPL, TSLA)
        - Fill in number of shares and your average cost per share
        - Click 'Fetch Current Prices' to auto-populate current prices
        - Adjust growth rate for future projections
        """)
        
        fetch_button = gr.Button("πŸ”„ Fetch Current Prices", variant="secondary")
        fetch_button.click(fetch_current_prices, inputs=portfolio_df, outputs=portfolio_df)
        
        growth_rate = gr.Slider(
            minimum=0, 
            maximum=50, 
            step=1, 
            value=10, 
            label="Annual Growth Rate (%)", 
            interactive=True,
            info="Expected annual return for projections (0-50%)"
        )
        growth_rate_label = gr.Markdown("**Selected Growth Rate: 10%**")
    
    with gr.Accordion("βš™οΈ Advanced Settings", open=False):
        system_prompt = gr.Textbox(
            label="System Prompt", 
            value=DEFAULT_SYSTEM_PROMPT, 
            lines=6,
            info="Customize the AI's behavior"
        )
        temperature = gr.Slider(
            label="Temperature", 
            value=0.6, 
            minimum=0.0, 
            maximum=1.0, 
            step=0.05,
            info="Lower = more focused, Higher = more creative"
        )
        top_p = gr.Slider(
            label="Top P", 
            value=0.9, 
            minimum=0.0, 
            maximum=1.0, 
            step=0.05,
            info="Nucleus sampling threshold"
        )
        top_k = gr.Slider(
            label="Top K", 
            value=50, 
            minimum=1, 
            maximum=100, 
            step=1,
            info="Limit to top K tokens"
        )
        repetition_penalty = gr.Slider(
            label="Repetition Penalty", 
            value=1.2, 
            minimum=1.0, 
            maximum=2.0,
            step=0.05,
            info="Penalize repeated tokens"
        )
        max_new_tokens = gr.Slider(
            label="Max New Tokens", 
            value=DEFAULT_MAX_NEW_TOKENS, 
            minimum=1, 
            maximum=MAX_MAX_NEW_TOKENS, 
            step=1,
            info="Maximum length of generated response"
        )
    
    gr.Markdown(LICENSE)
    gr.Markdown('<p class="performance-note">⚑ Performance optimized with caching and improved error handling</p>')
    
    def update_growth_rate_label(growth_rate):
        return f"**Selected Growth Rate: {growth_rate}%**"
    
    def user(message, history):
        if not message:
            return "", history
        return "", history + [{"role": "user", "content": message}]
    
    def bot(history, sys_prompt, temp, tp, tk, rp, mnt, portfolio_df, growth_rate):
        if not history:
            logger.warning("History is empty, initializing with user message.")
            history = [{"role": "user", "content": ""}]
        
        message = history[-1]["content"]
        portfolio_data, chart_alloc = process_portfolio(portfolio_df, growth_rate)
        
        if portfolio_data:
            message += "\n\n" + portfolio_data
        
        history[-1]["content"] = message
        history.append({"role": "assistant", "content": ""})
        
        for new_text in generate(message, history[:-1], sys_prompt, mnt, temp, tp, tk, rp):
            history[-1]["content"] = new_text
            yield history, f"**Selected Growth Rate: {growth_rate}%**"
        
        if chart_alloc:
            # Append chart as a separate message
            yield history, f"**Selected Growth Rate: {growth_rate}%**"
    
    growth_rate.change(update_growth_rate_label, inputs=growth_rate, outputs=growth_rate_label)
    
    submit.click(
        user, 
        [msg, chatbot], 
        [msg, chatbot], 
        queue=False
    ).then(
        bot, 
        [chatbot, system_prompt, temperature, top_p, top_k, repetition_penalty, max_new_tokens, portfolio_df, growth_rate], 
        [chatbot, growth_rate_label]
    )
    
    msg.submit(
        user, 
        [msg, chatbot], 
        [msg, chatbot], 
        queue=False
    ).then(
        bot, 
        [chatbot, system_prompt, temperature, top_p, top_k, repetition_penalty, max_new_tokens, portfolio_df, growth_rate], 
        [chatbot, growth_rate_label]
    )
    
    clear.click(lambda: [], None, chatbot, queue=False)

demo.queue(max_size=128).launch()