Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
139 lines (109 loc) · 4.16 KB
/
Copy pathapp.py
File metadata and controls
139 lines (109 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import streamlit as st
import pandas as pd
from collections import defaultdict
from bio.ner_prediction import ner_prediction, highlight_entities
from db import init_db, save_record, get_records_by_patient
init_db()
st.set_page_config(page_title="MedNERDS", layout="wide")
st.title("🧬 MedNERDS App")
tab1, tab2 = st.tabs(["📝 Insert", "🔍 Find"])
with tab1:
# ==========================
# INPUT UI
# ==========================
patient_id = st.text_input("Introduce patient ID:", placeholder="Ej: 12345")
text = st.text_area(
"Introduce clinical notes:",
height=180,
placeholder="Ej: Patient diagnosed with diabetes and prescribed ibuprofen...",
)
run = st.button("Analize")
# ==========================
# PROCESS
# ==========================
if run and text.strip() and patient_id.strip():
pred_df, final_df = ner_prediction(text)
# convertir DataFrame → lista de dicts adaptados
entities = []
for _, row in pred_df.iterrows():
entities.append(
{
"type": row["entity_group"],
"value": row["value"],
"start": row["start"],
"end": row["end"],
"score": float(row["score"]),
}
)
grouped = defaultdict(list)
for ent in entities:
grouped[ent["type"]].append(ent["value"])
counts = {k: len(v) for k, v in grouped.items()}
# ==========================
# Texto con Highlights
# ==========================
st.subheader("📄 Highlighted Entities")
html = highlight_entities(text, entities)
st.markdown(html, unsafe_allow_html=True)
col1, col2 = st.columns(2)
# ==========================
# DETALLE ENTIDADES
# ==========================
with col1:
st.subheader("🔎 Recognized Entities")
if entities:
df = pd.DataFrame(entities)
st.dataframe(df, use_container_width=True, hide_index=True)
else:
st.info("No entities were detected at that threshold.")
# ==========================
# AGRUPACIÓN
# ==========================
with col2:
st.subheader("📊 Grouped by type")
if grouped:
df_grouped = pd.DataFrame(
[(k, v, counts[k]) for k, v in grouped.items()],
columns=["Type", "Values", "Count"],
)
st.dataframe(df_grouped, use_container_width=True, hide_index=True)
else:
st.info("Ungrouped data.")
# ==========================
# JSON RAW (debug útil)
# ==========================
st.markdown("---")
st.subheader("🧾 Structured Output")
finalOutput = final_df.copy()
finalOutput.insert(0, "Patient Id", patient_id)
st.dataframe(finalOutput, use_container_width=True, hide_index=True)
save_record(patient_id, final_df)
with tab2:
st.subheader("🔍 Search by patient ID")
search_id = st.text_input(
"Enter patient ID to search:", placeholder="Ej: 12345"
)
search_btn = st.button("Search")
if search_btn and search_id.strip():
records = get_records_by_patient(search_id)
if records:
df = pd.DataFrame(records)
df = df.rename(
columns={
"patient_id": "Patient ID",
"age": "Age",
"sex": "Sex",
"history": "History",
"symptoms": "Symptoms",
"medication": "Medication",
"created_at": "Date",
}
)
st.dataframe(df, use_container_width=True, hide_index=True)
else:
st.warning("No records found with that ID.")
# ==========================
# FOOTER
# ==========================
st.markdown("---")
st.caption("Biomedical MedNERDs · PyCamp 2026")