HamidBekam commited on
Commit
bc3b53f
1 Parent(s): 97abea4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -58
app.py CHANGED
@@ -1,58 +1,60 @@
1
- import sqlite3
2
- import pandas as pd
3
- import streamlit as st
4
- from transformers import pipeline
5
- from sklearn.metrics import accuracy_score
6
-
7
- # Load the data into a pandas dataframe
8
- df = pd.read_csv('https://raw.githubusercontent.com/SrinidhiRaghavan/AI-Sentiment-Analysis-on-IMDB-Dataset/master/test/imdb_te.csv', encoding= 'unicode_escape')
9
-
10
- # Create a connection to the database
11
- conn = sqlite3.connect('movie_reviews.db')
12
-
13
- # Add a column for the sentiment labels
14
- df['sentiment'] = ''
15
-
16
- # Load the data into a table
17
- df.to_sql('movie_reviews', conn, if_exists='replace', index=False)
18
-
19
- # Load the pre-trained sentiment analysis model
20
- classifier = pipeline('sentiment-analysis')
21
-
22
- # Extract sentiment labels for the movie reviews
23
- reviews = conn.execute('SELECT text FROM movie_reviews limit 10')
24
- for i, row in enumerate(reviews):
25
- review = row[0]
26
- sentiment = classifier(review[:512])[0]['label']
27
- if sentiment == 'POSITIVE':
28
- label = 1
29
- else:
30
- label = 0
31
- conn.execute('UPDATE movie_reviews SET sentiment = ? WHERE rowid = ?', (label, i+1))
32
- conn.commit()
33
-
34
- def main():
35
- # Load the data from the SQLite database
36
- X = pd.read_sql_query('SELECT text FROM movie_reviews limit 10', conn)
37
- y = pd.read_sql_query('SELECT sentiment FROM movie_reviews limit 10', conn)
38
-
39
- # Train a logistic regression model on the sentiment labels
40
- clf = pipeline('sentiment-analysis')
41
- y_pred = [int(result['label'] == 'POSITIVE') for result in clf(X['text'].to_list(), truncation=True)]
42
-
43
- # Evaluate the model on the testing set
44
- accuracy = accuracy_score(y['sentiment'].astype(int).to_list(), y_pred)
45
-
46
- # Create a Streamlit app
47
- st.title('Sentiment Analysis on Movie Reviews')
48
- st.subheader('Accuracy')
49
- st.write(f'{accuracy:.2f}')
50
-
51
- st.subheader('Movie Reviews')
52
- st.write(X)
53
-
54
- st.subheader('Sentiment Labels')
55
- st.write(y)
56
-
57
- if __name__ == '__main__':
58
- main()
 
 
 
1
+
2
+
3
+ import sqlite3
4
+ import pandas as pd
5
+ import streamlit as st
6
+ from transformers import pipeline
7
+ from sklearn.metrics import accuracy_score
8
+
9
+ # Load the data into a pandas dataframe
10
+ df = pd.read_csv('https://raw.githubusercontent.com/SrinidhiRaghavan/AI-Sentiment-Analysis-on-IMDB-Dataset/master/test/imdb_te.csv', encoding= 'unicode_escape')
11
+
12
+ # Create a connection to the database
13
+ conn = sqlite3.connect('movie_reviews.db')
14
+
15
+ # Add a column for the sentiment labels
16
+ df['sentiment'] = ''
17
+
18
+ # Load the data into a table
19
+ df.to_sql('movie_reviews', conn, if_exists='replace', index=False)
20
+
21
+ # Load the pre-trained sentiment analysis model
22
+ classifier = pipeline('sentiment-analysis')
23
+
24
+ # Extract sentiment labels for the movie reviews
25
+ reviews = conn.execute('SELECT text FROM movie_reviews limit 10')
26
+ for i, row in enumerate(reviews):
27
+ review = row[0]
28
+ sentiment = classifier(review[:512])[0]['label']
29
+ if sentiment == 'POSITIVE':
30
+ label = 1
31
+ else:
32
+ label = 0
33
+ conn.execute('UPDATE movie_reviews SET sentiment = ? WHERE rowid = ?', (label, i+1))
34
+ conn.commit()
35
+
36
+ def main():
37
+ # Load the data from the SQLite database
38
+ X = pd.read_sql_query('SELECT text FROM movie_reviews limit 10', conn)
39
+ y = pd.read_sql_query('SELECT sentiment FROM movie_reviews limit 10', conn)
40
+
41
+ # Train a logistic regression model on the sentiment labels
42
+ clf = pipeline('sentiment-analysis')
43
+ y_pred = [int(result['label'] == 'POSITIVE') for result in clf(X['text'].to_list(), truncation=True)]
44
+
45
+ # Evaluate the model on the testing set
46
+ accuracy = accuracy_score(y['sentiment'].astype(int).to_list(), y_pred)
47
+
48
+ # Create a Streamlit app
49
+ st.title('Sentiment Analysis on Movie Reviews')
50
+ st.subheader('Accuracy')
51
+ st.write(f'{accuracy:.2f}')
52
+
53
+ st.subheader('Movie Reviews')
54
+ st.write(X)
55
+
56
+ st.subheader('Sentiment Labels')
57
+ st.write(y)
58
+
59
+ if __name__ == '__main__':
60
+ main()