File size: 25,191 Bytes
c250a6a
f2878a6
c250a6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2222caa
c250a6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2222caa
c250a6a
 
 
 
2222caa
c250a6a
 
442cee7
c250a6a
 
6c1699a
32faf89
 
c250a6a
6c1699a
 
 
 
 
c250a6a
32faf89
c250a6a
 
 
 
9d49480
c250a6a
 
 
 
07c10bd
c250a6a
 
 
 
 
 
 
 
f1816c4
c250a6a
 
 
 
 
 
 
 
 
 
 
 
a40de97
c250a6a
 
 
 
 
 
 
 
 
 
2222caa
c250a6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2878a6
c250a6a
 
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

from huggingface_hub import InferenceClient
import requests
from bs4 import BeautifulSoup
import lxml
import gradio as gr
import os
# Initialize the InferenceClient
client = InferenceClient(token=os.getenv("HF_TOKEN"))
secretkey = os.getenv("FMP_KEY")

# BOT avatar for chat interface
BOT_AVATAR = 'https://automatedstockmining.org/wp-content/uploads/2024/09/logo.webp'

# API Documentation (placed outside the function)
data = '''
# Stock List API
stock_list_api = f"https://financialmodelingprep.com/api/v3/stock/list?apikey={secretkey}"

# Exchange Traded Fund Search API
etf_search_api = f"https://financialmodelingprep.com/api/v3/etf/list?apikey={secretkey}"

# Statement Symbols List API
statement_symbols_list_api = f"https://financialmodelingprep.com/api/v3/financial-statement-symbol-lists?apikey={secretkey}"

# Tradable Search API
tradable_search_api = f"https://financialmodelingprep.com/api/v3/available-traded/list?apikey={secretkey}"

# Commitment of Traders Report API
commitment_of_traders_report_api = f"https://financialmodelingprep.com/api/v4/commitment_of_traders_report/list?apikey={secretkey}"

# CIK List API
cik_list_api = f"https://financialmodelingprep.com/api/v3/cik_list?apikey={secretkey}"

# Euronext Symbols API
euronext_symbols_api = f"https://financialmodelingprep.com/api/v3/symbol/available-euronext?apikey={secretkey}"

# Symbol Changes API
symbol_changes_api = f"https://financialmodelingprep.com/api/v4/symbol_change?apikey={secretkey}"

# Exchange Symbols API
exchange_symbols_api = f"https://financialmodelingprep.com/api/v3/symbol/{exchange}?apikey={secretkey}"

# Available Indexes API
available_indexes_api = f"https://financialmodelingprep.com/api/v3/symbol/available-indexes?apikey={secretkey}"

# Company Profile API
company_profile_api = f"https://financialmodelingprep.com/api/v3/profile/{symbol}?apikey={secretkey}"

# Executive Compensation API
executive_compensation_api = f"https://financialmodelingprep.com/api/v4/governance/executive_compensation?symbol={symbol}&apikey={secretkey}"

# Compensation Benchmark API
compensation_benchmark_api = f"https://financialmodelingprep.com/api/v4/executive-compensation-benchmark?year={year}&apikey={secretkey}"

# Company Notes API
company_notes_api = f"https://financialmodelingprep.com/api/v4/company-notes?symbol={symbol}&apikey={secretkey}"

# Historical Employee API
historical_employee_api = f"https://financialmodelingprep.com/api/v4/historical/employee_count?symbol={symbol}&apikey={secretkey}"

# Employee Count API
employee_count_api = f"https://financialmodelingprep.com/api/v4/employee_count?symbol={symbol}&apikey={secretkey}"

# Screener (Stock) API
stock_screener_api = f"https://financialmodelingprep.com/api/v3/stock-screener?apikey={secretkey}"

# Stock Grade API
stock_grade_api = f"https://financialmodelingprep.com/api/v3/grade/{symbol}?apikey={secretkey}"

# Executives API
executives_api = f"https://financialmodelingprep.com/api/v3/key-executives/{symbol}?apikey={secretkey}"

# Company Core Information Summary API
company_core_info_summary_api = f"https://financialmodelingprep.com/api/v4/company-core-information?symbol={symbol}&apikey={secretkey}"

# Market Cap API
market_cap_api = f"https://financialmodelingprep.com/api/v3/market-capitalization/{symbol}?apikey={secretkey}"

# Historical Market Cap API
historical_market_cap_api = f"https://financialmodelingprep.com/api/v3/historical-market-capitalization/{symbol}?limit={limit}&from={start_date}&to={end_date}&apikey={secretkey}"

# All Countries API
all_countries_api = f"https://financialmodelingprep.com/api/v3/get-all-countries?apikey={secretkey}"

# Analyst Estimates API
analyst_estimates_api = f"https://financialmodelingprep.com/api/v3/analyst-estimates/{symbol}?apikey={secretkey}"

# Analyst Recommendation API
analyst_recommendation_api = f"https://financialmodelingprep.com/api/v3/analyst-stock-recommendations/{symbol}?apikey={secretkey}"

# Company Logo API
company_logo_api = f"https://financialmodelingprep.com/image-stock/{symbol}.png?apikey={secretkey}"

# Company Outlook API
company_outlook_api = f"https://financialmodelingprep.com/api/v4/company-outlook?symbol={symbol}&apikey={secretkey}"

# Stock Peers API
stock_peers_api = f"https://financialmodelingprep.com/api/v4/stock_peers?symbol={symbol}&apikey={secretkey}"

# Holidays and Trading Hours API
market_open_api = f"https://financialmodelingprep.com/api/v3/is-the-market-open?exchange={exchange}&apikey={secretkey}"

# All Exchanges Trading Hours API
all_exchanges_trading_hours_api = f"https://financialmodelingprep.com/api/v3/is-the-market-open-all?apikey={secretkey}"

# Delisted Companies API
delisted_companies_api = f"https://financialmodelingprep.com/api/v3/delisted-companies?apikey={secretkey}"

# Company Share Float API
company_share_float_api = f"https://financialmodelingprep.com/api/v4/shares_float?symbol={symbol}&apikey={secretkey}"

# Historical Share Float API
historical_share_float_api = f"https://financialmodelingprep.com/api/v4/historical/shares_float?symbol={symbol}&apikey={secretkey}"

# All Shares Float API
all_shares_float_api = f"https://financialmodelingprep.com/api/v4/shares_float/all?apikey={secretkey}"

# All Available Sectors API
available_sectors_api = f"https://financialmodelingprep.com/api/v3/sectors-list?apikey={secretkey}"

# All Available Industries API
available_industries_api = f"https://financialmodelingprep.com/api/v3/industries-list?apikey={secretkey}"

# All Available Exchanges API
available_exchanges_api = f"https://financialmodelingprep.com/api/v3/exchanges-list?apikey={secretkey}"

# Full Quote API
full_quote_api = f"https://financialmodelingprep.com/api/v3/quote/{symbol}?apikey={secretkey}"

# Quote Order API
quote_order_api = f"https://financialmodelingprep.com/api/v3/quote-order/{symbol}?apikey={secretkey}"

# Simple Quote API
simple_quote_api = f"https://financialmodelingprep.com/api/v3/quote-short/{symbol}?apikey={secretkey}"

# OTC Quote API
otc_quote_api = f"https://financialmodelingprep.com/api/v3/otc/real-time-price/{symbol}?apikey={secretkey}"

# Exchange Prices API
exchange_prices_api = f"https://financialmodelingprep.com/api/v3/quotes/{exchange}?apikey={secretkey}"

# Stock Price Change API
stock_price_change_api = f"https://financialmodelingprep.com/api/v3/stock-price-change/{symbol}?apikey={secretkey}"

# Aftermarket Trade API
aftermarket_trade_api = f"https://financialmodelingprep.com/api/v4/pre-post-market-trade/{symbol}?apikey={secretkey}"

# Aftermarket Quote API
aftermarket_quote_api = f"https://financialmodelingprep.com/api/v4/pre-post-market/{symbol}?apikey={secretkey}"

# Batch Quote API
batch_quote_api = f"https://financialmodelingprep.com/api/v4/batch-pre-post-market/{symbol}?apikey={secretkey}"

# Batch Trade API
batch_trade_api = f"https://financialmodelingprep.com/api/v4/batch-pre-post-market-trade/{symbol}?apikey={secretkey}"

# Last Forex API
last_forex_api = f"https://financialmodelingprep.com/api/v4/forex/last/{symbol}?apikey={secretkey}"




# Live Full Price w/ Orders API
live_full_price_api = f"https://financialmodelingprep.com/api/v3/stock/full/real-time-price/{symbol}?apikey={secretkey}"

# All Live Full Price w/ Orders API
all_live_full_price_api = f"https://financialmodelingprep.com/api/v3/stock/full/real-time-price?apikey={secretkey}"

# Forex Prices API
forex_prices_api = f"https://financialmodelingprep.com/api/v3/fx/{symbol}?apikey={secretkey}"

# Ratios API
ratios_api = f"https://financialmodelingprep.com/api/v3/ratios/{symbol}?period={period}&apikey={secretkey}"

# Ratios TTM API
ratios_ttm_api = f"https://financialmodelingprep.com/api/v3/ratios-ttm/{symbol}?apikey={secretkey}"

# Cashflow Growth API
cashflow_growth_api = f"https://financialmodelingprep.com/api/v3/cash-flow-statement-growth/{symbol}?period={period}&apikey={secretkey}"

# Income Growth API
income_growth_api = f"https://financialmodelingprep.com/api/v3/income-statement-growth/{symbol}?period={period}&apikey={secretkey}"

# Balance Sheet Growth API
balance_sheet_growth_api = f"https://financialmodelingprep.com/api/v3/balance-sheet-statement-growth/{symbol}?period={period}&apikey={secretkey}"

# Financial Growth API
financial_growth_api = f"https://financialmodelingprep.com/api/v3/financial-growth/{symbol}?period={period}&apikey={secretkey}"

# Financial Score API
financial_score_api = f"https://financialmodelingprep.com/api/v4/score?symbol={symbol}&apikey={secretkey}"

# Owner Earnings API
owner_earnings_api = f"https://financialmodelingprep.com/api/v4/owner_earnings?symbol={symbol}&apikey={secretkey}"

# Enterprise Values API
enterprise_values_api = f"https://financialmodelingprep.com/api/v3/enterprise-values/{symbol}/?period={period}&apikey={secretkey}"

# Discounted Cashflow API
discounted_cashflow_api = f"https://financialmodelingprep.com/api/v3/discounted-cash-flow/{symbol}?apikey={secretkey}"

# Advanced DCF API
advanced_dcf_api = f"https://financialmodelingprep.com/api/v4/advanced_discounted_cash_flow?symbol={symbol}&apikey={secretkey}"

# Levered DCF API
levered_dcf_api = f"https://financialmodelingprep.com/api/v4/advanced_levered_discounted_cash_flow?symbol={symbol}&apikey={secretkey}"

# Company Rating API
company_rating_api = f"https://financialmodelingprep.com/api/v3/rating/{symbol}?apikey={secretkey}"

# FMP Articles API
fmp_articles_api = f"https://financialmodelingprep.com/api/v3/fmp/articles?page={page}&size={size}&apikey={secretkey}"

# General News API
general_news_api = f"https://financialmodelingprep.com/api/v4/general_news?page={page}&apikey={secretkey}"


# Stock News Sentiments RSS Feed API
stock_news_sentiments_rss_api = f"https://financialmodelingprep.com/api/v4/stock-news-sentiments-rss-feed?page={page}&apikey={secretkey}"

# Forex News API
forex_news_api = f"https://financialmodelingprep.com/api/v4/forex_news?page={page}&apikey={secretkey}"

# Crypto News API
crypto_news_api = f"https://financialmodelingprep.com/api/v4/crypto_news?page={page}&apikey={secretkey}"

# Press Releases API
press_releases_api = f"https://financialmodelingprep.com/api/v3/press-releases?page={page}&apikey={secretkey}"

# Press Releases By Symbol API
press_releases_by_symbol_api = f"https://financialmodelingprep.com/api/v3/press-releases/{symbol}?apikey={secretkey}"

# Historical Social Sentiment API
historical_social_sentiment_api = f"https://financialmodelingprep.com/api/v4/historical/social-sentiment?symbol={symbol}&page={page}&apikey={secretkey}"

# Trending Social Sentiment API
trending_social_sentiment_api = f"https://financialmodelingprep.com/api/v4/social-sentiments/trending?type={type}&source={source}&apikey={secretkey}"

# Social Sentiment Changes API
social_sentiment_changes_api = f"https://financialmodelingprep.com/api/v4/social-sentiments/change?type={type}&source={source}&apikey={secretkey}"

# Earnings Transcript API
earnings_transcript_api = f"https://financialmodelingprep.com/api/v3/earning_call_transcript/{symbol}?year={year}&quarter={quarter}&apikey={secretkey}"

# Transcript Dates API
transcript_dates_api = f"https://financialmodelingprep.com/api/v4/earning_call_transcript?symbol={symbol}&apikey={secretkey}"

# Batch Earning Call Transcript API
batch_earning_call_transcript_api = f"https://financialmodelingprep.com/api/v4/batch_earning_call_transcript/{symbol}?year={year}&apikey={secretkey}"

# RSS Feed API
rss_feed_api = f"https://financialmodelingprep.com/api/v4/rss_feed?limit={limit}&type={type}&from={from_date}&to={to_date}&isDone={is_done}&apikey={secretkey}"

# RSS Feed V3 API
rss_feed_v3_api = f"https://financialmodelingprep.com/api/v3/rss_feed?page={page}&datatype=csv&apikey={secretkey}"

# RSS Feed All API
rss_feed_all_api = f"https://financialmodelingprep.com/api/v4/rss_feed_all?page={page}&apikey={secretkey}"

# RSS Feed 8-K API
rss_feed_8k_api = f"https://financialmodelingprep.com/api/v4/rss_feed_8k?apikey={secretkey}"

# SEC Filings API
sec_filings_api = f"https://financialmodelingprep.com/api/v3/sec_filings/{symbol}?type={type}&page={page}&apikey={secretkey}"

# Individual Industry Classification API
individual_industry_classification_api = f"https://financialmodelingprep.com/api/v4/standard_industrial_classification?symbol={symbol}&apikey={secretkey}"

# All Industry Classification API
all_industry_classification_api = f"https://financialmodelingprep.com/api/v4/standard_industrial_classification/all?apikey={secretkey}"

# Industry Classification Codes API
industry_classification_codes_api = f"https://financialmodelingprep.com/api/v4/standard_industrial_classification_list?apikey={secretkey}"

# Earnings Calendar API
earnings_calendar_api = f"https://financialmodelingprep.com/api/v3/earning_calendar?from={from_date}&to={to_date}&apikey={secretkey}"

# Earnings Historical & Upcoming API
earnings_historical_upcoming_api = f"https://financialmodelingprep.com/api/v3/historical/earning_calendar/{symbol}?apikey={secretkey}"

# Earnings Confirmed API
earnings_confirmed_api = f"https://financialmodelingprep.com/api/v4/earning-calendar-confirmed?from={from_date}&to={to_date}&apikey={secretkey}"

# Earnings Surprises API
earnings_surprises_api = f"https://financialmodelingprep.com/api/v3/earnings-surprises/{symbol}?apikey={secretkey}"

# Dividends Calendar API
dividends_calendar_api = f"https://financialmodelingprep.com/api/v3/stock_dividend_calendar?from={from_date}&to={to_date}&apikey={secretkey}"

# Dividends Historical API
dividends_historical_api = f"https://financialmodelingprep.com/api/v3/historical-price-full/stock_dividend/{symbol}?apikey={secretkey}"

# Splits Calendar API
splits_calendar_api = f"https://financialmodelingprep.com/api/v3/stock_split_calendar?from={from_date}&to={to_date}&apikey={secretkey}"

# Splits Historical API
splits_historical_api = f"https://financialmodelingprep.com/api/v3/historical-price-full/stock_split/{symbol}?apikey={secretkey}"

# IPO Confirmed API
ipo_confirmed_api = f"https://financialmodelingprep.com/api/v4/ipo-calendar-confirmed?from={from_date}&to={to_date}&apikey={secretkey}"

# IPO Prospectus API
ipo_prospectus_api = f"https://financialmodelingprep.com/api/v4/ipo-calendar-prospectus?apikey={secretkey}"

# IPO Calendar By Symbol API
ipo_calendar_by_symbol_api = f"https://financialmodelingprep.com/api/v3/ipo_calendar?apikey={secretkey}"

# M&A RSS Feed API
ma_rss_feed_api = f"https://financialmodelingprep.com/api/v4/mergers-acquisitions-rss-feed?page={page}&apikey={secretkey}"

# Search M&A API
search_ma_api = f"https://financialmodelingprep.com/api/v4/mergers-acquisitions/search?name={company_name}&apikey={secretkey}"

# Intraday Chart API
intraday_chart_api = f"https://financialmodelingprep.com/api/v3/historical-chart/5min/{symbol}?from={from_date}&to={to_date}&apikey={secretkey}"

# Daily Chart EOD API
daily_chart_eod_api = f"https://financialmodelingprep.com/api/v3/historical-price-full/{symbol}?apikey={secretkey}"

# All Forex Prices API
all_forex_prices_api = f"https://financialmodelingprep.com/api/v3/fx?apikey={secretkey}"

# Simple Moving Average (SMA) API
sma_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=sma&period={period}&apikey={secretkey}"

# Exponential Moving Average (EMA) API
ema_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=ema&period={period}&apikey={secretkey}"

# Weighted Moving Average (WMA) API
wma_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=wma&period={period}&apikey={secretkey}"

# Double Exponential Moving Average (DEMA) API
dema_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=dema&period={period}&apikey={secretkey}"

# Triple Exponential Moving Average (TEMA) API
tema_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=tema&period={period}&apikey={secretkey}"

# Williams %R Indicator API
williams_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=williams&period={period}&apikey={secretkey}"

# Relative Strength Index (RSI) API
rsi_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=rsi&period={period}&apikey={secretkey}"

# Average Directional Index (ADX) API
adx_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=adx&period={period}&apikey={secretkey}"

# Standard Deviation API
standard_deviation_api = f"https://financialmodelingprep.com/api/v3/technical_indicator/5min/{symbol}?type=standardDeviation&period={period}&apikey={secretkey}"

#current news on a stock
current news on a stock = https://financialmodelingprep.com/api/v3/stock_news?tickers={symbol}&apikey={apikey}


# current commodities prices
commodities prices = https://financialmodelingprep.com/api/v3/historical-price-full/{symbol}?apikey={secretkey}

'''

