Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 10 |
user_input = st.text_area("π Text Input", height=300)
|
| 11 |
|
| 12 |
-
#
|
| 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 |
-
#
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 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()
|