politweet / app.py
Nisse00's picture
fix
fae15d7
import gradio as gr
from textclassifier import TextClassifier as tc
import pandas as pd
from functions import functions as f
import time
USER_LIST = ['jimmieakesson', 'BuschEbba', 'annieloof', 'JohanPehrson', 'bolund', 'martastenevi', 'SwedishPM',
'dadgostarnooshi']
UserNameDict = dict(zip(['Jimmie Åkesson', 'Ebba Busch', 'Annie Lööf', 'Johan Pehrson', 'Per Bolund',
'Märta Stenevi', 'Magdalena Andersson', 'Nooshi Dadgostar'], USER_LIST))
Columns = ['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic', 'sub_topic', 'sentiment', 'target', 'tweet',
'date', 'urls', 'id', 'class_tuple', 'user_id']
def show_all_stats(ListChoices, SeeFullStats):
dataframe = pd.read_csv("{}/data/twitterdata.csv".format(tc.ROOT_PATH))
if SeeFullStats:
return dataframe[ListChoices]
else:
return pd.DataFrame()
def fixChoicesCorrectOrder(Choices):
ListChoices = [x for x in Columns if x in Choices]
return ListChoices
def MatchNameToUser(Name):
return UserNameDict[Name]
def main(From,
To,
Username,
UserNameChoices,
Nbr_Of_Tweets_To_Classify,
ListChoices,
SeeFullStats
):
def WhoToScrape():
if Username == "":
return MatchNameToUser(UserNameChoices[0])
else:
return Username
text_classifier = tc.TextClassifier(
from_date=From,
to_date=To,
user_list=WhoToScrape(),
num_tweets=int(Nbr_Of_Tweets_To_Classify))
text_classifier.run_main_pipeline()
dataframe = text_classifier.get_dataframe()
dataframe = dataframe[
['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic', 'sub_topic', 'sentiment', 'target', 'tweet',
'date', 'urls', 'id', 'class_tuple', 'user_id']]
return dataframe[fixChoicesCorrectOrder(ListChoices)], show_all_stats(fixChoicesCorrectOrder(ListChoices), SeeFullStats)
if __name__ == "__main__":
from datetime import date
demo = gr.Interface(
article="</a><br>From = The date from which you want to start the analysis.</a><br> To = The date to which "
"you want to end "
"the analysis.</a><br> Username = The username of the user you want to analyze.</a><br> How many "
"tweets to classify = "
"The number of tweets you want to analyze.</a><br>",
analytics_enabled=False,
theme="default",
title="Twitter data analysis",
fn=main,
inputs=[gr.components.Textbox(label="From", value='2022-01-01'),
gr.components.Textbox(label="To", value='2022-01-25'),
gr.components.Textbox(label="Username", value="BuschEbba"),
gr.components.Checkboxgroup(
choices=['Jimmie Åkesson', 'Ebba Busch', 'Annie Lööf', 'Johan Pehrson', 'Per Bolund',
'Märta Stenevi',
'Magdalena Andersson', 'Nooshi Dadgostar'], label=""),
gr.components.Textbox(label="How many Tweets to Classify", value="20"),
gr.components.Checkboxgroup(label="Options",
choices=['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic',
'sub_topic', 'sentiment', 'target', 'tweet', 'date', 'urls', 'id',
'class_tuple', 'user_id'],
value=['username', 'nlikes', 'nreplies', 'nretweets', 'main_topic',
'sub_topic', 'sentiment', 'target', 'tweet', 'date']
),
gr.components.Checkbox(label="Show full statistics")
],
outputs=[
gr.components.DataFrame(label="Summary statistics of the intervall you selected", max_rows=None),
gr.components.DataFrame(label="Summary statistics of the total database", max_rows=None, )])
demo.launch(share=False)