Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from sklearn.linear_model import LinearRegression
|
6 |
+
from transformers import pipeline
|
7 |
+
from datetime import datetime, timedelta
|
8 |
+
|
9 |
+
# Sentiment Analyzer
|
10 |
+
sentiment_model = pipeline("sentiment-analysis", model="cardiffnlp/twitter-roberta-base-sentiment")
|
11 |
+
|
12 |
+
st.title("AI Market Analysis")
|
13 |
+
|
14 |
+
ticker = st.text_input("Enter Stock/Crypto Ticker", value="AAPL")
|
15 |
+
|
16 |
+
if st.button("Analyze"):
|
17 |
+
try:
|
18 |
+
# Get market data
|
19 |
+
data = yf.download(ticker, period="6mo")
|
20 |
+
data = data[['Close']].dropna()
|
21 |
+
data['Days'] = range(len(data))
|
22 |
+
|
23 |
+
# Model Prediksi Sederhana
|
24 |
+
model = LinearRegression()
|
25 |
+
model.fit(data[['Days']], data['Close'])
|
26 |
+
data['Predicted'] = model.predict(data[['Days']])
|
27 |
+
|
28 |
+
# Plot Harga
|
29 |
+
fig, ax = plt.subplots()
|
30 |
+
data['Close'].plot(ax=ax, label="Actual")
|
31 |
+
data['Predicted'].plot(ax=ax, label="Predicted")
|
32 |
+
ax.set_title(f"{ticker} Price Analysis")
|
33 |
+
ax.legend()
|
34 |
+
st.pyplot(fig)
|
35 |
+
|
36 |
+
# Dummy news (karena gak scrapping realtime news dulu)
|
37 |
+
st.subheader("News Sentiment Analysis (Sample Headlines)")
|
38 |
+
headlines = [
|
39 |
+
f"{ticker} stock rises after positive earnings report",
|
40 |
+
f"Market analysts are uncertain about {ticker} future",
|
41 |
+
f"{ticker} faces regulatory challenges in new markets"
|
42 |
+
]
|
43 |
+
|
44 |
+
for h in headlines:
|
45 |
+
result = sentiment_model(h)[0]
|
46 |
+
st.write(f"**{h}** → `{result['label']}` ({round(result['score'], 2)})")
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
st.error(f"Error: {e}")
|