Sentiment2D / app.py
rfdougherty's picture
Improve layout
57a095c
raw
history blame
1.38 kB
import gradio as gr
import pandas as pd
from sentiment2d import Sentiment2D
s2d = Sentiment2D()
history = []
FONT_COLOR = "#666666"
FILL_COLOR = "rgb(.7,.7,.7)"
TITLE = "COMPASS Pathways 2D Sentiment Model"
EXAMPLES = [
"This is so awesome!",
"You're driving me up the wall!",
"I'm so lonely I could cry.",
"I'm not feeling very sad at all.",
"I'm just chillin' this morning.",
]
def sentiment(text, clear_history):
global history
valence, arousal = s2d(text)
# res = f"{text}: valence={valence:0.3f}, arousal={arousal:0.3f}"
res = dict(text=text, valence=valence, arousal=arousal, words=len(text.split()))
if clear_history:
history = []
history.append(res)
df = pd.DataFrame(history)
res_txt = [
f"{r['text']}: valence={r['valence']:0.3f}, arousal={r['arousal']:0.3f}"
for r in history
]
return "\n".join(res_txt), df
iface = gr.Interface(
fn=sentiment,
inputs=[gr.Textbox(lines=1, placeholder="Text for 2d sentiment..."), "checkbox"],
outputs=[
gr.TextBox(lines=5, max_lines=5, label="Results"),
gr.ScatterPlot(
x="valence",
y="arousal",
tooltip="text",
size="words",
x_lim=[-1.05, 1.05],
y_lim=[-1.05, 1.05],
),
],
titel=TITLE,
examples=EXAMPLES,
)
iface.launch()