File size: 2,033 Bytes
10f06ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc6d06f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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"

@app.route('/analyze-priority', methods=['GET'])
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