Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created March 4, 2024 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsh-code/15fe8e46c948b9830a236089d0682877 to your computer and use it in GitHub Desktop.
Save tsh-code/15fe8e46c948b9830a236089d0682877 to your computer and use it in GitHub Desktop.
spacy flask example
from flask import Flask, request, jsonify
import spacy
app = Flask(__name__)
nlp = spacy.load("en_core_web_sm")
def extract_people(text: str):
entities = nlp(text)
full_names = set()
for entity in entities.ents:
if entity.label_ in ['PER', 'PERSON']:
# Check if the entity has both a first name and a last name
if len(entity.text.split()) >= 2:
full_names.add(entity.text)
return list(full_names)
@app.route("/people", methods=['POST'])
def people():
data = request.json
content = data.get('content')
entities = extract_people(content)
return jsonify({"entities": entities})
if __name__ == "__main__":
app.run(port=8000, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment