SoulSync / emotion_selector.py
Priyacodes310's picture
Upload emotion_selector.py
692a2cd verified
import gradio as gr
class EmotionSelector(gr.Component):
def __init__(self, label="Select your emotion", **kwargs):
super().__init__(**kwargs)
self.label = label
self.emotions = [
("joy", "😊"),
("anger", "😠"),
("sadness", "😒"),
("fear", "😨"),
("surprise", "😲"),
("love", "❀️"),
("neutral", "😐"),
("disgust", "🀒")
]
def preprocess(self, input_data):
return input_data
def postprocess(self, output_data):
return output_data
def api_info(self):
"""Required method for Gradio components."""
return {
"type": "string",
"description": "Custom emotion selection component",
"enum": ["joy", "anger", "sadness", "fear", "surprise", "love", "neutral", "disgust"]
}
def example_payload(self):
"""Required method to prevent NotImplementedError."""
return ["neutral"] # Default example input
def get_block(self, root):
with gr.Row():
gr.Markdown(f"### {self.label}")
btns = []
for emotion, emoji in self.emotions:
btn = gr.Button(f"{emoji} {emotion.capitalize()}", variant="secondary", scale=1)
btns.append((btn, emotion))
selected_emotion = gr.State(value="neutral")
for btn, emotion in btns:
btn.click(lambda e=emotion: e, None, selected_emotion)
return selected_emotion
def _get_props(self):
return {"label": self.label}