Spaces:
Sleeping
Sleeping
dkvbabu commited on
Commit ·
bf751d0
0
Parent(s):
Clean deploy - no binary files
Browse files- .gitignore +1 -0
- Dockerfile +16 -0
- flask_api.py +62 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
"sentiment_model.pkl"
|
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy all files
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
# Install dependencies
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# Expose port 7860 (required by HF Spaces)
|
| 13 |
+
EXPOSE 7860
|
| 14 |
+
|
| 15 |
+
# Run Flask app
|
| 16 |
+
CMD ["python", "flask_api.py"]
|
flask_api.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
from flask_cors import CORS
|
| 3 |
+
import joblib
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
CORS(app) # Allows Streamlit Space to call this API
|
| 8 |
+
|
| 9 |
+
# Load model
|
| 10 |
+
model = joblib.load('sentiment_model.pkl')
|
| 11 |
+
|
| 12 |
+
@app.route('/', methods=['GET'])
|
| 13 |
+
def home():
|
| 14 |
+
return jsonify({
|
| 15 |
+
'message': 'FDP Sentiment API is running on Hugging Face!',
|
| 16 |
+
'endpoints': ['/predict POST', '/batch_predict POST', '/health GET']
|
| 17 |
+
})
|
| 18 |
+
|
| 19 |
+
@app.route('/health', methods=['GET'])
|
| 20 |
+
def health():
|
| 21 |
+
return jsonify({'status': 'healthy', 'model': 'TF-IDF + Logistic Regression'})
|
| 22 |
+
|
| 23 |
+
@app.route('/predict', methods=['POST'])
|
| 24 |
+
def predict():
|
| 25 |
+
data = request.get_json()
|
| 26 |
+
text = data.get('text', '').strip()
|
| 27 |
+
|
| 28 |
+
if not text:
|
| 29 |
+
return jsonify({"error": "Empty text provided"}), 400
|
| 30 |
+
if len(text) < 3:
|
| 31 |
+
return jsonify({"error": "Text too short"}), 400
|
| 32 |
+
|
| 33 |
+
pred_label = model.predict([text])[0]
|
| 34 |
+
|
| 35 |
+
confidence = 0.95
|
| 36 |
+
if hasattr(model, 'predict_proba'):
|
| 37 |
+
try:
|
| 38 |
+
proba = model.predict_proba([text])[0]
|
| 39 |
+
classes = list(model.classes_)
|
| 40 |
+
idx = classes.index(str(pred_label))
|
| 41 |
+
confidence = float(proba[idx])
|
| 42 |
+
except Exception:
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
return jsonify({
|
| 46 |
+
"sentiment": str(pred_label),
|
| 47 |
+
"confidence": round(confidence, 4)
|
| 48 |
+
})
|
| 49 |
+
|
| 50 |
+
@app.route('/batch_predict', methods=['POST'])
|
| 51 |
+
def batch():
|
| 52 |
+
data = request.get_json()
|
| 53 |
+
preds = model.predict(data['texts'])
|
| 54 |
+
probas = model.predict_proba(data['texts'])
|
| 55 |
+
results = [
|
| 56 |
+
{'text': t, 'sentiment': p, 'confidence': float(round(max(pr), 4))}
|
| 57 |
+
for t, p, pr in zip(data['texts'], preds, probas)
|
| 58 |
+
]
|
| 59 |
+
return jsonify({'results': results})
|
| 60 |
+
|
| 61 |
+
if __name__ == '__main__':
|
| 62 |
+
app.run(host='0.0.0.0', port=7860) # HF Spaces requires port 7860
|