Spaces:
Sleeping
Sleeping
Download flask_app.py from VectorV17/new-klein: direct link, hf CLI and curl.
- Browser
- Download file 969 Bytes
-
https://huggingface.co/spaces/VectorV17/new-klein/resolve/main/flask_app.py
- Command line
-
hf download hf://spaces/VectorV17/new-klein/flask_app.py
-
curl -L -o flask_app.py https://huggingface.co/spaces/VectorV17/new-klein/resolve/main/flask_app.py
969 Bytes
| from flask import Flask, request, jsonify | |
| import joblib | |
| import re | |
| app = Flask(__name__) | |
| pipeline = joblib.load('intent_pipeline.pkl') | |
| label_encoder = joblib.load('label_encoder.pkl') | |
| def clean_text(text): | |
| text = str(text).lower() | |
| return re.sub(r'[^a-z0-9\s]', '', text).strip() | |
| def predict(): | |
| data = request.get_json(force=True) | |
| text = data.get('text', '') | |
| if not text.strip(): | |
| return jsonify({'error': 'Empty input text'}), 400 | |
| cleaned = clean_text(text) | |
| pred_idx = pipeline.predict([cleaned])[0] | |
| predicted_intent = label_encoder.inverse_transform([pred_idx])[0] | |
| probs = pipeline.predict_proba([cleaned])[0] | |
| confidence = float(max(probs)) | |
| return jsonify({ | |
| 'input_text': text, | |
| 'predicted_intent': predicted_intent, | |
| 'confidence': round(confidence, 4) | |
| }) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=7860, debug=False) |