asteios/pathos
0
1import gradio as gr2from transformers import pipeline3import numpy as np4import plotly.graph_objects as go5 6# Initialize the emotion classification model7classifier = pipeline(task="text-classification", model="SamLowe/roberta-base-go_emotions", top_k=None)8 9# Expanded PAD mapping for predefined emotions10pad_mapping = {11 'admiration': (0.5, 0.3, 0.6),12 'amusement': (0.8, 0.7, 0.4),13 'anger': (-0.8, 0.9, -0.4),14 'annoyance': (-0.6, 0.7, -0.3),15}16 17def classify_text(text):18 results = classifier(text)[0] # Assuming the first element is the list we want19 pad_values = np.array([0.0, 0.0, 0.0])20 total_weight = 021 22 # Since results are a list of dictionaries23 for result in results:24 emotion = result.get('label')25 score = result.get('score')26 if emotion in pad_mapping:27 emotion_pad = np.array(pad_mapping[emotion])28 weighted_pad = emotion_pad * score29 pad_values += weighted_pad30 total_weight += score31 32 if total_weight > 0:33 pad_values /= total_weight34 35 fig = go.Figure()36 37 # Add predefined emotion points38 for emotion, coords in pad_mapping.items():39 fig.add_trace(go.Scatter3d(40 x=[coords[0]], y=[coords[1]], z=[coords[2]],41 mode='markers',42 marker=dict(size=6),43 name=emotion44 ))45 46 # Add classified text point47 fig.add_trace(go.Scatter3d(48 x=[pad_values[0]], y=[pad_values[1]], z=[pad_values[2]],49 mode='markers',50 marker=dict(symbol='x', size=8, color='red'),51 name="Classified Text"52 ))53 54 fig.update_layout(55 title='Interactive PAD Space Representation',56 scene=dict(57 xaxis_title='Pleasure',58 yaxis_title='Arousal',59 zaxis_title='Dominance'60 )61 )62 63 return fig.to_html()64 65interface = gr.Interface(fn=classify_text, inputs="text", outputs="html")66interface.launch()67 