Spaces:
Sleeping
Sleeping
from flask import Flask, request, jsonify | |
from transformers import pipeline | |
app = Flask(__name__) | |
# Initialize the sentiment analysis pipeline | |
sentiment_classifier = pipeline("sentiment-analysis") | |
def analyze_priority(text): | |
# Get sentiment analysis | |
sentiment_result = sentiment_classifier(text)[0] | |
sentiment_score = sentiment_result['score'] | |
sentiment_label = sentiment_result['label'] | |
# Convert text to lowercase for keyword checking | |
text = text.lower() | |
# Define urgency indicators | |
urgent_indicators = ['urgent', 'emergency', 'asap', 'immediately', 'critical'] | |
high_indicators = ['important', 'priority', 'soon', 'significant'] | |
# Check for urgent keywords | |
has_urgent = any(word in text for word in urgent_indicators) | |
has_high = any(word in text for word in high_indicators) | |
# Determine priority based on both sentiment and keywords | |
if has_urgent or (sentiment_label == 'NEGATIVE' and sentiment_score > 0.8): | |
return "urgent" | |
elif has_high or (sentiment_label == 'NEGATIVE' and sentiment_score > 0.6): | |
return "high" | |
elif sentiment_label == 'NEGATIVE': | |
return "normal" | |
else: | |
return "low" | |
def get_priority(): | |
text = request.args.get('text', '') | |
if not text: | |
return jsonify({ | |
'error': 'No text provided', | |
'status': 400 | |
}), 400 | |
try: | |
priority = analyze_priority(text) | |
sentiment_result = sentiment_classifier(text)[0] | |
return jsonify({ | |
'text': text, | |
'priority': priority, | |
'status': 200, | |
'details': { | |
'sentiment': sentiment_result | |
} | |
}) | |
except Exception as e: | |
return jsonify({ | |
'error': f'Analysis failed: {str(e)}', | |
'status': 500 | |
}), 500 | |
if __name__ == '__main__': | |
app.run(debug=False, host="0.0.0.0", port=7860) # Required for Hugging Face |