Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import pandas as pd
|
4 |
+
import time
|
5 |
+
|
6 |
+
# --- Page Configuration ---
|
7 |
+
st.set_page_config(
|
8 |
+
page_title="MediDoc Organizer",
|
9 |
+
page_icon="🩺",
|
10 |
+
layout="wide"
|
11 |
+
)
|
12 |
+
|
13 |
+
# --- Backend API URL ---
|
14 |
+
BACKEND_URL = "http://127.0.0.1:8000"
|
15 |
+
|
16 |
+
# --- Helper Functions ---
|
17 |
+
def check_backend_connection():
|
18 |
+
"""Check if backend is running"""
|
19 |
+
try:
|
20 |
+
response = requests.get(f"{BACKEND_URL}/health", timeout=5)
|
21 |
+
return response.status_code == 200
|
22 |
+
except:
|
23 |
+
return False
|
24 |
+
|
25 |
+
# --- Main Application ---
|
26 |
+
st.title("🩺 MediDoc Organizer")
|
27 |
+
st.write("Your intelligent assistant for organizing and understanding medical reports.")
|
28 |
+
|
29 |
+
# --- Check Backend Connection ---
|
30 |
+
if not check_backend_connection():
|
31 |
+
st.error("Backend server is not running. Please start the FastAPI server first.")
|
32 |
+
st.code("python main.py")
|
33 |
+
st.stop()
|
34 |
+
|
35 |
+
# --- Sidebar ---
|
36 |
+
st.sidebar.title("User Profile")
|
37 |
+
user_name = st.sidebar.text_input("Enter your name", value="Arnav Bansal")
|
38 |
+
st.sidebar.write(f"Welcome, **{user_name}**!")
|
39 |
+
|
40 |
+
# --- Main Tabs ---
|
41 |
+
tab1, tab2, tab3 = st.tabs(["Upload Documents", "View Documents", "Search History"])
|
42 |
+
|
43 |
+
# --- Tab 1: Upload Documents ---
|
44 |
+
with tab1:
|
45 |
+
st.header("Upload Your Medical Documents")
|
46 |
+
st.write("Upload PDF files and images of your medical documents for automatic processing.")
|
47 |
+
|
48 |
+
uploaded_files = st.file_uploader(
|
49 |
+
"Choose your medical documents",
|
50 |
+
type=['pdf', 'png', 'jpg', 'jpeg'],
|
51 |
+
accept_multiple_files=True
|
52 |
+
)
|
53 |
+
|
54 |
+
if st.button("Upload and Process"):
|
55 |
+
if uploaded_files:
|
56 |
+
for uploaded_file in uploaded_files:
|
57 |
+
with st.spinner(f"Processing {uploaded_file.name}..."):
|
58 |
+
files = {'file': (uploaded_file.name, uploaded_file.getvalue(), uploaded_file.type)}
|
59 |
+
|
60 |
+
try:
|
61 |
+
response = requests.post(f"{BACKEND_URL}/upload/", files=files, timeout=60)
|
62 |
+
if response.status_code == 200:
|
63 |
+
result = response.json()
|
64 |
+
st.success(f"Successfully processed {uploaded_file.name}")
|
65 |
+
|
66 |
+
# Show extracted information
|
67 |
+
info = result.get('info', {})
|
68 |
+
st.write(f"**Category:** {info.get('category', 'N/A')}")
|
69 |
+
st.write(f"**Date:** {info.get('document_date', 'N/A')}")
|
70 |
+
st.write(f"**Doctor:** {info.get('doctor_name', 'N/A')}")
|
71 |
+
st.write(f"**Hospital:** {info.get('hospital_name', 'N/A')}")
|
72 |
+
st.write(f"**Summary:** {info.get('summary', 'N/A')}")
|
73 |
+
st.write("---")
|
74 |
+
else:
|
75 |
+
st.error(f"Error processing {uploaded_file.name}: {response.text}")
|
76 |
+
|
77 |
+
except Exception as e:
|
78 |
+
st.error(f"Error processing {uploaded_file.name}: {str(e)}")
|
79 |
+
else:
|
80 |
+
st.warning("Please select files to upload.")
|
81 |
+
|
82 |
+
# --- Tab 2: View Documents ---
|
83 |
+
with tab2:
|
84 |
+
st.header("Your Medical Documents")
|
85 |
+
|
86 |
+
try:
|
87 |
+
response = requests.get(f"{BACKEND_URL}/documents/", timeout=10)
|
88 |
+
if response.status_code == 200:
|
89 |
+
data = response.json()
|
90 |
+
documents = data.get("documents", [])
|
91 |
+
|
92 |
+
if documents:
|
93 |
+
st.write(f"Found {len(documents)} documents")
|
94 |
+
|
95 |
+
# Create a simple table
|
96 |
+
for doc in documents:
|
97 |
+
st.write("**File:**", doc.get('filename', 'Unknown'))
|
98 |
+
st.write("**Category:**", doc.get('category', 'N/A'))
|
99 |
+
st.write("**Date:**", doc.get('document_date', 'N/A'))
|
100 |
+
st.write("**Doctor:**", doc.get('doctor_name', 'N/A'))
|
101 |
+
st.write("**Hospital:**", doc.get('hospital_name', 'N/A'))
|
102 |
+
st.write("**Summary:**", doc.get('summary', 'N/A'))
|
103 |
+
st.write("---")
|
104 |
+
else:
|
105 |
+
st.info("No documents uploaded yet.")
|
106 |
+
else:
|
107 |
+
st.error("Could not retrieve documents from the backend.")
|
108 |
+
except Exception as e:
|
109 |
+
st.error(f"Connection error: {str(e)}")
|
110 |
+
|
111 |
+
# --- Tab 3: Search History ---
|
112 |
+
with tab3:
|
113 |
+
st.header("Search Your Medical History")
|
114 |
+
st.write("Ask questions about your medical history in natural language.")
|
115 |
+
|
116 |
+
# Example queries
|
117 |
+
st.write("**Example questions:**")
|
118 |
+
st.write("- What was the result of my latest blood test?")
|
119 |
+
st.write("- Show me all prescriptions from Dr. Smith")
|
120 |
+
st.write("- What medications am I currently taking?")
|
121 |
+
|
122 |
+
search_query = st.text_input("Enter your question:")
|
123 |
+
|
124 |
+
if st.button("Search"):
|
125 |
+
if search_query.strip():
|
126 |
+
with st.spinner("Searching through your medical records..."):
|
127 |
+
try:
|
128 |
+
response = requests.get(
|
129 |
+
f"{BACKEND_URL}/search/",
|
130 |
+
params={"query": search_query},
|
131 |
+
timeout=30
|
132 |
+
)
|
133 |
+
|
134 |
+
if response.status_code == 200:
|
135 |
+
results = response.json()
|
136 |
+
|
137 |
+
st.write("### Search Results")
|
138 |
+
|
139 |
+
# Display the answer
|
140 |
+
if results.get("answer"):
|
141 |
+
st.write("**Answer:**")
|
142 |
+
st.write(results['answer'])
|
143 |
+
|
144 |
+
# Display sources
|
145 |
+
sources = results.get("sources", [])
|
146 |
+
if sources:
|
147 |
+
st.write("**Sources:**")
|
148 |
+
for source in sources:
|
149 |
+
st.write(f"- {source['filename']}")
|
150 |
+
if 'summary' in source:
|
151 |
+
st.write(f" Summary: {source['summary']}")
|
152 |
+
else:
|
153 |
+
st.warning("Could not find an answer. Try rephrasing your question.")
|
154 |
+
else:
|
155 |
+
st.error(f"Search failed: {response.text}")
|
156 |
+
|
157 |
+
except Exception as e:
|
158 |
+
st.error(f"Search error: {str(e)}")
|
159 |
+
else:
|
160 |
+
st.warning("Please enter a search query.")
|
161 |
+
|
162 |
+
# --- Footer ---
|
163 |
+
st.write("---")
|
164 |
+
st.write("MediDoc Organizer - Built for better healthcare management")
|