Spaces:
Sleeping
Sleeping
| 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) | |