WRX020510 commited on
Commit
33a51b3
Β·
verified Β·
1 Parent(s): 6f04bba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -30
app.py CHANGED
@@ -2,48 +2,46 @@ import streamlit as st
2
  from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
 
4
  def main():
5
- # Streamlit app interface design
6
  st.title("πŸ“ˆ Financial Text Analysis")
7
- st.write("Enter the financial text below to analyze its sentiment and generate a summary:")
8
 
9
- # User input area for text
10
  user_input = st.text_area("πŸ“ Text Input", height=300)
11
 
12
- # Load sentiment analysis pipeline only once using st.session_state
13
  if 'sentiment_analyzer' not in st.session_state:
14
- # Load your custom sentiment analysis model
15
  model = AutoModelForSequenceClassification.from_pretrained("WRX020510/CustomModel_twitter", num_labels=3)
16
  tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
17
  st.session_state.sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
18
-
19
- # Load summarization pipeline only once using st.session_state
20
  if 'summarizer' not in st.session_state:
21
- # Load the T5 model for summarization
22
  st.session_state.summarizer = pipeline("summarization", model="t5-small")
23
 
24
- # Button to analyze sentiment
25
- if st.button("Analyze Sentiment"):
26
- if user_input:
27
- with st.spinner('Analyzing sentiment...'):
28
- # Performing sentiment analysis
29
- sentiment_result = st.session_state.sentiment_analyzer(user_input)
30
- sentiment = sentiment_result[0]['label']
31
- confidence = sentiment_result[0]['score']
32
- st.write(f"Sentiment: {sentiment} with Confidence: {confidence:.2f}")
33
- else:
34
- st.error("Please enter some text to analyze sentiment.")
 
35
 
36
- # Button to generate summary
37
- if st.button("Generate Summary"):
38
- if user_input:
39
- with st.spinner('Generating summary...'):
40
- # Generating the summary from the input text
41
- summary_result = st.session_state.summarizer(user_input, max_length=130, min_length=30, do_sample=False)
42
- summary = summary_result[0]['summary_text']
43
- st.write("πŸ“œ Summary:")
44
- st.write(summary)
45
- else:
46
- st.error("Please enter some text to generate a summary.")
47
 
48
  if __name__ == "__main__":
49
  main()
 
2
  from transformers import pipeline, AutoModelForSequenceClassification, AutoTokenizer
3
 
4
  def main():
5
+ # Streamlit app interface design with updated title and emoji usage
6
  st.title("πŸ“ˆ Financial Text Analysis")
 
7
 
8
+ # User input area with updated prompt and emoji
9
  user_input = st.text_area("πŸ“ Text Input", height=300)
10
 
11
+ # Initialize sentiment analysis and summarization pipelines only once using st.session_state
12
  if 'sentiment_analyzer' not in st.session_state:
13
+ # Load your custom sentiment analysis model with updated paths
14
  model = AutoModelForSequenceClassification.from_pretrained("WRX020510/CustomModel_twitter", num_labels=3)
15
  tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
16
  st.session_state.sentiment_analyzer = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
17
+
 
18
  if 'summarizer' not in st.session_state:
 
19
  st.session_state.summarizer = pipeline("summarization", model="t5-small")
20
 
21
+ # Sentiment Analysis Section
22
+ with st.container():
23
+ st.write("## Sentiment Analysis")
24
+ if st.button("Analyze Sentiment"):
25
+ if user_input:
26
+ with st.spinner('Analyzing sentiment...'):
27
+ sentiment_result = st.session_state.sentiment_analyzer(user_input)
28
+ sentiment = sentiment_result[0]['label']
29
+ confidence = sentiment_result[0]['score']
30
+ st.write(f"Sentiment: {sentiment} with Confidence: {confidence:.2f}")
31
+ else:
32
+ st.error("Please enter some text to analyze sentiment.")
33
 
34
+ # Summary Section with updated header
35
+ with st.container():
36
+ st.write("πŸ“œ Summary")
37
+ if st.button("Generate Summary"):
38
+ if user_input:
39
+ with st.spinner('Generating summary...'):
40
+ summary_result = st.session_state.summarizer(user_input, max_length=130, min_length=30, do_sample=False)
41
+ summary = summary_result[0]['summary_text']
42
+ st.write(summary)
43
+ else:
44
+ st.error("Please enter some text to generate a summary.")
45
 
46
  if __name__ == "__main__":
47
  main()