# Helper function to convert JSON to human-readable format
def json_to_text(json_data):
    """
    Converts a JSON object into a human-readable format.
    """
    if isinstance(json_data, list):
        json_data = json_data[0]  # Process only the first element if it's a list

    readable_text = ""

    # Loop through the dictionary and create readable text
    for key, value in json_data.items():
        readable_text += f"The {key.replace('_', ' ')} is {value}. "

    return readable_text

# Main function to process user input and handle API or web scraping
def detect_data_request(user_input, history):
    youreq = user_input
    
    gr.Info('thinking about how to respond')
    # Get the response from the model (no streaming)
    response = client.chat_completion(
        model="Qwen/Qwen2.5-72B-Instruct",
        messages=[{"role": "user", "content": f"here is the fmp documentation {data}, if there is an endpoint to answer {youreq} give it, otherwise say no, give absolutely no further response. IF THE USERS REQUEST IS FOR DATA THAT COULD BE IN AN ENDPOINT. RETURN YES AND THE ENDPOINT BELOW. IF NOT RETURN NO AND NOTHING ELSE, replace secretkey with this {secretkey}. IF THE USER GIVES A SPECIFIC YEAR IN THEIR REQUEST, IGNORE THE YEAR AND GIVE THE API ENDPOINT, AS IT WILL HAVE THE INFO FOR THAT YEAR. REPLACE THE VALUES INSIDE ANY {'CURLY BRACES'} WITH THE RELEVANT INFO FOR WHAT THE USER IS ASKING FOR. AS THESE ENDPOINTS CONTAIN HISTORICAL DATA AS WELL MAKE SURE YOU RETURN AN ENDPOINT EVEN WHEN THEY ARE ASKING FOR PREVIOUS DATA LIKE FOR THE LAST WEEK AS IT IS BETTER IF YOU RETURN AN ENDPOINT AND NO DATA IS THERE, THAN IF YOU DONT RETURN ONE. YOU MUST NEVER RETURN THE SECRET KEY TO THE USER, JUST GIVE YES OR NO WITH THE ENDPOINT "}],
        max_tokens=500,
        stream=False  # Disable streaming to get the full response in one go
    )

    # Extract the response
    full_response = response.choices[0].message['content']
    first_resp = full_response[:3].lower()
    url_resp = full_response[3:]

    if first_resp == 'yes':
        try:

            # Fetch data from the provided API endpoint
            requestfromfp = requests.get(url_resp)
            requestfromfp.raise_for_status()  # Check for request errors
            jsoned_data = requestfromfp.json()
            gr.Info('located an endpoint and retrieving and processing the data you want')
            # Convert the JSON response into human-readable text
            human_readable_text = json_to_text(jsoned_data)
            stringed_text = str(human_readable_text)
            # Send the data to the LLM for further processing
            final_response = ""
            response = client.chat_completion(
                model="Qwen/Qwen2.5-72B-Instruct",
                messages=[{"role": "user", "content": f"answer {user_input} with {human_readable_text} as IM.B an AI chatbot built for investing by automatedstockmining.org"}],
                max_tokens=1500,
                stream=False
            )

            full_response = response.choices[0].message['content']
            history.append(("You: " + user_input, "IM.B: " + full_response))

            
        except:
            if requestfromfp == []:
                history.append(("You: " + user_input, "IM.B: " + 'sorry the response was empty'))
            else:
                history.append(("You: " + user_input, "IM.B: " + f'there was data in the response but for some reason we couldnt parse it '))
    else:
        # If no data endpoint exists, switch to scraping a valid webpage
        response = client.chat_completion(
            model="Qwen/Qwen2.5-72B-Instruct",
            messages=[{"role": "user", "content": f"You are an assistant that detects whether the user is asking for data or not. Here is their request: {youreq}. You respond 'yes' if they are and 'no' if not. If yes, then in the second line provide the exact URL, not an API, a valid webpage which is definitely there and easy to scrape. If no, only respond with 'no' and nothing else. if they ask for anything current, respond yes and give a url as the training database that bots like you run on may have out of date data"}],
            max_tokens=1500,
            stream=False
        )

        full_response = response.choices[0].message['content']
        if "yes" in full_response.lower():
            url = full_response[3:]  # Extract the URL part
            try:
                gr.Info('searching the web for sites that may contain the data you want')
                request = requests.get(url)
                request.raise_for_status()

                soup_of_data = BeautifulSoup(request.text, 'lxml')
                tags = ['body']
                listed_data = soup_of_data.find_all(tags)

                # Extract text from relevant tags
                extracted_content = ''.join([element.get_text(separator=" ", strip=True) for element in listed_data[:100]])

                response = client.chat_completion(
                    model="Qwen/Qwen2.5-72B-Instruct",
                    messages=[{"role": "user", "content": f"you are IM.B, an intelligent investing AI model built by automatedstockmining.org, answer this question: {youreq} with this data {extracted_content}, if the data is not there say to the user: sorry, looks like there was error processing the data on our end, please rephrase question, referencing specific websites you want the data from. YOU NEVER CONTRADICT THE DATA YOU ARE GIVEN AND SAY ITS NOT RIGHT, YOU GO OFF THE DATA YOU RECIEVE NOT YOUR KNOWLEDGE!"}],
                    max_tokens=1500,
                    stream=False
                )

                final_response = response.choices[0].message['content']
                history.append(("You: " + user_input, "IM.B: " + final_response))
            except requests.exceptions.RequestException as e:
                history.append(("You: " + user_input, f"IM.B: Error fetching URL: {e}"))

        else:
            gr.Info('getting your response ready')
            response = client.chat_completion(
                model="mistralai/Mistral-7B-Instruct-v0.3",
                messages=[{"role": "user", "content": f"you are IM.B, an intelligent investing AI model built by automatedstockmining.org however you answer whatever the user requests, answer this question as IM.B,  {youreq}, tell the user that you have access to real time data so that if they want some real time data, just ask for it"}],
                max_tokens=1500,
                stream=False
            )

            full_response = response.choices[0].message['content']
            history.append(("You: " + user_input, "IM.B: " + full_response))

    return history, ""  # Clear the input box

