JEPHONETORRE commited on
Commit
44f9ee7
·
1 Parent(s): f02da07
Files changed (2) hide show
  1. app.py +120 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
+ import torch
5
+ import matplotlib.pyplot as plt
6
+ from datasets import Dataset
7
+ import asyncio
8
+
9
+ # Handle asyncio loop issues
10
+ try:
11
+ asyncio.get_running_loop()
12
+ except RuntimeError: # No running event loop
13
+ asyncio.run(asyncio.sleep(0))
14
+
15
+ # Load pre-trained model and tokenizer
16
+ MODEL_PATH = "distilbert-base-uncased-finetuned-sst-2-english" # Default Hugging Face sentiment model
17
+ model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
18
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
19
+
20
+ # Define a sentiment analysis function
21
+ def sentiment_analysis(text):
22
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
23
+ outputs = model(**inputs)
24
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
25
+ sentiment = torch.argmax(probabilities, dim=1).item()
26
+ confidence = torch.max(probabilities, dim=1).values.item()
27
+ return ("POSITIVE" if sentiment == 1 else "NEGATIVE", confidence)
28
+
29
+ # Streamlit app
30
+ st.title("Twitter Sentiment Analysis App")
31
+ st.write("Analyze sentiments in Twitter-like text data using a pre-trained model.")
32
+
33
+ # Tabs for navigation
34
+ tab1, tab2 = st.tabs(["Analyze Sentiments", "Sample Dataset"])
35
+
36
+ with tab1:
37
+ st.header("Analyze Sentiments")
38
+ st.write("Upload a dataset to analyze sentiments of text data.")
39
+
40
+ # File uploader
41
+ data_file = st.file_uploader("Upload your CSV file", type=["csv"])
42
+
43
+ if data_file is not None:
44
+ # Read the dataset
45
+ data = pd.read_csv(data_file)
46
+
47
+ # Display the dataset
48
+ st.subheader("Dataset Preview")
49
+ st.write(data.head())
50
+
51
+ # Check for text column selection
52
+ text_column = st.selectbox("Select the column containing text for analysis:", data.columns)
53
+
54
+ if st.button("Analyze Sentiment"):
55
+ # Clean the text column: Remove NaN values and ensure text input is string type
56
+ data[text_column] = data[text_column].fillna("").astype(str)
57
+
58
+ # Perform sentiment analysis
59
+ st.write("Analyzing sentiments...")
60
+ results = data[text_column].apply(lambda x: sentiment_analysis(x))
61
+ data['Sentiment'] = results.apply(lambda x: x[0])
62
+ data['Confidence'] = results.apply(lambda x: x[1])
63
+
64
+ # Display results
65
+ st.subheader("Analysis Results")
66
+ st.write(data[[text_column, 'Sentiment', 'Confidence']])
67
+
68
+ # Plot sentiment distribution
69
+ st.subheader("Sentiment Distribution")
70
+ sentiment_counts = data['Sentiment'].value_counts()
71
+ fig, ax = plt.subplots()
72
+ sentiment_counts.plot(kind='bar', ax=ax, color=['green', 'blue', 'red'])
73
+ ax.set_title("Sentiment Distribution")
74
+ ax.set_xlabel("Sentiment")
75
+ ax.set_ylabel("Count")
76
+ st.pyplot(fig)
77
+
78
+ # Option to download results
79
+ st.subheader("Download Results")
80
+ csv = data.to_csv(index=False)
81
+ st.download_button(
82
+ label="Download Sentiment Analysis Results",
83
+ data=csv,
84
+ file_name="sentiment_analysis_results.csv",
85
+ mime="text/csv",
86
+ )
87
+ else:
88
+ st.write("Please upload a dataset to proceed.")
89
+
90
+ with tab2:
91
+ st.header("Sample Dataset")
92
+ st.write("Download a sample dataset to try out the app.")
93
+
94
+ # Provide a sample dataset for download
95
+ sample_data = pd.DataFrame({
96
+ "Tweet": [
97
+ "I love this product! It's amazing.",
98
+ "This is the worst service I have ever received.",
99
+ "I'm not sure how I feel about this.",
100
+ "Absolutely fantastic experience!",
101
+ "Terrible. Would not recommend."
102
+ ]
103
+ })
104
+ st.write(sample_data)
105
+
106
+ sample_csv = sample_data.to_csv(index=False)
107
+ st.download_button(
108
+ label="Download Sample Dataset",
109
+ data=sample_csv,
110
+ file_name="sample_twitter_dataset.csv",
111
+ mime="text/csv",
112
+ )
113
+
114
+ st.write("Follow these steps:")
115
+ st.markdown("""
116
+ 1. Go to the **Analyze Sentiments** tab.
117
+ 2. Upload the sample dataset or your own dataset in CSV format.
118
+ 3. Select the column containing the text to analyze.
119
+ 4. Click **Analyze Sentiment** to view results and download them.
120
+ """)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ streamlit
2
+ pandas
3
+ transformers
4
+ torch
5
+ matplotlib