File size: 2,981 Bytes
4f15ff1
 
 
 
c973b79
4f15ff1
 
 
 
 
 
 
 
c973b79
 
 
 
 
 
 
 
 
 
 
 
4f15ff1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

#utils.py
import openai
import os
#import easyocr
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import matplotlib.pyplot as plt
from nltk.sentiment.vader import SentimentIntensityAnalyzer
import nltk
nltk.download('vader_lexicon')

#def ocr_reader(lan, png):
#    locs = []; words = []; confidence = []
#    try:
#        reader = easyocr.Reader([str(lan)]) #Initialise Language
#        result = reader.readtext(str(png))
#        for i in result:
#            locs.append(i[0])
#            words.append(i[1])
#            confidence.append(i[2])
#        return locs, words, confidence
#    except Exception as e:
#        print(e)
        
def get_completion(prompt, model="gpt-3.5-turbo"):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=0, # this is the degree of randomness of the model's output
    )
    content = response.choices[0].message["content"]
    token_dict = {
        'prompt_tokens':response['usage']['prompt_tokens'],
        'completion_tokens':response['usage']['completion_tokens'],
        'total_tokens':response['usage']['total_tokens'],
            }
    moderation_output = openai.Moderation.create(input=prompt)["results"][0]
    return content, token_dict, moderation_output

def get_radar(df):
    df['names'] = df.index
    fig = go.Figure()
    fig.add_trace(go.Scatterpolar(
                  r=df.category_scores,
                  theta=df.names,
                  fill='toself',
                  name='Moderation'))
    fig.update_layout(
        polar=dict(radialaxis=dict(
                      visible=True,
                      range=[0, 1])),
              showlegend=False)

    return fig

def plot_wordcloud(wc):
    fig, ax = plt.subplots(figsize = (12, 8))
    ax.imshow(wc, interpolation="bilinear")
    plt.axis("off")
    return fig

def get_sentiment(text):
    sentences = [' '.join(sent.split()).strip() for sent in text.split('.')]
    df = pd.DataFrame(sentences, columns=['content'])
    sid = SentimentIntensityAnalyzer()
    df['sentiment'] = df['content'].apply(lambda x: sid.polarity_scores(x))  
    df = pd.concat([df.drop(['sentiment'], axis=1), df['sentiment'].apply(pd.Series)], axis=1)
    df = df.rename(columns={'neu': 'neutral', 'neg': 'negative', 'pos': 'positive'})
    df['confidence'] = df[["negative", "neutral", "positive"]].max(axis=1)
    df['sentiment'] = df[["negative", "neutral", "positive"]].idxmax(axis=1)
    grouped = pd.DataFrame(df['sentiment'].value_counts()).reset_index()
    grouped.columns = ['sentiment', 'count']
    
    fig = px.scatter(df, y='sentiment', color='sentiment', size='confidence', 
                     hover_data=['content'], 
                     color_discrete_map={"negative": "firebrick", "neutral": "navajowhite", "positive": "darkgreen"})
    fig.update_layout(width=800,height=300,)
    return df, fig