File size: 6,782 Bytes
cf6fecb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests
import pandas as pd
import time

# --- Page Configuration ---
st.set_page_config(
    page_title="MediDoc Organizer",
    page_icon="🩺",
    layout="wide"
)

# --- Backend API URL ---
BACKEND_URL = "http://127.0.0.1:8000"

# --- Helper Functions ---
def check_backend_connection():
    """Check if backend is running"""
    try:
        response = requests.get(f"{BACKEND_URL}/health", timeout=5)
        return response.status_code == 200
    except:
        return False

# --- Main Application ---
st.title("🩺 MediDoc Organizer")
st.write("Your intelligent assistant for organizing and understanding medical reports.")

# --- Check Backend Connection ---
if not check_backend_connection():
    st.error("Backend server is not running. Please start the FastAPI server first.")
    st.code("python main.py")
    st.stop()

# --- Sidebar ---
st.sidebar.title("User Profile")
user_name = st.sidebar.text_input("Enter your name", value="Arnav Bansal")
st.sidebar.write(f"Welcome, **{user_name}**!")

# --- Main Tabs ---
tab1, tab2, tab3 = st.tabs(["Upload Documents", "View Documents", "Search History"])

# --- Tab 1: Upload Documents ---
with tab1:
    st.header("Upload Your Medical Documents")
    st.write("Upload PDF files and images of your medical documents for automatic processing.")

    uploaded_files = st.file_uploader(
        "Choose your medical documents",
        type=['pdf', 'png', 'jpg', 'jpeg'],
        accept_multiple_files=True
    )
    
    if st.button("Upload and Process"):
        if uploaded_files:
            for uploaded_file in uploaded_files:
                with st.spinner(f"Processing {uploaded_file.name}..."):
                    files = {'file': (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)}
                    
                    try:
                        response = requests.post(f"{BACKEND_URL}/upload/", files=files, timeout=60)
                        if response.status_code == 200:
                            result = response.json()
                            st.success(f"Successfully processed {uploaded_file.name}")
                            
                            # Show extracted information
                            info = result.get('info', {})
                            st.write(f"**Category:** {info.get('category', 'N/A')}")
                            st.write(f"**Date:** {info.get('document_date', 'N/A')}")
                            st.write(f"**Doctor:** {info.get('doctor_name', 'N/A')}")
                            st.write(f"**Hospital:** {info.get('hospital_name', 'N/A')}")
                            st.write(f"**Summary:** {info.get('summary', 'N/A')}")
                            st.write("---")
                        else:
                            st.error(f"Error processing {uploaded_file.name}: {response.text}")
                            
                    except Exception as e:
                        st.error(f"Error processing {uploaded_file.name}: {str(e)}")
        else:
            st.warning("Please select files to upload.")

# --- Tab 2: View Documents ---
with tab2:
    st.header("Your Medical Documents")
    
    try:
        response = requests.get(f"{BACKEND_URL}/documents/", timeout=10)
        if response.status_code == 200:
            data = response.json()
            documents = data.get("documents", [])
            
            if documents:
                st.write(f"Found {len(documents)} documents")
                
                # Create a simple table
                for doc in documents:
                    st.write("**File:**", doc.get('filename', 'Unknown'))
                    st.write("**Category:**", doc.get('category', 'N/A'))
                    st.write("**Date:**", doc.get('document_date', 'N/A'))
                    st.write("**Doctor:**", doc.get('doctor_name', 'N/A'))
                    st.write("**Hospital:**", doc.get('hospital_name', 'N/A'))
                    st.write("**Summary:**", doc.get('summary', 'N/A'))
                    st.write("---")
            else:
                st.info("No documents uploaded yet.")
        else:
            st.error("Could not retrieve documents from the backend.")
    except Exception as e:
        st.error(f"Connection error: {str(e)}")

# --- Tab 3: Search History ---
with tab3:
    st.header("Search Your Medical History")
    st.write("Ask questions about your medical history in natural language.")
    
    # Example queries
    st.write("**Example questions:**")
    st.write("- What was the result of my latest blood test?")
    st.write("- Show me all prescriptions from Dr. Smith")
    st.write("- What medications am I currently taking?")
    
    search_query = st.text_input("Enter your question:")

    if st.button("Search"):
        if search_query.strip():
            with st.spinner("Searching through your medical records..."):
                try:
                    response = requests.get(
                        f"{BACKEND_URL}/search/", 
                        params={"query": search_query},
                        timeout=30
                    )
                    
                    if response.status_code == 200:
                        results = response.json()
                        
                        st.write("### Search Results")
                        
                        # Display the answer
                        if results.get("answer"):
                            st.write("**Answer:**")
                            st.write(results['answer'])
                            
                            # Display sources
                            sources = results.get("sources", [])
                            if sources:
                                st.write("**Sources:**")
                                for source in sources:
                                    st.write(f"- {source['filename']}")
                                    if 'summary' in source:
                                        st.write(f"  Summary: {source['summary']}")
                        else:
                            st.warning("Could not find an answer. Try rephrasing your question.")
                    else:
                        st.error(f"Search failed: {response.text}")
                        
                except Exception as e:
                    st.error(f"Search error: {str(e)}")
        else:
            st.warning("Please enter a search query.")

# --- Footer ---
st.write("---")
st.write("MediDoc Organizer - Built for better healthcare management")