Yoshi Malaise
2025-10-29
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 640
#| components: [editor, viewer]
from shiny import reactive
from shiny.express import input, render, ui
analysis_result = reactive.value("Run the analysis to see the results")
ui.page_opts(fillable=True)
ui.tags.style(
"""
.card {
max-width: 400px; width: 100%;
margin-left: auto; margin-right: auto;
height: 45%;
}
"""
)
with ui.card():
ui.card_header("Sentiment Analysis")
ui.input_text_area("message", "What's the message you would like to run the sentiment analysis on?", value="")
@reactive.effect
@reactive.event(input.analyse)
async def perform_the_analysis():
import micropip
await micropip.install("transformers_js_py")
from transformers_js_py import import_transformers_js
transformers = await import_transformers_js()
pipeline = transformers.pipeline
pipe = await pipeline('sentiment-analysis')
out = await pipe(input.message())
res = "We are " + str(round(out[0]['score'] * 100, 2)) + "% confident that the sentiment is " + out[0]['label']
analysis_result.set(res)
ui.input_action_button("analyse", "Run the analysis")
with ui.card():
ui.card_header("Result")
@render.text
def value():
return str(analysis_result.get())