Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Load a lightweight sentiment model (CPU-friendly) | |
| classifier = pipeline("sentiment-analysis") | |
| # Map model labels to emoji sets | |
| EMOJI_MAP = { | |
| "POSITIVE": ["π", "π", "π", "π₯", "π"], | |
| "NEGATIVE": ["π", "π", "π‘", "π", "π"], | |
| "NEUTRAL": ["π", "π€", "π", "π¬", "π"], | |
| } | |
| def analyze_mood(text: str) -> str: | |
| if not text or not text.strip(): | |
| return "β Please enter some text." | |
| result = classifier(text)[0] | |
| label = result["label"].upper() | |
| score = float(result["score"]) | |
| # Low confidence β treat as neutral to avoid overclaiming | |
| if score < 0.60: | |
| label = "NEUTRAL" | |
| emojis = " ".join(EMOJI_MAP.get(label, ["π€·ββοΈ"])) | |
| return f"{emojis} (Confidence: {score:.2f})" | |
| with gr.Blocks(title="Emoji Mood Classifier") as demo: | |
| gr.Markdown( | |
| "# Emoji Mood Classifier\n" | |
| "A fun twist on sentiment analysis β returns emojis matching your text's mood." | |
| ) | |
| inp = gr.Textbox(label="Enter text", placeholder="Type a sentence here...", lines=3) | |
| out = gr.Textbox(label="Emoji Mood", lines=2) | |
| btn = gr.Button("Analyze") | |
| btn.click(analyze_mood, inputs=inp, outputs=out) | |
| # Expose a top-level `demo` for Spaces | |
| if __name__ == "__main__": | |
| demo.launch() | |