Message
Analysis: Excellent! The model has been successfully downloaded to `/app/model_cache/sentiment_model`. The download included:
- tokenizer_config.json
- config.json
- vocab.txt
- model.safetensors (268MB)
Now I need to create the Flask API that:
1. Loads the model from the local cache
2. Exposes a POST endpoint at /sentiment
3. Accepts JSON with "text" field
4. Returns sentiment analysis results with confidence scores
5. Runs on port 5000, accessible from 0.0.0.0
Plan: 1. Create a Flask application script (app.py) that:
- Loads the model and tokenizer from the cache directory
- Implements the /sentiment POST endpoint
- Processes text and returns sentiment with confidence scores
- Handles errors appropriately with 400 status codes
2. Run the Flask app in the background on port 5000
3. Verify the service is running
New Terminal Output:
root@h200-006-219:/app# cat > app.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_name = "distilbert-base-uncased-finetuned-sst-2-english"
> cache_dir = "/app/model_cache/sentiment_model"
>
> print("Loading model and tokenizer from cache...")
> tokenizer = AutoTokenizer.from_pretrained(model_name, cache_dir=cache_dir)
> model = AutoModelForSequenceClassification.from_pretrained(model_name, cache_dir=cache_dir)
> 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 body"}), 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
>
> # Apply softmax to get probabilities
> probabilities = torch.softmax(logits, dim=1)[0]
>
> # Get confidence scores (model outputs: [negative, positive])
> 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
> }
> })
>
> except Exception as e:
> return jsonify({"error": str(e)}), 400
>
> if __name__ == '__main__':
> app.run(host='0.0.0.0', port=5000)
> EOF
root@h200-006-219:/app# nohup python3 app.py > flask.log 2>&1 &
[1] 900691
root@h200-006-219:/app# sleep 3 && cat flask.log
nohup: ignoring input
Loading model and tokenizer from cache...
Model loaded successfully!
* Serving Flask app 'app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://10.137.226.203:5000
Press CTRL+C to quit
root@h200-006-219:/app#
Prompt: 2,864 tokens
Completion: 970 tokens