File size: 1,649 Bytes
457ceff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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}")