Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import spacy
|
3 |
+
from spacy import displacy
|
4 |
+
import json
|
5 |
+
import subprocess
|
6 |
+
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_md"])
|
7 |
+
|
8 |
+
|
9 |
+
# Initialize spaCy
|
10 |
+
nlp = spacy.load("en_core_web_md")
|
11 |
+
|
12 |
+
# Define sample data
|
13 |
+
data = {
|
14 |
+
"fruit": ["apple", "pear", "orange"],
|
15 |
+
"vegetable": ["broccoli", "spinach", "tomato"],
|
16 |
+
"meat": ['beef', 'pork', 'turkey', 'duck']
|
17 |
+
}
|
18 |
+
|
19 |
+
# Streamlit app
|
20 |
+
st.title('Named Entity Recognition with spaCy')
|
21 |
+
|
22 |
+
user_input = st.text_area("Enter text:", "")
|
23 |
+
|
24 |
+
if st.button("Process"):
|
25 |
+
if user_input:
|
26 |
+
# Process the text
|
27 |
+
doc = nlp(user_input)
|
28 |
+
|
29 |
+
# Visualization options
|
30 |
+
options = {
|
31 |
+
"colors": {"fruit": "darkorange", "vegetable": "limegreen", "meat": "salmon"},
|
32 |
+
"ents": ["fruit", "vegetable", "meat"],
|
33 |
+
}
|
34 |
+
|
35 |
+
# JSON serialization with only entity and type
|
36 |
+
result_dict = {'entities': []}
|
37 |
+
|
38 |
+
for ent in doc.ents:
|
39 |
+
ent_data = {
|
40 |
+
'entity': ent.text,
|
41 |
+
'type': ent.label_
|
42 |
+
}
|
43 |
+
result_dict['entities'].append(ent_data)
|
44 |
+
|
45 |
+
result_json = json.dumps(result_dict, indent=4)
|
46 |
+
|
47 |
+
# Display results
|
48 |
+
st.subheader("Named Entities")
|
49 |
+
html = displacy.render(doc, style="ent", page=True, minify=True)
|
50 |
+
st.write(html, unsafe_allow_html=True)
|
51 |
+
st.subheader("Entities in JSON format")
|
52 |
+
st.json(result_json)
|
53 |
+
|