Spaces:
Runtime error
Runtime error
MSP RAJA commited on
Commit ·
a30b891
1
Parent(s): f784d15
fixed output
Browse files
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
app.py
CHANGED
|
@@ -3,18 +3,10 @@ from flask import Flask, request, jsonify
|
|
| 3 |
import os
|
| 4 |
from wtforms import Form, StringField
|
| 5 |
from wtforms.validators import DataRequired
|
| 6 |
-
from config import model_ckpt, pipe, labels
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
| 10 |
-
# # configure logging
|
| 11 |
-
# logging.basicConfig(
|
| 12 |
-
# filename='app.log',
|
| 13 |
-
# level=logging.INFO,
|
| 14 |
-
# format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
| 15 |
-
# )
|
| 16 |
-
# logger = logging.getLogger(__name__)
|
| 17 |
-
|
| 18 |
class PredictForm(Form):
|
| 19 |
text = StringField('text', [DataRequired()])
|
| 20 |
|
|
@@ -29,13 +21,19 @@ def predict(text: str) -> dict:
|
|
| 29 |
if preds:
|
| 30 |
pred = preds[0]
|
| 31 |
pred = sorted(pred, key=lambda x: x['score'], reverse=True)
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
return {}
|
| 35 |
except Exception as e:
|
| 36 |
logger.error("Error processing request: %s", str(e))
|
| 37 |
return {'error': str(e)}, 500
|
| 38 |
|
|
|
|
| 39 |
@app.route('/language', methods=['POST'])
|
| 40 |
def predict_language():
|
| 41 |
"""
|
|
@@ -57,11 +55,11 @@ def predict_language():
|
|
| 57 |
description: Invalid request
|
| 58 |
500:
|
| 59 |
description: Internal server error
|
|
|
|
|
|
|
| 60 |
"""
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
text = request.json['text']
|
| 64 |
-
if not text:
|
| 65 |
return jsonify({'error': 'Empty text provided'}), 400
|
| 66 |
|
| 67 |
result = predict(text)
|
|
@@ -69,8 +67,7 @@ def predict_language():
|
|
| 69 |
return jsonify(result)
|
| 70 |
else:
|
| 71 |
return jsonify({'error': 'No predictions found'}), 400
|
| 72 |
-
|
| 73 |
-
# return jsonify({'error': 'Invalid input provided'}), 400
|
| 74 |
|
| 75 |
if __name__ == '__main__':
|
| 76 |
log_file = 'app.log'
|
|
@@ -78,4 +75,3 @@ if __name__ == '__main__':
|
|
| 78 |
logger = logging.getLogger(__name__)
|
| 79 |
logger.info("Running the app...")
|
| 80 |
app.run()
|
| 81 |
-
|
|
|
|
| 3 |
import os
|
| 4 |
from wtforms import Form, StringField
|
| 5 |
from wtforms.validators import DataRequired
|
| 6 |
+
from config import model_ckpt, pipe, labels, THRESHOLD
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
class PredictForm(Form):
|
| 11 |
text = StringField('text', [DataRequired()])
|
| 12 |
|
|
|
|
| 21 |
if preds:
|
| 22 |
pred = preds[0]
|
| 23 |
pred = sorted(pred, key=lambda x: x['score'], reverse=True)
|
| 24 |
+
if pred[0]["score"] > THRESHOLD:
|
| 25 |
+
return {labels.get(p["label"],p["label"]): float(p["score"]) for p in pred[:1]}
|
| 26 |
+
else:
|
| 27 |
+
score = pred[0]["score"]
|
| 28 |
+
logger.error("Prediction score below threshold. text: %s, score: %s", text, score)
|
| 29 |
+
return {'error': "Prediction score below threshold"}
|
| 30 |
else:
|
| 31 |
return {}
|
| 32 |
except Exception as e:
|
| 33 |
logger.error("Error processing request: %s", str(e))
|
| 34 |
return {'error': str(e)}, 500
|
| 35 |
|
| 36 |
+
|
| 37 |
@app.route('/language', methods=['POST'])
|
| 38 |
def predict_language():
|
| 39 |
"""
|
|
|
|
| 55 |
description: Invalid request
|
| 56 |
500:
|
| 57 |
description: Internal server error
|
| 58 |
+
400:
|
| 59 |
+
description: Prediction score below threshold
|
| 60 |
"""
|
| 61 |
+
text = request.json.get('text')
|
| 62 |
+
if not text or len(text)==0:
|
|
|
|
|
|
|
| 63 |
return jsonify({'error': 'Empty text provided'}), 400
|
| 64 |
|
| 65 |
result = predict(text)
|
|
|
|
| 67 |
return jsonify(result)
|
| 68 |
else:
|
| 69 |
return jsonify({'error': 'No predictions found'}), 400
|
| 70 |
+
|
|
|
|
| 71 |
|
| 72 |
if __name__ == '__main__':
|
| 73 |
log_file = 'app.log'
|
|
|
|
| 75 |
logger = logging.getLogger(__name__)
|
| 76 |
logger.info("Running the app...")
|
| 77 |
app.run()
|
|
|
config.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from transformers import pipeline
|
| 2 |
|
|
|
|
| 3 |
model_ckpt = "papluca/xlm-roberta-base-language-detection"
|
| 4 |
pipe = pipeline("text-classification", model=model_ckpt)
|
| 5 |
|
|
|
|
| 1 |
from transformers import pipeline
|
| 2 |
|
| 3 |
+
THRESHOLD = 0.80
|
| 4 |
model_ckpt = "papluca/xlm-roberta-base-language-detection"
|
| 5 |
pipe = pipeline("text-classification", model=model_ckpt)
|
| 6 |
|