ai-kit / slack_sentiment_analysis /ui_sentiment_analysis.py
Kim Adams
all of the things
2cd1c76
import gradio as gr
import pandas as pd
import os
from utilities import constants
from slack_sentiment_analysis import sentiment_analysis
sentimentData=None
def Summary(code):
global sentimentData
sum_messages,sentimentData, itemCount = sentiment_analysis.AnalyzeSentiment(code)
print("itemCount:"+str(itemCount) + " sentimentData:"+str(sentimentData))
sentimentPlot.y_lim=[0, itemCount]
return sentimentData,sum_messages,sentimentData
def InitDF():
global commDF
commDF=pd.DataFrame({"role": [""], "content": [""] })
def UpdateWithExample(newExample):
fileName=newExample.replace(" ","_").lower()
if os.path.exists(f"{constants.JSON_PREFIX}{fileName}.json"):
with open(f"{constants.JSON_PREFIX}{fileName}.json", 'r') as file:
return file.read()
with gr.Blocks() as ui:
label = gr.Label(show_label=False, value=constants.SLACK_SENTIMENT_ANALYSIS, container=False)
with gr.Row():
with gr.Column():
gr.Markdown(constants.PURPOSE_MD)
gr.Markdown(constants.SLACK_SENTIMENT_ANALYSIS_PURPOSE)
with gr.Column():
gr.Markdown(constants.DIRECTIONS_MD)
gr.Markdown(value=constants.SLACK_SENTIMENT_ANALYSIS_DIRECTIONS)
code=gr.Textbox(label=constants.INITIAL_SLACK, interactive=True, placeholder=constants.SUMMARY_MD_DIRECTIONS, lines=3)
gr.Markdown(constants.SLACK_EXAMPLES_MD)
with gr.Row():
with gr.Column():
examplesPos = gr.Radio([constants.JSON_1, constants.JSON_2, constants.JSON_3, constants.JSON_4],
show_label=False, info=constants.POSITIVE_TREND)
with gr.Column():
examplesNeg = gr.Radio([constants.JSON_5, constants.JSON_6, constants.JSON_7, constants.JSON_8],
show_label=False, info=constants.NEGATIVE_TREND)
sentimentize=gr.Button(value=constants.ANALYZE_SENTIMENT, variant="primary")
with gr.Column():
gr.Markdown(constants.SENTIMENT_DATA_MD)
sentimentPlot = gr.ScatterPlot(value=sentimentData, y="id", y_title=constants.PEOPLE,
x="sentiment_score", x_title=constants.SENTIMENT,
y_lim=[0,10], x_lim=[-0.1, 1.1], show_label=False,
color_legend_title=constants.SENTIMENT, color="sentiment",
size="size", size_legend_position="none", height = 500, width=500,
tooltip=["user","date","message","sentiment"])
with gr.Column():
gr.Markdown(constants.SENTIMENT_MD)
sentiment=gr.DataFrame(pd.DataFrame({"user": [""], "sentiment_score": [""], "sentiment": [""] }),
wrap=True, show_label=False, height="500")
with gr.Row():
commDF=gr.DataFrame(type="pandas", value=pd.DataFrame({"role": [""], "content": [""] }),
wrap=True, show_label=False, label=constants.OPENAI_LOG)
sentimentize.click(Summary, inputs=[code], outputs=[sentiment,commDF,sentimentPlot])
examplesPos.input(UpdateWithExample,inputs=[examplesPos], outputs=[code])
examplesNeg.input(UpdateWithExample,inputs=[examplesNeg], outputs=[code])
InitDF()