File size: 2,345 Bytes
192ba4d
786f732
3af157b
 
 
 
 
47e3bd8
 
3af157b
 
786f732
 
ded56f4
 
 
 
 
 
3af157b
ded56f4
3af157b
786f732
 
 
3af157b
 
ded56f4
3af157b
 
ded56f4
3af157b
ded56f4
3af157b
ded56f4
3af157b
ded56f4
3af157b
 
 
192ba4d
 
 
3af157b
 
 
 
ded56f4
3af157b
ded56f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import shutil
import streamlit as st
from llm import load_llm, response_generator
from vector_store import load_vector_store, process_pdf
from uuid import uuid4

repo_id = "Qwen/Qwen2.5-1.5B-Instruct-GGUF"
filename = "qwen2.5-1.5b-instruct-q8_0.gguf"

llm = load_llm(repo_id, filename)
vector_store = load_vector_store()

st.title("Medical Triage System")
st.subheader("Upload Referral Letters for Triage")
st.write(
    "This AI-powered system analyzes referral letters to classify them as **Urgent** or **Routine** "
    "and suggests either a **Face-to-Face** or **Virtual Appointment**."
)

# Initialize state
if "messages" not in st.session_state:
    vector_store.reset_collection()
    if os.path.exists("./temp"):
        shutil.rmtree("./temp")
    st.session_state.messages = []

# Display chat messages
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.write(message["content"])

# Accept PDF uploads
with st.sidebar:
    st.title("Upload Referral Letters")
    uploaded_files = st.file_uploader(
        "Choose PDF files", accept_multiple_files=True, type="pdf"
    )
    if uploaded_files is not None:
        for uploaded_file in uploaded_files:
            temp_dir = "./temp"
            if not os.path.exists(temp_dir):
                os.makedirs(temp_dir)
            temp_file = f"./temp/{uploaded_file.name}-{uuid4()}.pdf"
            with open(temp_file, "wb") as file:
                file.write(uploaded_file.getvalue())

            st.write(f"Processing {uploaded_file.name}...")
            process_pdf(temp_file, vector_store)
            st.success(f"Processed {uploaded_file.name} successfully. ✅")

# Process user query
if prompt := st.text_input("Enter triage-related query (e.g., 'Is this urgent?')"):
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        retriever = vector_store.as_retriever(search_kwargs={"k": 3})
        response = response_generator(llm, st.session_state.messages, prompt, retriever)

        st.markdown(response["answer"])
        with st.expander("See Context"):
            st.write(response["context"])

    st.session_state.messages.append({"role": "assistant", "content": response["answer"]})