rohitme890712 commited on
Commit
bacd746
·
verified ·
1 Parent(s): 5687191

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -12
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  from transformers import pipeline
3
 
4
  # Load the sentiment analysis pipeline
@@ -11,15 +12,64 @@ sentiment_pipeline = load_pipeline()
11
  # Streamlit app
12
  st.title("Sentiment Analysis Tool")
13
 
14
- # User input
15
- text = st.text_area("Enter text to analyze:", "This is a great movie!")
16
-
17
- # Analyze button
18
- if st.button("Analyze"):
19
- if text:
20
- with st.spinner("Analyzing..."):
21
- result = sentiment_pipeline(text)[0] # Get the first (and only) result
22
- st.write("Sentiment:", result['label'])
23
- st.write("Confidence:", result['score'])
24
- else:
25
- st.warning("Please enter some text to analyze.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
  from transformers import pipeline
4
 
5
  # Load the sentiment analysis pipeline
 
12
  # Streamlit app
13
  st.title("Sentiment Analysis Tool")
14
 
15
+ # Input option: Text area or CSV file
16
+ input_option = st.radio("Choose input method:", ("Text Area", "CSV File"))
17
+
18
+ if input_option == "Text Area":
19
+ # User input (Text Area)
20
+ text = st.text_area("Enter text to analyze:", "This is a great movie!")
21
+
22
+ # Analyze button
23
+ if st.button("Analyze Text"):
24
+ if text:
25
+ with st.spinner("Analyzing..."):
26
+ result = sentiment_pipeline(text)[0] # Get the first (and only) result
27
+ st.write("Sentiment:", result['label'])
28
+ st.write("Confidence:", result['score'])
29
+ else:
30
+ st.warning("Please enter some text to analyze.")
31
+
32
+ elif input_option == "CSV File":
33
+ # CSV File Upload
34
+ uploaded_file = st.file_uploader("Upload a CSV file", type="csv")
35
+
36
+ if uploaded_file is not None:
37
+ try:
38
+ df = pd.read_csv(uploaded_file)
39
+ st.write("Preview of the CSV file:")
40
+ st.dataframe(df.head()) # Display the first few rows
41
+
42
+ # Column selection
43
+ text_column = st.selectbox("Select the column containing the text:", df.columns)
44
+
45
+ # Analyze button
46
+ if st.button("Analyze CSV"):
47
+ if text_column:
48
+ with st.spinner("Analyzing..."):
49
+ # Perform sentiment analysis on each row
50
+ results = []
51
+ for index, row in df.iterrows():
52
+ text = row[text_column]
53
+ try:
54
+ result = sentiment_pipeline(text)[0]
55
+ results.append({"text": text, "sentiment": result['label'], "confidence": result['score']})
56
+ except Exception as e:
57
+ results.append({"text": text, "sentiment": "Error", "confidence": 0.0, "error": str(e)}) #append error to text column
58
+ results_df = pd.DataFrame(results)
59
+ st.write("Sentiment Analysis Results:")
60
+ st.dataframe(results_df)
61
+
62
+ # Download the results as CSV
63
+ csv = results_df.to_csv(index=False).encode('utf-8')
64
+ st.download_button(
65
+ label="Download results as CSV",
66
+ data=csv,
67
+ file_name="sentiment_analysis_results.csv",
68
+ mime='text/csv',
69
+ )
70
+
71
+ else:
72
+ st.warning("Please select the text column.")
73
+
74
+ except Exception as e:
75
+ st.error(f"Error reading the CSV file: {e}")