Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ import requests
|
|
7 |
from sklearn.ensemble import RandomForestClassifier
|
8 |
from textblob import TextBlob
|
9 |
import yfinance as yf
|
|
|
10 |
|
11 |
# --- Constants ---
|
12 |
CRYPTO_SYMBOLS = ["BTC-USD", "ETH-USD", "LTC-USD", "XRP-USD"]
|
@@ -302,59 +303,93 @@ def analyze_market(market_type, symbol, interval, forecast_steps, daily_seasonal
|
|
302 |
forecast_df_display.rename(columns={"ds": "Date", "yhat": "Forecast", "yhat_lower": "Lower Bound", "yhat_upper": "Upper Bound"}, inplace=True)
|
303 |
return forecast_plot, tech_plot, rsi_plot, macd_plot, forecast_df_display, growth_label, error_message, sentiment_score
|
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 |
-
seasonality_mode_dd,
|
353 |
-
changepoint_scale_slider,
|
354 |
-
sentiment_keyword_txt,
|
355 |
-
],
|
356 |
-
outputs=[forecast_plot, tech_plot, rsi_plot, macd_plot, forecast_df, growth_label_output, gr.Label(label="Error Message"), sentiment_label_output]
|
357 |
)
|
358 |
|
359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
360 |
demo.launch()
|
|
|
7 |
from sklearn.ensemble import RandomForestClassifier
|
8 |
from textblob import TextBlob
|
9 |
import yfinance as yf
|
10 |
+
import re
|
11 |
|
12 |
# --- Constants ---
|
13 |
CRYPTO_SYMBOLS = ["BTC-USD", "ETH-USD", "LTC-USD", "XRP-USD"]
|
|
|
303 |
forecast_df_display.rename(columns={"ds": "Date", "yhat": "Forecast", "yhat_lower": "Lower Bound", "yhat_upper": "Upper Bound"}, inplace=True)
|
304 |
return forecast_plot, tech_plot, rsi_plot, macd_plot, forecast_df_display, growth_label, error_message, sentiment_score
|
305 |
|
306 |
+
def chatbot_response(message, history):
|
307 |
+
market_type = "Crypto" # Default market type
|
308 |
+
symbol = "BTC-USD" # Default symbol
|
309 |
+
interval = "1h" # Default interval
|
310 |
+
forecast_steps = DEFAULT_FORECAST_STEPS
|
311 |
+
daily_seasonality = DEFAULT_DAILY_SEASONALITY
|
312 |
+
weekly_seasonality = DEFAULT_WEEKLY_SEASONALITY
|
313 |
+
yearly_seasonality = DEFAULT_YEARLY_SEASONALITY
|
314 |
+
seasonality_mode = DEFAULT_SEASONALITY_MODE
|
315 |
+
changepoint_prior_scale = DEFAULT_CHANGEPOINT_PRIOR_SCALE
|
316 |
+
sentiment_keyword = ""
|
317 |
+
|
318 |
+
# Simple keyword parsing - improve this for more robust parsing
|
319 |
+
message_lower = message.lower()
|
320 |
+
if "stock" in message_lower:
|
321 |
+
market_type = "Stock"
|
322 |
+
symbol = "AAPL" # Default stock symbol
|
323 |
+
elif "crypto" in message_lower:
|
324 |
+
market_type = "Crypto"
|
325 |
+
symbol = "BTC-USD" # Default crypto symbol
|
326 |
+
|
327 |
+
for crypto_sym in CRYPTO_SYMBOLS:
|
328 |
+
if crypto_sym.lower() in message_lower:
|
329 |
+
symbol = crypto_sym
|
330 |
+
market_type = "Crypto"
|
331 |
+
break
|
332 |
+
for stock_sym in STOCK_SYMBOLS:
|
333 |
+
if stock_sym.lower() in message_lower:
|
334 |
+
symbol = stock_sym
|
335 |
+
market_type = "Stock"
|
336 |
+
break
|
337 |
+
|
338 |
+
for intv in INTERVAL_OPTIONS:
|
339 |
+
if intv in message_lower:
|
340 |
+
interval = intv
|
341 |
+
break
|
342 |
+
|
343 |
+
forecast_steps_match = re.search(r'forecast\s*(\d+)\s*steps', message_lower)
|
344 |
+
if forecast_steps_match:
|
345 |
+
forecast_steps = int(forecast_steps_match.group(1))
|
346 |
+
|
347 |
+
sentiment_match = re.search(r'sentiment\s*(.+)', message_lower)
|
348 |
+
if sentiment_match:
|
349 |
+
sentiment_keyword = sentiment_match.group(1).strip()
|
350 |
+
|
351 |
+
plots, tech_plot, rsi_plot, macd_plot, forecast_df, growth_label, error_message, sentiment_score = analyze_market(
|
352 |
+
market_type, symbol, interval, forecast_steps, daily_seasonality, weekly_seasonality, yearly_seasonality, seasonality_mode, changepoint_prior_scale, sentiment_keyword
|
|
|
|
|
|
|
|
|
|
|
353 |
)
|
354 |
|
355 |
+
response = ""
|
356 |
+
if error_message:
|
357 |
+
response += f"Error: {error_message}\n\n"
|
358 |
+
|
359 |
+
if plots and not error_message:
|
360 |
+
response += "Here is the price forecast plot.\n\n" # In a real chatbot, you might provide a link or embed the plot if possible.
|
361 |
+
else:
|
362 |
+
response += "Could not generate forecast plot.\n\n"
|
363 |
+
|
364 |
+
if tech_plot and rsi_plot and macd_plot and not error_message:
|
365 |
+
response += "Technical analysis plots (Bollinger Bands, RSI, MACD) are generated.\n\n" # Again, link or embed plots in a real chatbot
|
366 |
+
else:
|
367 |
+
response += "Could not generate technical analysis plots.\n\n"
|
368 |
+
|
369 |
+
if not error_message:
|
370 |
+
response += f"Explosive Growth Prediction: {growth_label}\n"
|
371 |
+
response += f"Sentiment Score (for keyword '{sentiment_keyword}'): {sentiment_score:.2f}\n"
|
372 |
+
|
373 |
+
if not forecast_df.empty:
|
374 |
+
# Summarize forecast data instead of displaying the full dataframe in text
|
375 |
+
forecast_summary = forecast_df.tail().to_string() # Just showing last few rows as summary
|
376 |
+
response += "\nForecast Data Summary (last few points):\n" + forecast_summary + "\n"
|
377 |
+
else:
|
378 |
+
response += "\nNo forecast data available.\n"
|
379 |
+
|
380 |
+
return response
|
381 |
+
|
382 |
+
with gr.ChatInterface(
|
383 |
+
chatbot_response,
|
384 |
+
title="Market Analysis Chatbot",
|
385 |
+
description="Ask me about crypto or stock market analysis. For example, try: 'Analyze crypto BTC-USD 1d forecast 30 steps sentiment Bitcoin' or 'Stock AAPL 1h analysis'.",
|
386 |
+
examples=[
|
387 |
+
"Analyze crypto ETH-USD 1h",
|
388 |
+
"Stock MSFT 1d forecast 10 steps",
|
389 |
+
"Crypto LTC-USD 1wk sentiment Litecoin",
|
390 |
+
"Analyze stock GOOGL",
|
391 |
+
"What about crypto XRP-USD?",
|
392 |
+
],
|
393 |
+
theme=gr.themes.Base()
|
394 |
+
) as demo:
|
395 |
demo.launch()
|