Spaces:
Runtime error
Runtime error
| from flask import Flask, request, jsonify, render_template | |
| from transformers import AutoModelForTokenClassification, AutoTokenizer, pipeline | |
| from flask_cors import CORS | |
| import logging | |
| logging.basicConfig(level=logging.DEBUG) | |
| # Log untuk mendeteksi worker Gunicorn | |
| if __name__ != '__main__': | |
| logging.info("Gunicorn worker started") | |
| app = Flask(__name__) | |
| CORS(app) | |
| model_path = "./model" | |
| logging.info("Loading model and tokenizer...") | |
| model = AutoModelForTokenClassification.from_pretrained(model_path) | |
| tokenizer = AutoTokenizer.from_pretrained(model_path) | |
| nlp = pipeline("token-classification", model=model, tokenizer=tokenizer) | |
| logging.info("Model and tokenizer loaded successfully.") | |
| def home(): | |
| return render_template('index.html') | |
| def pos_tag(): | |
| data = request.json | |
| text = data.get('text') if data else None | |
| if not text: | |
| return jsonify({"error": "Please provide input text"}), 400 | |
| # Handling UTF-8 encoding issues | |
| text = text.encode('utf-8').decode('utf-8') | |
| try: | |
| results = nlp(text) | |
| tagged_tokens = [ | |
| {"word": res["word"].replace('Ġ', ''), | |
| "tag": res["entity"].replace('B-', '').replace('I-', '')} | |
| for res in results | |
| ] | |
| return jsonify({"tagged_tokens": tagged_tokens}) | |
| except Exception as e: | |
| logging.error(f"Error processing text: {str(e)}") | |
| return jsonify({"error": "Error processing text"}), 500 | |
| if __name__ == '__main__': | |
| app.run(port=5007, debug=True) | |