36 lines
1018 B
Python
36 lines
1018 B
Python
from flask import Flask, request, jsonify
|
|
import os
|
|
import json
|
|
|
|
app = Flask(__name__)
|
|
|
|
ANNOTATION_FILE = (
|
|
"spacy_training/annotation_data.json" # relativer Pfad im Container/Projekt
|
|
)
|
|
|
|
|
|
@app.route("/api/spacy-training-entry", methods=["POST"])
|
|
def append_training_entry():
|
|
new_entry = request.get_json()
|
|
|
|
if not new_entry or "text" not in new_entry or "entities" not in new_entry:
|
|
return jsonify({"error": "Ungültiges Format"}), 400
|
|
|
|
# Bestehende Datei laden
|
|
if os.path.exists(ANNOTATION_FILE):
|
|
with open(ANNOTATION_FILE, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
else:
|
|
data = []
|
|
|
|
# Optional: Duplikat vermeiden
|
|
if new_entry in data:
|
|
return jsonify({"message": "Eintrag bereits vorhanden."}), 200
|
|
|
|
# Anfügen
|
|
data.append(new_entry)
|
|
with open(ANNOTATION_FILE, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, indent=2, ensure_ascii=False)
|
|
|
|
return jsonify({"message": "Eintrag erfolgreich gespeichert."}), 200
|