Spaces:
Runtime error
Runtime error
File size: 1,902 Bytes
bc3b53f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 59 60 61 |
import sqlite3
import pandas as pd
import streamlit as st
from transformers import pipeline
from sklearn.metrics import accuracy_score
# Load the data into a pandas dataframe
df = pd.read_csv('https://raw.githubusercontent.com/SrinidhiRaghavan/AI-Sentiment-Analysis-on-IMDB-Dataset/master/test/imdb_te.csv', encoding= 'unicode_escape')
# Create a connection to the database
conn = sqlite3.connect('movie_reviews.db')
# Add a column for the sentiment labels
df['sentiment'] = ''
# Load the data into a table
df.to_sql('movie_reviews', conn, if_exists='replace', index=False)
# Load the pre-trained sentiment analysis model
classifier = pipeline('sentiment-analysis')
# Extract sentiment labels for the movie reviews
reviews = conn.execute('SELECT text FROM movie_reviews limit 10')
for i, row in enumerate(reviews):
review = row[0]
sentiment = classifier(review[:512])[0]['label']
if sentiment == 'POSITIVE':
label = 1
else:
label = 0
conn.execute('UPDATE movie_reviews SET sentiment = ? WHERE rowid = ?', (label, i+1))
conn.commit()
def main():
# Load the data from the SQLite database
X = pd.read_sql_query('SELECT text FROM movie_reviews limit 10', conn)
y = pd.read_sql_query('SELECT sentiment FROM movie_reviews limit 10', conn)
# Train a logistic regression model on the sentiment labels
clf = pipeline('sentiment-analysis')
y_pred = [int(result['label'] == 'POSITIVE') for result in clf(X['text'].to_list(), truncation=True)]
# Evaluate the model on the testing set
accuracy = accuracy_score(y['sentiment'].astype(int).to_list(), y_pred)
# Create a Streamlit app
st.title('Sentiment Analysis on Movie Reviews')
st.subheader('Accuracy')
st.write(f'{accuracy:.2f}')
st.subheader('Movie Reviews')
st.write(X)
st.subheader('Sentiment Labels')
st.write(y)
if __name__ == '__main__':
main()
|