# Function to clear the chat history
def clear_history():
    return [], ""

# Function to undo the last user-bot message pair
def undo_last(history):
    if len(history) > 0:
        history.pop()  # Remove the last user-bot message pair
    return history, ""

# Gradio UI setup
with gr.Blocks() as demo:
    gr.Markdown("<h1 style='text-align: center;'>IM.B</h1>")

    with gr.Column():
        chatbot_display = gr.Chatbot(label="Chat History", avatar_images=[None, BOT_AVATAR], height=600)

        # User input and send button
        with gr.Row():
            user_input = gr.Textbox(placeholder="Type your message here...", label=None, show_label=False)
            send_button = gr.Button("Send")

    # Buttons for Clear and Undo
    with gr.Row():
        clear_button = gr.Button("Clear Chat")
        undo_button = gr.Button("Undo Last")

    # Define action when send button is clicked
    send_button.click(
        detect_data_request,
        inputs=[user_input, chatbot_display],
        outputs=[chatbot_display, user_input]
    )

    # Allow pressing Enter to submit the input
    user_input.submit(
        detect_data_request,
        inputs=[user_input, chatbot_display],
        outputs=[chatbot_display, user_input]
    )

    # Action for Clear Button
    clear_button.click(
        clear_history,
        outputs=[chatbot_display, user_input]
    )

    # Action for Undo Button
    undo_button.click(
        undo_last,
        inputs=[chatbot_display],
        outputs=[chatbot_display, user_input]
    )

# Launch the Gradio app
demo.launch()