Spaces:
Runtime error
Runtime error
Commit
·
10c1d41
1
Parent(s):
ed05415
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|