MarketX / app.py
ApaCu's picture
Create app.py
457ceff verified
import streamlit as st
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from transformers import pipeline
from datetime import datetime, timedelta
# Sentiment Analyzer
sentiment_model = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
st.title("AI Market Analysis")
ticker = st.text_input("Enter Stock/Crypto Ticker", value="AAPL")
if st.button("Analyze"):
try:
# Get market data
data = yf.download(ticker, period="6mo")
data = data[['Close']].dropna()
data['Days'] = range(len(data))
# Model Prediksi Sederhana
model = LinearRegression()
model.fit(data[['Days']], data['Close'])
data['Predicted'] = model.predict(data[['Days']])
# Plot Harga
fig, ax = plt.subplots()
data['Close'].plot(ax=ax, label="Actual")
data['Predicted'].plot(ax=ax, label="Predicted")
ax.set_title(f"{ticker} Price Analysis")
ax.legend()
st.pyplot(fig)
# Dummy news (karena gak scrapping realtime news dulu)
st.subheader("News Sentiment Analysis (Sample Headlines)")
headlines = [
f"{ticker} stock rises after positive earnings report",
f"Market analysts are uncertain about {ticker} future",
f"{ticker} faces regulatory challenges in new markets"
]
for h in headlines:
result = sentiment_model(h)[0]
st.write(f"**{h}** β†’ `{result['label']}` ({round(result['score'], 2)})")
except Exception as e:
st.error(f"Error: {e}")