Spaces:
Running
Running
import json | |
from flask import jsonify | |
# ๐น Generate Tags from Text | |
def generate_tags(content): | |
stop_words = {"the", "and", "is", "in", "to", "a", "of", "on", "for"} | |
words = content.lower().split() | |
tags = [word for word in words if word not in stop_words and len(word) > 3] | |
return list(set(tags)) | |
# ๐น Parse JSON Responses | |
def parse_json(response): | |
try: | |
return json.loads(response) | |
except json.JSONDecodeError: | |
return None | |
# ๐น Error Handlers | |
def error_response(message, status_code): | |
return jsonify({"error": message}), status_code | |
# Update emotion categorization mapping | |
EMOTION_CATEGORIES = { | |
"goal-oriented": ["desire", "anticipation", "optimism"], | |
"social": ["gratitude", "admiration", "love"], | |
"reflective": ["remorse", "sadness", "disappointment"], | |
"urgent": ["fear", "nervousness", "surprise"], | |
"critical": ["anger", "disgust", "annoyance"], | |
"joyful": ["joy", "excitement", "amusement"] | |
} | |
SENTIMENT_MAP = { | |
"LABEL_0": "negative", | |
"LABEL_1": "neutral", | |
"LABEL_2": "positive" | |
} | |
def categorize_memory(emotions, sentiment): | |
"""Improved categorization with fallback logic""" | |
if not emotions: | |
return f"uncategorized-{sentiment['label']}" | |
# Find direct matches | |
for emotion in emotions: | |
for category, keywords in EMOTION_CATEGORIES.items(): | |
if emotion['label'] in keywords: | |
return f"{category}-{sentiment['label']}" | |
# Fallback to sentiment-based category | |
return f"neutral-{sentiment['label']}" | |