File size: 2,392 Bytes
1b3030e
111bafe
1b3030e
111bafe
1b3030e
111bafe
 
 
 
 
 
 
 
 
1b3030e
 
 
 
 
 
 
111bafe
1b3030e
111bafe
 
1b3030e
 
 
 
 
 
 
 
 
 
 
 
111bafe
 
 
 
 
 
1b3030e
 
 
 
111bafe
 
1b3030e
 
 
 
 
 
 
 
 
 
111bafe
1b3030e
 
 
 
 
111bafe
 
 
 
 
 
 
 
 
 
 
1b3030e
 
111bafe
 
 
1b3030e
 
111bafe
 
 
1b3030e
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
85
86
87
88
import json
import panel as pn
from sentrifyai import api

pn.extension(sizing_mode="stretch_width")

ICON_URLS = {
    "brand-github": "https://github.com/holoviz/panel",
    "brand-twitter": "https://twitter.com/Panel_Org",
    "brand-linkedin": "https://www.linkedin.com/company/panel-org",
    "message-circle": "https://discourse.holoviz.org/",
    "brand-discord": "https://discord.gg/AXRHnJU6sP",
}

async def classify_emotion(message: str):
    emotions = api.Emotions()
    try:
        results = emotions.emotion(model_slug='Emotion-1.0', message=message)
        return results
    except Exception as e:
        return {"error": str(e)}

def process_inputs(message: str):
    try:
        main.disabled = True
        
        # Perform emotion classification
        yield "##### βš™οΈ Classifying emotions..."
        results = yield from classify_emotion(message)
        
        # Display results
        yield "##### πŸŽ‰ Emotion Classification Results:"
        if "error" in results:
            yield f"Error: {results['error']}"
        else:
            for emotion, score in results.items():
                yield f"{emotion}: {score:.2f}"
    
    finally:
        main.disabled = False


# create widgets
message_input = pn.widgets.TextInput(
    name="Enter a message for emotion classification",
    placeholder="Type your message here...",
    sizing_mode="stretch_width"
)

classify_button = pn.widgets.Button(name="Classify Emotion", button_type="primary")

# define callback function for button click
def on_button_click(event):
    message = message_input.value
    if message:
        generator = process_inputs(message)
        panel_content[:] = generator

classify_button.on_click(on_button_click)

# create main panel content
panel_content = pn.Column(
    "### 😊 Emotion Classification",
    message_input,
    classify_button,
)

# add footer
footer_row = pn.Row(pn.Spacer(), align="center")
for icon, url in ICON_URLS.items():
    href_button = pn.widgets.Button(icon=icon, width=35, height=35)
    href_button.js_on_click(code=f"window.open('{url}')")
    footer_row.append(href_button)
footer_row.append(pn.Spacer())

# create dashboard
main = pn.Column(
    panel_content,
    footer_row,
)

title = "Emotion Classification"
pn.template.MaterialTemplate(
    title=title,
    main=main,
    header_background="#F08080",
).servable(title=title)