Spaces:
Sleeping
Sleeping
import json | |
import panel as pn | |
from sentrifyai import api | |
import asyncio | |
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) | |
json_results = json.dumps(results, indent=4) | |
return json_results | |
except Exception as e: | |
return json.dumps({"error": str(e)}, indent=4) | |
async def process_inputs(message: str, panel_content): | |
try: | |
main.disabled = True | |
results = await classify_emotion(message) | |
# Display results | |
panel_content.append("##### π Emotion Classification Results:") | |
panel_content.append(results) | |
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: | |
panel_content.clear() | |
asyncio.create_task(process_inputs(message, panel_content)) | |
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) | |