Message
Analysis: Excellent! The model has been downloaded successfully to /app/model_cache/sentiment_model. Now I need to:
1. Create the Flask API application with the sentiment endpoint
2. Run the service in the background on port 5000, accessible from 0.0.0.0
The Flask app needs to:
- Load the model from the local cache
- Expose a POST endpoint at /sentiment
- Accept JSON with {"text": "..."}
- Return sentiment analysis results with confidence scores
- Handle errors appropriately with 400 status codes
Plan: 1. Create a Flask application file (sentiment_api.py) that:
- Loads the model from /app/model_cache/sentiment_model
- Creates a POST /sentiment endpoint
- Processes text input and returns sentiment with confidence scores
- Handles errors with proper error messages and 400 status codes
2. Run the Flask application in the background using nohup or &
3. Verify the service is running
New Terminal Output:
root@h200-006-219:/app# cat > sentiment_api.py << 'EOF'
> from flask import Flask, request, jsonify
> from transformers import AutoTokenizer, AutoModelForSequenceClassification
> import torch
>
> app = Flask(__name__)
>
> # Load model and tokenizer from local cache
> MODEL_PATH = "/app/model_cache/sentiment_model"
> print(f"Loading model from {MODEL_PATH}...")
> tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
> model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
> print("Model loaded successfully!")
>
> @app.route('/sentiment', methods=['POST'])
> def analyze_sentiment():
> try:
> # Get JSON data from request
> data = request.get_json()
>
> # Validate input
> if not data:
> return jsonify({"error": "No JSON data provided"}), 400
>
> if "text" not in data:
> return jsonify({"error": "Missing 'text' field in request"}), 400
>
> text = data["text"]
>
> if not isinstance(text, str):
> return jsonify({"error": "'text' field must be a string"}), 400
>
> if not text.strip():
> return jsonify({"error": "'text' field cannot be empty"}), 400
>
> # Tokenize and run inference
> inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
>
> with torch.no_grad():
> outputs = model(**inputs)
> logits = outputs.logits
> probabilities = torch.nn.functional.softmax(logits, dim=1)[0]
>
> # Get confidence scores
> negative_score = probabilities[0].item()
> positive_score = probabilities[1].item()
>
> # Determine sentiment
> sentiment = "positive" if positive_score > negative_score else "negative"
>
> # Return response
> return jsonify({
> "sentiment": sentiment,
> "confidence": {
> "positive": positive_score,
> "negative": negative_score
> }
> }), 200
>
> except Exception as e:
> return jsonify({"error": str(e)}), 400
>
> if __name__ == '__main__':
> app.run(host='0.0.0.0', port=5000, debug=False)
> EOF
root@h200-006-219:/app# nohup python3 sentiment_api.py > sentiment_api.log 2>&1 &
[1] 2699881
root@h200-006-219:/app# sleep 3
root@h200-006-219:/app# ps aux | grep sentiment_api
bash: ps: command not found
root@h200-006-219:/app#
Prompt: 4,987 tokens
Completion: 942 tokens