import gradio as gr import pandas as pd import matplotlib.pyplot as plt import numpy as np from sklearn.decomposition import LatentDirichletAllocation from sklearn.feature_extraction.text import CountVectorizer def concat_comments(sup_comment: list[str], comment: list[str]) -> list[str]: format_s = "{s}\n{c}" return [ format_s.format(s=s, c=c) for s, c in zip(sup_comment, comment) ] def main(button, chose_context): df = pd.read_csv('./data/results.csv', index_col=0) print(chose_context) data = concat_comments(df.sup_comment, df.comment) subreddits = df.subreddit.value_counts().index[:22] weight_counts = { t: [ df[df.Topic_key_word == t].subreddit.value_counts()[subreddit] / df.subreddit.value_counts()[subreddit] for subreddit in subreddits ] for t in topics } irony_percs = { t: [ len( df[df.subreddit == subreddit][(df[df.subreddit == subreddit].Topic_key_word == t) & (df[df.subreddit == subreddit].label == 1)] ) / len( df[df.subreddit == subreddit] ) for subreddit in subreddits ] for t in topics } width = 0.9 fig, ax = plt.subplots(figsize = (10, 7)) plt.axhline(0.5, color = 'red', ls=":", alpha = .3) bottom = np.zeros(len(subreddits)) for k, v in weight_counts.items(): p = ax.bar(subreddits, v, width, label=k, bottom=bottom) ax.bar(subreddits, irony_percs[k], width - 0.01, bottom=bottom, color = 'black', edgecolor = 'white', alpha = .2, hatch = '\\') bottom += v ax.set_title("Perc of topics for each subreddit") ax.legend(loc="upper right") plt.xticks(rotation=70) plt.show() with gr.Blocks() as demo: button = gr.Radio( label="Plot type", choices=['scatter_plot', 'heatmap', 'us_map', 'interactive_barplot', "radial", "multiline"], value='scatter_plot' ) chose_context = gr.Radio( label="Context LDA", choices=['comment', 'sup comment', 'sup comment + comment'], value='scatter_plot' ) plot = gr.Plot(label="Plot") button.change(main, inputs=[button, chose_context], outputs=[plot]) demo.load(main, inputs=[button], outputs=[plot]) # iface = gr.Interface(fn=greet, inputs="text", outputs="text") if __name__ == "__main__": demo.launch()