|
|
|
from huggingface_hub import InferenceClient |
|
import requests |
|
from bs4 import BeautifulSoup |
|
import lxml |
|
import gradio as gr |
|
import os |
|
|
|
client = InferenceClient(token=os.getenv("HF_TOKEN")) |
|
secretkey = os.getenv("FMP_KEY") |
|
|
|
|
|
BOT_AVATAR = 'https://automatedstockmining.org/wp-content/uploads/2024/09/logo.webp' |
|
|
|
|
|
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} |
|
|
|
''' |
|
|
|
|
|
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] |
|
|
|
readable_text = "" |
|
|
|
|
|
for key, value in json_data.items(): |
|
readable_text += f"The {key.replace('_', ' ')} is {value}. " |
|
|
|
return readable_text |
|
|
|
|
|
def detect_data_request(user_input, history): |
|
youreq = user_input |
|
|
|
gr.Info('thinking about how to respond') |
|
|
|
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 |
|
) |
|
|
|
|
|
full_response = response.choices[0].message['content'] |
|
first_resp = full_response[:3].lower() |
|
url_resp = full_response[3:] |
|
|
|
if first_resp == 'yes': |
|
try: |
|
|
|
|
|
requestfromfp = requests.get(url_resp) |
|
requestfromfp.raise_for_status() |
|
jsoned_data = requestfromfp.json() |
|
gr.Info('located an endpoint and retrieving and processing the data you want') |
|
|
|
human_readable_text = json_to_text(jsoned_data) |
|
stringed_text = str(human_readable_text) |
|
|
|
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: |
|
|
|
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:] |
|
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) |
|
|
|
|
|
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, "" |
|
|
|
|
|
def clear_history(): |
|
return [], "" |
|
|
|
|
|
def undo_last(history): |
|
if len(history) > 0: |
|
history.pop() |
|
return history, "" |
|
|
|
|
|
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) |
|
|
|
|
|
with gr.Row(): |
|
user_input = gr.Textbox(placeholder="Type your message here...", label=None, show_label=False) |
|
send_button = gr.Button("Send") |
|
|
|
|
|
with gr.Row(): |
|
clear_button = gr.Button("Clear Chat") |
|
undo_button = gr.Button("Undo Last") |
|
|
|
|
|
send_button.click( |
|
detect_data_request, |
|
inputs=[user_input, chatbot_display], |
|
outputs=[chatbot_display, user_input] |
|
) |
|
|
|
|
|
user_input.submit( |
|
detect_data_request, |
|
inputs=[user_input, chatbot_display], |
|
outputs=[chatbot_display, user_input] |
|
) |
|
|
|
|
|
clear_button.click( |
|
clear_history, |
|
outputs=[chatbot_display, user_input] |
|
) |
|
|
|
|
|
undo_button.click( |
|
undo_last, |
|
inputs=[chatbot_display], |
|
outputs=[chatbot_display, user_input] |
|
) |
|
|
|
|
|
demo.launch() |
|
|