Spaces:
Runtime error
Runtime error
HamidBekam
commited on
Commit
·
bc3b53f
1
Parent(s):
97abea4
Update app.py
Browse files
app.py
CHANGED
@@ -1,58 +1,60 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
for
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
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()
|