Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
# Initialize the sentiment analysis pipeline
|
| 7 |
+
sentiment_classifier = pipeline("sentiment-analysis")
|
| 8 |
+
|
| 9 |
+
def analyze_priority(text):
|
| 10 |
+
# Get sentiment analysis
|
| 11 |
+
sentiment_result = sentiment_classifier(text)[0]
|
| 12 |
+
sentiment_score = sentiment_result['score']
|
| 13 |
+
sentiment_label = sentiment_result['label']
|
| 14 |
+
|
| 15 |
+
# Convert text to lowercase for keyword checking
|
| 16 |
+
text = text.lower()
|
| 17 |
+
|
| 18 |
+
# Define urgency indicators
|
| 19 |
+
urgent_indicators = ['urgent', 'emergency', 'asap', 'immediately', 'critical']
|
| 20 |
+
high_indicators = ['important', 'priority', 'soon', 'significant']
|
| 21 |
+
|
| 22 |
+
# Check for urgent keywords
|
| 23 |
+
has_urgent = any(word in text for word in urgent_indicators)
|
| 24 |
+
has_high = any(word in text for word in high_indicators)
|
| 25 |
+
|
| 26 |
+
# Determine priority based on both sentiment and keywords
|
| 27 |
+
if has_urgent or (sentiment_label == 'NEGATIVE' and sentiment_score > 0.8):
|
| 28 |
+
return "urgent"
|
| 29 |
+
elif has_high or (sentiment_label == 'NEGATIVE' and sentiment_score > 0.6):
|
| 30 |
+
return "high"
|
| 31 |
+
elif sentiment_label == 'NEGATIVE':
|
| 32 |
+
return "normal"
|
| 33 |
+
else:
|
| 34 |
+
return "low"
|
| 35 |
+
|
| 36 |
+
@app.route('/analyze-priority', methods=['GET'])
|
| 37 |
+
def get_priority():
|
| 38 |
+
text = request.args.get('text', '')
|
| 39 |
+
|
| 40 |
+
if not text:
|
| 41 |
+
return jsonify({
|
| 42 |
+
'error': 'No text provided',
|
| 43 |
+
'status': 400
|
| 44 |
+
}), 400
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
priority = analyze_priority(text)
|
| 48 |
+
sentiment_result = sentiment_classifier(text)[0]
|
| 49 |
+
|
| 50 |
+
return jsonify({
|
| 51 |
+
'text': text,
|
| 52 |
+
'priority': priority,
|
| 53 |
+
'status': 200,
|
| 54 |
+
'details': {
|
| 55 |
+
'sentiment': sentiment_result
|
| 56 |
+
}
|
| 57 |
+
})
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return jsonify({
|
| 60 |
+
'error': f'Analysis failed: {str(e)}',
|
| 61 |
+
'status': 500
|
| 62 |
+
}), 500
|
| 63 |
+
|
| 64 |
+
if __name__ == '__main__':
|
| 65 |
+
app.run(debug=True)
|