File size: 25,043 Bytes
c1ea847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdf2f16
 
 
 
c1ea847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdf2f16
 
 
 
 
 
 
 
 
 
c1ea847
 
 
 
 
 
bdf2f16
 
 
c1ea847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec7237c
44829cc
72f515a
8755ade
c1ea847
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdf2f16
 
c1ea847
72f515a
44829cc
aeaf850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44829cc
 
aeaf850
 
 
 
8e9c3b9
c1ea847
 
 
 
 
 
 
bdf2f16
c1ea847
 
bdf2f16
c1ea847
 
 
8e9c3b9
 
 
bdf2f16
8e9c3b9
 
 
 
 
 
 
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

import torch
import yfinance as yf
import matplotlib.pyplot as plt
import mplfinance as mpf
from PIL import Image, ImageDraw, ImageFont
import gradio as gr
import datetime
import logging
from transformers import AutoProcessor, AutoModelForPreTraining
import tempfile
import os
import spaces
import pandas as pd
import numpy as np
from scipy import stats
import seaborn as sns
import base64
from io import BytesIO



# Configure logging
logging.basicConfig(filename='debug.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Load the chart_analysis model and processor
processor = AutoProcessor.from_pretrained("mobenta/chart_analysis")
model = AutoModelForPreTraining.from_pretrained("mobenta/chart_analysis")

@spaces.GPU
def predict(image, input_text):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.to(device)

    image = image.convert("RGB")
    inputs = processor(text=input_text, images=image, return_tensors="pt")
    inputs = {k: v.to(device) for k, v in inputs.items()}
    
    prompt_length = inputs['input_ids'].shape[1]
    generate_ids = model.generate(**inputs, max_new_tokens=512)
    output_text = processor.batch_decode(generate_ids[:, prompt_length:], skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]

    return output_text

def fetch_stock_data(ticker='TSLA', start='2010-01-01', end=None, interval='1d'):
    if end is None:
        end = datetime.date.today().isoformat()
    try:
        logging.debug(f"Fetching data for {ticker} from {start} to {end} with interval {interval}")
        stock = yf.Ticker(ticker)
        data = stock.history(start=start, end=end, interval=interval)
        if data.empty:
            logging.warning(f"No data fetched for {ticker} in the range {start} to {end}")
            return pd.DataFrame()
        logging.debug(f"Fetched data with {len(data)} rows")
        return data
    except Exception as e:
        logging.error(f"Error fetching data: {e}")
        return pd.DataFrame()

def create_stock_chart(data, ticker, filename='chart.png', timeframe='1d', indicators=None):
    try:
        logging.debug(f"Creating chart for {ticker} with timeframe {timeframe} and saving to {filename}")
        title = f"{ticker.upper()} Price Data (Timeframe: {timeframe})"

        plt.rcParams["axes.titlesize"] = 10
        my_style = mpf.make_mpf_style(base_mpf_style='charles')

        # Calculate indicators if selected
        addplot = []
        if indicators:
            if 'RSI' in indicators:
                delta = data['Close'].diff(1)
                gain = delta.where(delta > 0, 0)
                loss = -delta.where(delta < 0, 0)
                avg_gain = gain.rolling(window=14).mean()
                avg_loss = loss.rolling(window=14).mean()
                rs = avg_gain / avg_loss
                rsi = 100 - (100 / (1 + rs))
                addplot.append(mpf.make_addplot(rsi, panel=2, color='orange', ylabel='RSI'))
            if 'SMA21' in indicators:
                logging.debug("Calculating SMA 21")
                sma_21 = data['Close'].rolling(window=21).mean()
                addplot.append(mpf.make_addplot(sma_21, color='purple', linestyle='dashed'))
            if 'SMA50' in indicators:
                logging.debug("Calculating SMA 50")
                sma_50 = data['Close'].rolling(window=50).mean()
                addplot.append(mpf.make_addplot(sma_50, color='orange', linestyle='dashed'))
            if 'SMA200' in indicators:
                logging.debug("Calculating SMA 200")
                sma_200 = data['Close'].rolling(window=200).mean()
                addplot.append(mpf.make_addplot(sma_200, color='brown', linestyle='dashed'))
            if 'VWAP' in indicators:
                logging.debug("Calculating VWAP")
                vwap = (data['Volume'] * (data['High'] + data['Low'] + data['Close']) / 3).cumsum() / data['Volume'].cumsum()
                addplot.append(mpf.make_addplot(vwap, color='blue', linestyle='dashed'))
            if 'Bollinger Bands' in indicators:
                logging.debug("Calculating Bollinger Bands")
                sma = data['Close'].rolling(window=20).mean()
                std = data['Close'].rolling(window=20).std()
                upper_band = sma + (std * 2)
                lower_band = sma - (std * 2)
                addplot.append(mpf.make_addplot(upper_band, color='green', linestyle='dashed'))
                addplot.append(mpf.make_addplot(lower_band, color='green', linestyle='dashed'))

        fig, axlist = mpf.plot(data, type='candle', style=my_style, volume=True, addplot=addplot, returnfig=True)
        fig.suptitle(title, y=0.98)

        # Save chart image
        fig.savefig(filename, dpi=300)
        plt.close(fig)

        # Open and add financial data to the image
        image = Image.open(filename)
        draw = ImageDraw.Draw(image)
        font = ImageFont.load_default()  # Use default font, you can also use custom fonts if available

        # Financial metrics to add
        metrics = {
            "Ticker": ticker,
            "Latest Close": f"${data['Close'].iloc[-1]:,.2f}",
            "Volume": f"{data['Volume'].iloc[-1]:,.0f}"
        }

        # Add additional metrics if indicators are present
        if 'SMA21' in indicators:
            metrics["SMA 21"] = f"${data['Close'].rolling(window=21).mean().iloc[-1]:,.2f}"
        if 'SMA50' in indicators:
            metrics["SMA 50"] = f"${data['Close'].rolling(window=50).mean().iloc[-1]:,.2f}"
        if 'SMA200' in indicators:
            metrics["SMA 200"] = f"${data['Close'].rolling(window=200).mean().iloc[-1]:,.2f}"

        # Draw metrics on the image
        y_text = image.height - 50  # Starting y position for text
        for key, value in metrics.items():
            text = f"{key}: {value}"
            draw.text((10, y_text), text, font=font, fill=(255, 255, 255))  # White color text
            y_text += 20

        # Resize image
        new_size = (image.width * 3, image.height * 3)
        resized_image = image.resize(new_size, Image.LANCZOS)
        resized_image.save(filename)

        logging.debug(f"Resized image with timeframe {timeframe} and ticker {ticker} saved to {filename}")
    except Exception as e:
        logging.error(f"Error creating or resizing chart: {e}")
        raise

def combine_images(image_paths, output_path='combined_chart.png'):
    try:
        logging.debug(f"Combining images {image_paths} into {output_path}")
        images = [Image.open(path) for path in image_paths]

        # Calculate total width and max height for combined image
        total_width = sum(img.width for img in images)
        max_height = max(img.height for img in images)

        combined_image = Image.new('RGB', (total_width, max_height))
        x_offset = 0
        for img in images:
            combined_image.paste(img, (x_offset, 0))
            x_offset += img.width

        combined_image.save(output_path)
        logging.debug(f"Combined image saved to {output_path}")
        return output_path
    except Exception as e:
        logging.error(f"Error combining images: {e}")
        raise

def perform_trend_analysis(data):
    # Perform trend analysis
    close_prices = data['Close']
    time_index = np.arange(len(close_prices))
    slope, intercept, r_value, p_value, std_err = stats.linregress(time_index, close_prices)
    
    trend_line = slope * time_index + intercept
    
    plt.figure(figsize=(12, 6))
    plt.plot(close_prices.index, close_prices, label='Close Price')
    plt.plot(close_prices.index, trend_line, color='red', label='Trend Line')
    plt.title('Trend Analysis')
    plt.xlabel('Date')
    plt.ylabel('Price')
    plt.legend()
    
    trend_filename = 'trend_analysis.png'
    plt.savefig(trend_filename)
    plt.close()
    
    trend_strength = abs(r_value)
    trend_direction = "upward" if slope > 0 else "downward"
    
    analysis_text = f"Trend Analysis:\n"
    analysis_text += f"The stock shows a {trend_direction} trend.\n"
    analysis_text += f"Trend strength (R-squared): {trend_strength:.2f}\n"
    analysis_text += f"Slope: {slope:.4f}\n"
    
    return analysis_text, trend_filename

def perform_correlation_analysis(data_dict):
    # Perform correlation analysis
    combined_data = pd.DataFrame({ticker: data['Close'] for ticker, data in data_dict.items()})
    correlation_matrix = combined_data.corr()
    
    plt.figure(figsize=(10, 8))
    sns.heatmap(correlation_matrix, annot=True, cmap='coolwarm', vmin=-1, vmax=1, center=0)
    plt.title('Correlation Analysis')
    
    corr_filename = 'correlation_analysis.png'
    plt.savefig(corr_filename)
    plt.close()
    
    analysis_text = f"Correlation Analysis:\n"
    for i in range(len(correlation_matrix.columns)):
        for j in range(i+1, len(correlation_matrix.columns)):
            ticker1 = correlation_matrix.columns[i]
            ticker2 = correlation_matrix.columns[j]
            corr = correlation_matrix.iloc[i, j]
            analysis_text += f"Correlation between {ticker1} and {ticker2}: {corr:.2f}\n"
    
    return analysis_text, corr_filename

def comprehensive_investment_strategy():
    strategy = """
    Comprehensive Investment Strategy Analysis:

    1. Fundamental Analysis:
    - Assess financial health using key metrics like P/E ratio, EPS growth, debt-to-equity ratio, and free cash flow.
    - Analyze quarterly and annual earnings reports, focusing on revenue growth, profit margins, and management guidance.
    - Consider industry trends such as technological disruption, regulatory changes, and shifting consumer preferences.

    2. Technical Analysis:
    - Utilize chart patterns like head and shoulders, double tops/bottoms, and cup and handle.
    - Employ technical indicators including Moving Averages, RSI, MACD, and Bollinger Bands.
    - Incorporate volume analysis to confirm trend strength and potential reversals.

    3. Macroeconomic Analysis:
    - Monitor key economic indicators: GDP growth, inflation rates, unemployment figures, and consumer sentiment indices.
    - Track central bank policies, particularly interest rate decisions and quantitative easing programs.
    - Evaluate geopolitical events' impact through news analysis and global market correlations.

    4. Risk Management:
    - Implement diversification across sectors, geographies, and asset classes.
    - Use position sizing based on account size and individual stock volatility.
    - Set stop-loss orders at key technical levels, typically 5-15% below purchase price depending on stock volatility.

    5. Sentiment Analysis:
    - Gauge market sentiment through tools like the VIX, put/call ratio, and investor surveys.
    - Monitor social media trends and financial news sentiment using natural language processing tools.
    - Apply contrarian strategies when extreme bullish or bearish sentiment is detected, supported by fundamental and technical analysis.

    6. Options Trading:
    - Employ covered calls for income generation on long-term holds.
    - Use protective puts to hedge downside risk on larger positions.
    - Implement iron condors or credit spreads to take advantage of high implied volatility environments.

    7. Long-Term Investing:
    - Identify companies with strong competitive advantages, consistent revenue growth, and solid balance sheets.
    - Focus on businesses with high return on invested capital (ROIC) and effective management teams.
    - Include dividend aristocrats and growth stocks at a reasonable price (GARP) for a balanced approach.

    8. Market Psychology:
    - Apply principles of behavioral finance, recognizing common biases like herd mentality and recency bias.
    - Maintain a trading journal to track decisions and emotions, promoting self-awareness and improvement.
    - Develop and stick to a rules-based system to minimize emotional decision-making.

    This comprehensive strategy aims to balance various analytical approaches, providing a robust framework for investment decisions across different market conditions.
    """
    return strategy

def analyze_uploaded_image(image, query):
    try:
        logging.debug(f"Analyzing uploaded image with query: {query}")
        insights = predict(image, query)
        return insights, image
    except Exception as e:
        logging.error(f"Error analyzing uploaded image: {e}")
        return f"Error analyzing uploaded image: {e}", None

def gradio_interface(ticker1, ticker2, ticker3, ticker4, start_date, end_date, query, analysis_type, interval, indicators, uploaded_image):
    try:
        logging.debug(f"Starting gradio_interface with tickers: {ticker1}, {ticker2}, {ticker3}, {ticker4}, start_date: {start_date}, end_date: {end_date}, query: {query}, analysis_type: {analysis_type}, interval: {interval}")

        if analysis_type == 'Comprehensive Investment Strategy':
            return comprehensive_investment_strategy(), None

        if uploaded_image is not None:
            return analyze_uploaded_image(uploaded_image, query)

        tickers = [ticker.split(':')[0].strip() for ticker in [ticker1, ticker2, ticker3, ticker4] if ticker]
        chart_paths = []
        data_dict = {}

        for i, ticker in enumerate(tickers):
            if ticker:
                data = fetch_stock_data(ticker, start=start_date, end=end_date, interval=interval)
                if data.empty:
                    return f"No data available for {ticker} in the specified date range.", None
                data_dict[ticker] = data
                with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_chart:
                    chart_path = temp_chart.name
                    create_stock_chart(data, ticker, chart_path, timeframe=interval, indicators=indicators)
                    chart_paths.append(chart_path)

        if analysis_type == 'Comparative Analysis' and len(chart_paths) > 1:
            with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_combined_chart:
                combined_chart_path = temp_combined_chart.name
                combine_images(chart_paths, combined_chart_path)
                insights = predict(Image.open(combined_chart_path), query)
                return insights, combined_chart_path
        elif analysis_type == 'Trend Analysis':
            if len(data_dict) > 0:
                first_ticker = list(data_dict.keys())[0]
                analysis_text, trend_chart = perform_trend_analysis(data_dict[first_ticker])
                return analysis_text, trend_chart
            else:
                return "No data available for trend analysis.", None
        elif analysis_type == 'Correlation Analysis':
            if len(data_dict) > 1:
                analysis_text, corr_chart = perform_correlation_analysis(data_dict)
                return analysis_text, corr_chart
            else:
                return "At least two tickers are required for correlation analysis.", None
        else:
            # Single ticker analysis
            if chart_paths:
                insights = predict(Image.open(chart_paths[0]), query)
                return insights, chart_paths[0]
            else:
                return "No tickers provided.", None
    except Exception as e:
        logging.error(f"Error in Gradio interface: {e}")
        return f"Error processing image or query: {e}", None

def gradio_app():
    with gr.Blocks() as demo:
        gr.Markdown("""
        ## 📈 Advanced Stock Analysis Dashboard

        This application provides a comprehensive stock analysis tool that allows users to input up to four stock tickers, specify date ranges, and select various financial indicators. The core functionalities include:

        1. **Data Fetching and Chart Creation**: Historical stock data is fetched from Yahoo Finance, and candlestick charts are generated with optional financial indicators like RSI, SMA, VWAP, and Bollinger Bands.

        2. **Text Analysis and Insights Generation**: The application uses a pre-trained model based on the Paligema architecture to analyze the input chart and text query, generating insightful analysis based on the provided financial data and context.

        3. **Trend Analysis**: Performs trend analysis on a single stock, showing the trend line and providing information about the trend strength and direction.

        4. **Correlation Analysis**: Analyzes the correlation between multiple stocks, providing a correlation matrix and heatmap.

        5. **Comprehensive Investment Strategy**: Provides a detailed investment strategy based on fundamental analysis, technical analysis, macroeconomic factors, risk management, and more.

        6. **User Interface**: Users can interactively select stocks, date ranges, intervals, and indicators. The app supports single ticker analysis, comparative analysis, trend analysis, correlation analysis, and comprehensive investment strategy.

        7. **Logging and Debugging**: Detailed logging helps in debugging and tracking the application's processes.

        8. **Enhanced Image Processing**: The app adds financial metrics and annotations to the generated charts, ensuring clear presentation of data.

        9. **Custom Chart Analysis**: Users can upload their own chart images for analysis.

        This tool leverages various analysis techniques to provide detailed insights into stock market trends, offering an interactive and educational experience for users.
        """)

        ticker_options = [
            "ES=F: E-Mini S&P 500 Sep 24",
            "YM=F: Mini Dow Jones Indus.-$5 Sep 24",
            "NQ=F: Nasdaq 100 Sep 24",
            "RTY=F: E-mini Russell 2000 Index Future",
            "ZB=F: U.S. Treasury Bond Futures, Sep-",
            "ZN=F: 10-Year T-Note Futures, Sep-2024",
            "ZF=F: Five-Year US Treasury Note Future",
            "ZT=F: 2-Year T-Note Futures, Sep-2024",
            "GC=F: Gold",
            "MGC=F: Micro Gold Futures, Dec-2024",
            "SI=F: Silver",
            "SIL=F: Micro Silver Futures, Sep-2024",
            "PL=F: Platinum Oct 24",
            "HG=F: Copper Sep 24",
            "PA=F: Palladium Sep 24",
            "CL=F: Crude Oil",
            "HO=F: Heating Oil Aug 24",
            "NG=F: Natural Gas Aug 24",
            "RB=F: RBOB Gasoline Aug 24",
            "BZ=F: Brent Crude Oil Last Day Financ",
            "BO=F: Mont Belvieu LDH Propane (OPIS)",
            "ZC=F: Corn Futures, Dec-2024",
            "ZO=F: Oat Futures, Dec-2024",
            "KE=F: KC HRW Wheat Futures, Sep-2024",
            "ZR=F: Rough Rice Futures, Sep-2024",
            "ZM=F: S&P Composite 1500 ESG Tilted I",
            "ZL=F: Soybean Oil Futures, Dec-2024",
            "ZS=F: Soybean Futures, Nov-2024",
            "GF=F: WisdomTree International High D",
            "HE=F: Lean Hogs Futures, Aug-2024",
            "LE=F: Live Cattle Futures, Aug-2024",
            "CC=F: Cocoa Sep 24",
            "KC=F: Coffee Sep 24",
            "CT=F: Cotton Oct 24",
            "LBS=F: Random Length Lumber Futures",
            "OJ=F: Orange Juice Sep 24",
            "^IRX: 13 WEEK TREASURY BILL",
            "^FVX: Treasury Yield 5 Years",
            "^TNX: CBOE Interest Rate 10 Year T No",
            "^TYX: Treasury Yield 30 Years",
            "EURUSD=X: EUR/USD",
            "JPY=X: USD/JPY",
            "GBPUSD=X: GBP/USD",
            "AUDUSD=X: AUD/USD",
            "NZDUSD=X: NZD/USD",
            "EURJPY=X: EUR/JPY",
            "GBPJPY=X: GBP/JPY",
            "EURGBP=X: EUR/GBP",
            "EURCAD=X: EUR/CAD",
            "EURSEK=X: EUR/SEK",
            "EURCHF=X: EUR/CHF",
            "EURHUF=X: EUR/HUF",
            "EURJPY=X: EUR/JPY",
            "CNY=X: USD/CNY",
            "HKD=X: USD/HKD",
            "SGD=X: USD/SGD",
            "INR=X: USD/INR",
            "MXN=X: USD/MXN",
            "PHP=X: USD/PHP",
            "IDR=X: USD/IDR",
            "THB=X: USD/THB",
            "MYR=X: USD/MYR",
            "ZAR=X: USD/ZAR",
            "RUB=X: USD/RUB",
            "BTC-USD: Bitcoin USD",
            "ETH-USD: Ethereum USD",
            "USDT-USD: Tether USDT USD",
            "BNB-USD: BNB USD",
            "SOL-USD: Solana USD",
            "USDC-USD: USD Coin USD",
            "XRP-USD: XRP",
            "STETH-USD: Lido Staked ETH USD",
            "DOGE-USD: Dogecoin USD",
            "TON11419-USD: Toncoin USD",
            "ADA-USD: Cardano USD",
            "WSTETH-USD: Lido wstETH USD",
            "WTRX-USD: Wrapped TRON USD",
            "TRX-USD: TRON USD",
            "AVAX-USD: Avalanche USD",
            "WETH-USD: Wrapped Bitcoin USD",
            "SHIB-USD: Shiba Inu USD",
            "DOT-USD: Polkadot USD",
            "LINK-USD: Chainlink USD",
            "BCH-USD: Bitcoin Cash USD",
            "EDLC-USD: Edelman USD",
            "NEAR-USD: NEAR Protocol USD",
            "EETH-USD: staked.fi ETH USD",
            "LEO-USD: UNUS SED LEO USD",
            "TSLA: Tesla, Inc.",
            "NVDA: NVIDIA Corporation",
            "F: Ford Motor Company",
            "DXCM: DexCom, Inc.",
            "SWN: Southwestern Energy Company",
            "AAL: American Airlines Group Inc.",
            "AMD: Advanced Micro Devices, Inc.",
            "PLUG: Plug Power Inc.",
            "BAC: Bank of America Corporation",
            "MARA: Marathon Digital Holdings, Inc.",
            "GOOGL: Alphabet Inc.",
            "AAPL: Apple Inc.",
            "MMM: 3M Company",
            "NKE: NIKE, Inc.",
            "PFE: Pfizer Inc.",
            "SMYMY: Sirius XM Holdings Inc.",
            "INTC: Intel Corporation",
            "SOFI: SoFi Technologies, Inc.",
            "BTGR: BTS Group Holdings Public Company Limited",
            "LCID: Lucid Group, Inc.",
            "PLTR: Palantir Technologies Inc.",
            "NWL: Newell Brands Inc.",
            "VALE: Vale S.A.",
            "CLSK: CleanSpark, Inc.",
            "BTE: Baytex Energy Corp.",
            "ASTS: AST SpaceMobile, Inc.",
            "ET: Energy Transfer LP",
            "AMZN: Amazon.com, Inc.",
            "HBI: Hanesbrands Inc.",
            "CMG: Chipotle Mexican Grill, Inc.",
            "GOOG: Alphabet Inc.",
            "AGNC: AGNC Investment Corp.",
            "RIVN: Rivian Automotive, Inc.",
            "GOLD: Barrick Gold Corporation",
            "AVTR: Avantor, Inc.",
            "CMCSA: Comcast Corporation",
            "RIG: Transocean Ltd.",
            "MUV2: Micron Technology, Inc.",
            "WBD: Warner Bros. Discovery, Inc.",
            "AVGO: Broadcom Inc.",
            "CCL: Carnival Corporation & plc",
            "MSFT: Microsoft Corporation",
            "WMT: Walmart Inc.",
            "HBAN: Huntington Bancshares Incorporated",
            "GM: General Motors Company",
            "BN: Brookfield Corporation",
            "CSCO: Cisco Systems, Inc.",
            "ERIC: Telefonaktiebolaget LM Ericsson (publ)",
            "INFY: Infosys Limited"
        ]

        with gr.Row():
            ticker1 = gr.Dropdown(label="Primary Ticker", choices=ticker_options, value="AAPL: Apple Inc.")
            ticker2 = gr.Dropdown(label="Secondary Ticker", choices=ticker_options, value="MSFT: Microsoft Corporation")
            ticker3 = gr.Dropdown(label="Third Ticker", choices=ticker_options, value="GOOGL: Alphabet Inc.")
            ticker4 = gr.Dropdown(label="Fourth Ticker", choices=ticker_options, value="AMZN: Amazon.com, Inc.")

        with gr.Row():
            start_date = gr.Textbox(label="Start Date", value="2022-01-01")
            end_date = gr.Textbox(label="End Date", value=datetime.date.today().isoformat())
            interval = gr.Dropdown(label="Interval", choices=['1m', '2m', '5m', '15m', '30m', '60m', '90m', '1h', '1d', '5d', '1wk', '1mo', '3mo'], value='1d')

        with gr.Row():
            indicators = gr.CheckboxGroup(label="Indicators", choices=['RSI', 'SMA21', 'SMA50', 'SMA200', 'VWAP', 'Bollinger Bands'], value=['SMA21', 'SMA50'])
            analysis_type = gr.Radio(label="Analysis Type", choices=['Single Ticker', 'Comparative Analysis', 'Trend Analysis', 'Correlation Analysis', 'Comprehensive Investment Strategy', 'Custom Chart Analysis'], value='Single Ticker')

        query = gr.Textbox(label="Analysis Query", value="Analyze the price trends.")
        uploaded_image = gr.Image(label="Upload Custom Chart (optional)", type="pil")
        analyze_button = gr.Button("Analyze")
        output_image = gr.Image(label="Analysis Chart")
        output_text = gr.Textbox(label="Generated Insights", lines=10)

        analyze_button.click(
            fn=gradio_interface,
            inputs=[ticker1, ticker2, ticker3, ticker4, start_date, end_date, query, analysis_type, interval, indicators, uploaded_image],
            outputs=[output_text, output_image]
        )

    demo.launch()

if __name__ == "__main__":
    gradio_app()