HamidBekam's picture
Update app.py
45178de
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()