File size: 2,242 Bytes
4ab9396
3ee3a5a
39a1750
4ab9396
33a51b3
3ee3a5a
39a1750
33a51b3
3ee3a5a
4ab9396
33a51b3
3ee3a5a
33a51b3
6f04bba
 
3ee3a5a
33a51b3
4ab9396
 
 
33a51b3
 
6e93ef3
33a51b3
 
 
 
 
 
 
 
 
3ee3a5a
33a51b3
 
 
 
 
 
 
 
 
 
 
4ab9396
 
 
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
import streamlit as st
from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer

def main():
    # Streamlit app interface design with updated title and emoji usage
    st.title("πŸ“ˆ Financial Text Analysis")

    # User input area with updated prompt and emoji
    user_input = st.text_area("πŸ“ Text Input", height=300)

    # Initialize sentiment analysis and summarization pipelines only once using st.session_state
    if 'sentiment_analyzer' not in st.session_state:
        # Load your custom sentiment analysis model with updated paths
        model = AutoModelForSequenceClassification.from_pretrained("WRX020510/CustomModel_twitter", num_labels=3)
        tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
        st.session_state.sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
    
    if 'summarizer' not in st.session_state:
        st.session_state.summarizer = pipeline("summarization", model="t5-small")

    # Sentiment Analysis Section
    with st.container():
        st.write("πŸ” Sentiment Analysis")
        if st.button("Analyze Sentiment"):
            if user_input:
                with st.spinner('Analyzing sentiment...'):
                    sentiment_result = st.session_state.sentiment_analyzer(user_input)
                    sentiment = sentiment_result[0]['label']
                    confidence = sentiment_result[0]['score']
                    st.write(f"Sentiment: {sentiment} with Confidence: {confidence:.2f}")
            else:
                st.error("Please enter some text to analyze sentiment.")

    # Summary Section with updated header
    with st.container():
        st.write("πŸ“œ Summary")
        if st.button("Generate Summary"):
            if user_input:
                with st.spinner('Generating summary...'):
                    summary_result = st.session_state.summarizer(user_input, max_length=130, min_length=30, do_sample=False)
                    summary = summary_result[0]['summary_text']
                    st.write(summary)
            else:
                st.error("Please enter some text to generate a summary.")

if __name__ == "__main__":
    main()