MacDash commited on
Commit
1e2c87b
·
verified ·
1 Parent(s): 42e86be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -53
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
- # --- Gradio Interface ---
306
- with gr.Blocks(theme=gr.themes.Base()) as demo:
307
- gr.Markdown("# Market Analysis and Prediction")
308
-
309
- with gr.Row():
310
- with gr.Column():
311
- market_type_dd = gr.Radio(label="Market Type", choices=["Crypto", "Stock"], value="Crypto")
312
- symbol_dd = gr.Dropdown(label="Symbol", choices=CRYPTO_SYMBOLS, value="BTC-USD")
313
- interval_dd = gr.Dropdown(label="Interval", choices=INTERVAL_OPTIONS, value="1h")
314
- forecast_steps_slider = gr.Slider(label="Forecast Steps", minimum=1, maximum=100, value=DEFAULT_FORECAST_STEPS, step=1)
315
- daily_box = gr.Checkbox(label="Daily Seasonality", value=DEFAULT_DAILY_SEASONALITY)
316
- weekly_box = gr.Checkbox(label="Weekly Seasonality", value=DEFAULT_WEEKLY_SEASONALITY)
317
- yearly_box = gr.Checkbox(label="Yearly Seasonality", value=DEFAULT_YEARLY_SEASONALITY)
318
- seasonality_mode_dd = gr.Dropdown(label="Seasonality Mode", choices=["additive", "multiplicative"], value=DEFAULT_SEASONALITY_MODE)
319
- changepoint_scale_slider = gr.Slider(label="Changepoint Prior Scale", minimum=0.01, maximum=1.0, step=0.01, value=DEFAULT_CHANGEPOINT_PRIOR_SCALE)
320
- sentiment_keyword_txt = gr.Textbox(label="Sentiment Keyword (optional)")
321
-
322
- with gr.Column():
323
- forecast_plot = gr.Plot(label="Price Forecast")
324
- with gr.Row():
325
- tech_plot = gr.Plot(label="Technical Analysis")
326
- rsi_plot = gr.Plot(label="RSI Indicator")
327
- with gr.Row():
328
- macd_plot = gr.Plot(label="MACD")
329
- forecast_df = gr.Dataframe(label="Forecast Data", headers=["Date", "Forecast", "Lower Bound", "Upper Bound"])
330
- growth_label_output = gr.Label(label="Explosive Growth Prediction")
331
- sentiment_label_output = gr.Number(label="Sentiment Score")
332
-
333
- def update_symbol_choices(market_type):
334
- if market_type == "Crypto":
335
- return gr.Dropdown(choices=CRYPTO_SYMBOLS, value="BTC-USD")
336
- elif market_type == "Stock":
337
- return gr.Dropdown(choices=STOCK_SYMBOLS, value="AAPL")
338
- return gr.Dropdown(choices=[], value=None)
339
- market_type_dd.change(fn=update_symbol_choices, inputs=[market_type_dd], outputs=[symbol_dd])
340
-
341
- analyze_button = gr.Button("Analyze Market", variant="primary")
342
- analyze_button.click(
343
- fn=analyze_market,
344
- inputs=[
345
- market_type_dd,
346
- symbol_dd,
347
- interval_dd,
348
- forecast_steps_slider,
349
- daily_box,
350
- weekly_box,
351
- yearly_box,
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
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()