Spaces:
Sleeping
Sleeping
File size: 4,366 Bytes
0fc1003 | 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | import os
import logging
from dotenv import load_dotenv
import streamlit as st
from langchain_chroma import Chroma
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_openai import ChatOpenAI
# Get a logger for this module
logger = logging.getLogger(__name__)
logger.info("Design Page...")
# -------------------------------
# PAGE CONFIG (MUST BE FIRST)
# -------------------------------
PORT = int(os.environ.get("PORT", 8501))
st.markdown("""
<style>
.main-title {
font-size: 52px;
font-weight: 800;
text-align: center;
color: #0B5ED7;
margin-bottom: 5px;
}
.sub-title {
font-size: 20px;
text-align: center;
color: #555555;
margin-bottom: 30px;
}
</style>
""", unsafe_allow_html=True)
st.markdown(
'<div class="main-title">๐ AI Medical Labelling System</div>',
unsafe_allow_html=True
)
st.markdown(
'<div class="sub-title">Simplifying FDA Drug Safety Information using Generative AI & RAG</div>',
unsafe_allow_html=True
)
# -------------------------------
# CUSTOM CSS (FANCY DESIGN)
# -------------------------------
st.markdown("""
<style>
.main {
background-color: #f7f9fc;
}
.big-title {
font-size:40px;
font-weight:700;
color:#1f4e79;
}
.subtitle {
font-size:18px;
color:#555;
}
.result-card {
background-color:white;
padding:20px;
border-radius:12px;
box-shadow:0px 2px 10px rgba(0,0,0,0.08);
margin-top:15px;
}
</style>
""", unsafe_allow_html=True)
# -------------------------------
# HEADER
# -------------------------------
st.divider()
# -------------------------------
# SIDEBAR CONTROLS
# -------------------------------
with st.sidebar:
st.header("โ๏ธ Search Options")
drug_name = st.text_input(
"Drug Name",
placeholder="PHENYTOIN SODIUM"
)
selected_results = st.radio(
"Information Type",
["Side Effects", "Warnings", "Both"]
)
run_button = st.button("๐ Generate Explanation")
# -------------------------------
# LOAD ENV + MODELS
# -------------------------------
logger.info("Loading HuggingFace embedding model...")
load_dotenv()
working_dir = os.path.dirname(os.path.abspath(__file__))
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
vectordb = Chroma(
persist_directory=os.path.join(working_dir, "Chroma_db"),
embedding_function=embeddings
)
logger.info("Calling OpenAI model gpt-4o-mini...")
llm = ChatOpenAI(
model="gpt-4o-mini",
temperature=0
)
# -------------------------------
# RAG FUNCTION
# -------------------------------
def generate_section(drug_name, section, rules):
results = vectordb.get(
where={
"$and": [
{"generic_name": drug_name},
{"section": section}
]
}
)
documents = results.get("documents", [])
if not documents:
st.warning(f"No data found for {section}")
return
context = "\n".join(set(documents))
prompt = f"""
You are a medical assistant.
Rewrite the FDA drug information into simplified,
easy-to-understand language.
Rules:
{rules}
Drug: {drug_name}
FDA TEXT:
{context}
"""
with st.spinner("๐ง AI is analysing FDA data..."):
response = llm.invoke(prompt)
st.markdown(
f'<div class="result-card">{response.content}</div>',
unsafe_allow_html=True
)
logger.info("Configuring prompt..")
# -------------------------------
# RULES
# -------------------------------
SIDE_EFFECT_RULES = """
- Use simple English
- Bullet points (max 7)
- Group similar side effects
- Separate common vs serious
"""
WARNING_RULES = """
- Use simple English
- Bullet points (max 7)
- Group warnings clearly
"""
SECTION_MAP = {
"Side Effects": [("adverse_reactions", SIDE_EFFECT_RULES)],
"Warnings": [("warnings_and_cautions", WARNING_RULES)],
"Both": [
("adverse_reactions", SIDE_EFFECT_RULES),
("warnings_and_cautions", WARNING_RULES),
],
}
# -------------------------------
# MAIN ACTION
# -------------------------------
if run_button and drug_name:
st.subheader(f"Results for: {drug_name.upper()}")
for section, rules in SECTION_MAP[selected_results]:
generate_section(drug_name, section, rules)
elif run_button:
st.warning("Please enter a drug name.") |