Spaces:
Sleeping
Sleeping
Vineela Gampa
commited on
Fixing build yet another time
Browse files- backend.py +52 -27
- web/analyzer.html +13 -4
- web/past_data.html +25 -2
- web/script.js +1 -0
backend.py
CHANGED
|
@@ -24,8 +24,7 @@ from bert import analyze_with_clinicalBert, classify_disease_and_severity, extra
|
|
| 24 |
from disease_links import diseases as disease_links
|
| 25 |
from disease_steps import disease_next_steps
|
| 26 |
from disease_support import disease_doctor_specialty, disease_home_care
|
| 27 |
-
import
|
| 28 |
-
from typing import Optional, List
|
| 29 |
|
| 30 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 31 |
df = pd.read_csv("measurement.csv")
|
|
@@ -43,7 +42,21 @@ api = APIRouter(prefix="/api")
|
|
| 43 |
app.include_router(api)
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
app.mount("/app", StaticFiles(directory="web", html=True), name="web")
|
|
|
|
| 47 |
|
| 48 |
app.add_middleware(
|
| 49 |
CORSMiddleware,
|
|
@@ -82,15 +95,13 @@ try:
|
|
| 82 |
except Exception as e:
|
| 83 |
raise RuntimeError(f"Failed to configure Firebase: {e}")
|
| 84 |
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
class ChatResponse(BaseModel):
|
| 87 |
answer: str
|
| 88 |
|
| 89 |
-
|
| 90 |
-
class ChatRequest(BaseModel):
|
| 91 |
-
question: str
|
| 92 |
-
user_id: Optional[str] = "anonymous"
|
| 93 |
-
|
| 94 |
class ReportData(BaseModel):
|
| 95 |
user_id: str
|
| 96 |
reportDate: Optional[str] = None
|
|
@@ -158,16 +169,7 @@ def ocr_text_from_image(image_bytes: bytes) -> str:
|
|
| 158 |
print(response_text)
|
| 159 |
|
| 160 |
return response_text
|
| 161 |
-
|
| 162 |
-
@app.post("/chat/", response_model=ChatResponse)
|
| 163 |
-
async def chat_endpoint(request: ChatRequest):
|
| 164 |
-
"""
|
| 165 |
-
Chatbot endpoint that answers questions based on the last analyzed document and user history.
|
| 166 |
-
"""
|
| 167 |
-
global EXTRACTED_TEXT_CACHE
|
| 168 |
-
if not EXTRACTED_TEXT_CACHE:
|
| 169 |
-
raise HTTPException(status_code=400, detail="Please provide a document context by analyzing text first.")
|
| 170 |
-
|
| 171 |
try:
|
| 172 |
reports_ref = db.collection('users').document(request.user_id).collection('reports')
|
| 173 |
docs = reports_ref.order_by('timestamp', direction=firestore.Query.DESCENDING).limit(10).stream()
|
|
@@ -178,8 +180,35 @@ async def chat_endpoint(request: ChatRequest):
|
|
| 178 |
history_text += f"Report from {report_data.get('timestamp', 'N/A')}:\n{report_data.get('ocr_text', 'No OCR text found')}\n\n"
|
| 179 |
except Exception as e:
|
| 180 |
history_text = "No past reports found for this user."
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
full_document_text = EXTRACTED_TEXT_CACHE + "\n\n" + "PAST REPORTS:\n" + history_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
try:
|
| 185 |
full_prompt = system_prompt_chat.format(
|
|
@@ -205,6 +234,7 @@ async def analyze(
|
|
| 205 |
filename = file.filename.lower()
|
| 206 |
detected_diseases = set()
|
| 207 |
ocr_full = ""
|
|
|
|
| 208 |
if filename.endswith(".pdf"):
|
| 209 |
pdf_bytes = await file.read()
|
| 210 |
image_bytes_list = extract_images_from_pdf_bytes(pdf_bytes)
|
|
@@ -225,22 +255,19 @@ async def analyze(
|
|
| 225 |
return {"message": "Gemini model not available; please use BERT model."}
|
| 226 |
|
| 227 |
found_diseases = extract_non_negated_keywords(ocr_full)
|
| 228 |
-
print(f"CALLING FOUND DISEASES: {found_diseases}")
|
| 229 |
past = detect_past_diseases(ocr_full)
|
| 230 |
-
print(f"CALLING PAST DISEASES: {past}")
|
| 231 |
|
| 232 |
for disease in found_diseases:
|
| 233 |
if disease in past:
|
| 234 |
severity = classify_disease_and_severity(disease)
|
| 235 |
detected_diseases.add(((f"{disease}(detected as historical condition, but still under risk.)"), severity))
|
| 236 |
-
print(f"DETECTED DISEASES(PAST): {detected_diseases}")
|
| 237 |
else:
|
| 238 |
severity = classify_disease_and_severity(disease)
|
| 239 |
detected_diseases.add((disease, severity))
|
| 240 |
-
|
| 241 |
|
| 242 |
-
|
| 243 |
-
print("Detected diseases:",
|
| 244 |
ranges = analyze_measurements(ocr_full, df)
|
| 245 |
|
| 246 |
|
|
@@ -274,7 +301,6 @@ async def analyze(
|
|
| 274 |
next_steps_range = disease_next_steps.get(condition.lower(), ['Consult a doctor'])
|
| 275 |
specialist_range = disease_doctor_specialty.get(condition.lower(), "General Practitioner")
|
| 276 |
home_care_range = disease_home_care.get(condition.lower(), [])
|
| 277 |
-
print(f"HELLO!: {measurement}")
|
| 278 |
|
| 279 |
condition_version = condition.upper()
|
| 280 |
severity_version = severity.upper()
|
|
@@ -288,12 +314,11 @@ async def analyze(
|
|
| 288 |
"info_link": link_range
|
| 289 |
})
|
| 290 |
|
| 291 |
-
|
| 292 |
ranges = analyze_measurements(ocr_full, df)
|
| 293 |
print(analyze_measurements(ocr_full, df))
|
| 294 |
# print ("Ranges is being printed", ranges)
|
| 295 |
historical_med_data = detect_past_diseases(ocr_full)
|
| 296 |
-
print("***End of Code***")
|
| 297 |
|
| 298 |
return {
|
| 299 |
"ocr_text": ocr_full.strip(),
|
|
|
|
| 24 |
from disease_links import diseases as disease_links
|
| 25 |
from disease_steps import disease_next_steps
|
| 26 |
from disease_support import disease_doctor_specialty, disease_home_care
|
| 27 |
+
from past_reports import router as reports_router, db_fetch_reports
|
|
|
|
| 28 |
|
| 29 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 30 |
df = pd.read_csv("measurement.csv")
|
|
|
|
| 42 |
app.include_router(api)
|
| 43 |
|
| 44 |
|
| 45 |
+
'''app.add_middleware(
|
| 46 |
+
CORSMiddleware,
|
| 47 |
+
allow_origins=[
|
| 48 |
+
"http://localhost:8002"
|
| 49 |
+
"http://localhost:9000"
|
| 50 |
+
"http://localhost:5501"
|
| 51 |
+
],
|
| 52 |
+
allow_credentials=True,
|
| 53 |
+
allow_methods=["*"],
|
| 54 |
+
allow_headers=["*"],
|
| 55 |
+
)'''
|
| 56 |
+
|
| 57 |
+
|
| 58 |
app.mount("/app", StaticFiles(directory="web", html=True), name="web")
|
| 59 |
+
app.include_router(reports_router)
|
| 60 |
|
| 61 |
app.add_middleware(
|
| 62 |
CORSMiddleware,
|
|
|
|
| 95 |
except Exception as e:
|
| 96 |
raise RuntimeError(f"Failed to configure Firebase: {e}")
|
| 97 |
|
| 98 |
+
class ChatRequest(BaseModel):
|
| 99 |
+
user_id: Optional[str] = "anonymous"
|
| 100 |
+
question: str
|
| 101 |
|
| 102 |
class ChatResponse(BaseModel):
|
| 103 |
answer: str
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
class ReportData(BaseModel):
|
| 106 |
user_id: str
|
| 107 |
reportDate: Optional[str] = None
|
|
|
|
| 169 |
print(response_text)
|
| 170 |
|
| 171 |
return response_text
|
| 172 |
+
def get_past_reports_from_firestore(user_id: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
try:
|
| 174 |
reports_ref = db.collection('users').document(request.user_id).collection('reports')
|
| 175 |
docs = reports_ref.order_by('timestamp', direction=firestore.Query.DESCENDING).limit(10).stream()
|
|
|
|
| 180 |
history_text += f"Report from {report_data.get('timestamp', 'N/A')}:\n{report_data.get('ocr_text', 'No OCR text found')}\n\n"
|
| 181 |
except Exception as e:
|
| 182 |
history_text = "No past reports found for this user."
|
| 183 |
+
return history_text
|
| 184 |
+
|
| 185 |
+
def get_past_reports_from_sqllite(user_id: str):
|
| 186 |
+
try:
|
| 187 |
+
reports = db_fetch_reports(user_id=user_id, limit=10, offset=0)
|
| 188 |
+
|
| 189 |
+
history_text = ""
|
| 190 |
+
for report in reports:
|
| 191 |
+
history_text += f"Report from {report.get('report_date', 'N/A')}:\n{report.get('ocr_text', 'No OCR text found')}\n\n"
|
| 192 |
+
except Exception as e:
|
| 193 |
+
history_text = "No past reports found for this user."
|
| 194 |
+
return history_text
|
| 195 |
+
|
| 196 |
+
@app.post("/chat/", response_model=ChatResponse)
|
| 197 |
+
async def chat_endpoint(request: ChatRequest):
|
| 198 |
+
"""
|
| 199 |
+
Chatbot endpoint that answers questions based on the last analyzed document and user history.
|
| 200 |
+
"""
|
| 201 |
+
print("Received chat request for user:", request.user_id)
|
| 202 |
+
#history_text = get_past_reports_from_firestore(request.user_id)
|
| 203 |
+
history_text = get_past_reports_from_sqllite(request.user_id)
|
| 204 |
+
|
| 205 |
full_document_text = EXTRACTED_TEXT_CACHE + "\n\n" + "PAST REPORTS:\n" + history_text
|
| 206 |
+
|
| 207 |
+
if not full_document_text:
|
| 208 |
+
raise HTTPException(status_code=400, detail="No past reports or current data exists for this user")
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
|
| 212 |
|
| 213 |
try:
|
| 214 |
full_prompt = system_prompt_chat.format(
|
|
|
|
| 234 |
filename = file.filename.lower()
|
| 235 |
detected_diseases = set()
|
| 236 |
ocr_full = ""
|
| 237 |
+
print("Received request for file:", filename)
|
| 238 |
if filename.endswith(".pdf"):
|
| 239 |
pdf_bytes = await file.read()
|
| 240 |
image_bytes_list = extract_images_from_pdf_bytes(pdf_bytes)
|
|
|
|
| 255 |
return {"message": "Gemini model not available; please use BERT model."}
|
| 256 |
|
| 257 |
found_diseases = extract_non_negated_keywords(ocr_full)
|
|
|
|
| 258 |
past = detect_past_diseases(ocr_full)
|
|
|
|
| 259 |
|
| 260 |
for disease in found_diseases:
|
| 261 |
if disease in past:
|
| 262 |
severity = classify_disease_and_severity(disease)
|
| 263 |
detected_diseases.add(((f"{disease}(detected as historical condition, but still under risk.)"), severity))
|
|
|
|
| 264 |
else:
|
| 265 |
severity = classify_disease_and_severity(disease)
|
| 266 |
detected_diseases.add((disease, severity))
|
| 267 |
+
|
| 268 |
|
| 269 |
+
|
| 270 |
+
print("Detected diseases:", detected_diseases)
|
| 271 |
ranges = analyze_measurements(ocr_full, df)
|
| 272 |
|
| 273 |
|
|
|
|
| 301 |
next_steps_range = disease_next_steps.get(condition.lower(), ['Consult a doctor'])
|
| 302 |
specialist_range = disease_doctor_specialty.get(condition.lower(), "General Practitioner")
|
| 303 |
home_care_range = disease_home_care.get(condition.lower(), [])
|
|
|
|
| 304 |
|
| 305 |
condition_version = condition.upper()
|
| 306 |
severity_version = severity.upper()
|
|
|
|
| 314 |
"info_link": link_range
|
| 315 |
})
|
| 316 |
|
| 317 |
+
|
| 318 |
ranges = analyze_measurements(ocr_full, df)
|
| 319 |
print(analyze_measurements(ocr_full, df))
|
| 320 |
# print ("Ranges is being printed", ranges)
|
| 321 |
historical_med_data = detect_past_diseases(ocr_full)
|
|
|
|
| 322 |
|
| 323 |
return {
|
| 324 |
"ocr_text": ocr_full.strip(),
|
web/analyzer.html
CHANGED
|
@@ -190,6 +190,8 @@
|
|
| 190 |
</ul>
|
| 191 |
</nav>
|
| 192 |
|
|
|
|
|
|
|
| 193 |
<script>
|
| 194 |
const hamburger = document.getElementById("hamburger");
|
| 195 |
const mobileMenu = document.getElementById("mobile-menu");
|
|
@@ -470,7 +472,7 @@
|
|
| 470 |
|
| 471 |
let data;
|
| 472 |
try {
|
| 473 |
-
const res = await fetch("
|
| 474 |
method: "POST",
|
| 475 |
body: formData,
|
| 476 |
});
|
|
@@ -512,11 +514,18 @@
|
|
| 512 |
}
|
| 513 |
|
| 514 |
if (currentUser) {
|
| 515 |
-
await saveAnalysis(currentUser.uid, {
|
| 516 |
reportDate: date,
|
| 517 |
ocr_text: extractedText,
|
| 518 |
resolutions: recs,
|
| 519 |
measurements: findings,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 520 |
});
|
| 521 |
}
|
| 522 |
|
|
@@ -525,7 +534,7 @@
|
|
| 525 |
|
| 526 |
async function postReportToBackend(report) {
|
| 527 |
try {
|
| 528 |
-
const response = await fetch('
|
| 529 |
method: 'POST',
|
| 530 |
headers: {
|
| 531 |
'Content-Type': 'application/json',
|
|
@@ -559,7 +568,7 @@
|
|
| 559 |
chat.scrollTop = chat.scrollHeight;
|
| 560 |
|
| 561 |
try {
|
| 562 |
-
const response = await fetch(
|
| 563 |
method: "POST",
|
| 564 |
headers: {
|
| 565 |
"Content-Type": "application/json",
|
|
|
|
| 190 |
</ul>
|
| 191 |
</nav>
|
| 192 |
|
| 193 |
+
<!--Shared helpers (API base + query params) -->
|
| 194 |
+
<script src="script.js"></script>
|
| 195 |
<script>
|
| 196 |
const hamburger = document.getElementById("hamburger");
|
| 197 |
const mobileMenu = document.getElementById("mobile-menu");
|
|
|
|
| 472 |
|
| 473 |
let data;
|
| 474 |
try {
|
| 475 |
+
const res = await fetch(api("analyze/"), {
|
| 476 |
method: "POST",
|
| 477 |
body: formData,
|
| 478 |
});
|
|
|
|
| 514 |
}
|
| 515 |
|
| 516 |
if (currentUser) {
|
| 517 |
+
/*await saveAnalysis(currentUser.uid, {
|
| 518 |
reportDate: date,
|
| 519 |
ocr_text: extractedText,
|
| 520 |
resolutions: recs,
|
| 521 |
measurements: findings,
|
| 522 |
+
});*/
|
| 523 |
+
await postReportToBackend({
|
| 524 |
+
user_id: currentUser.email,
|
| 525 |
+
report_date: new Date(),
|
| 526 |
+
ocr_text: extractedText,
|
| 527 |
+
anomalies: JSON.stringify(recs),
|
| 528 |
+
measurements: JSON.stringify(findings),
|
| 529 |
});
|
| 530 |
}
|
| 531 |
|
|
|
|
| 534 |
|
| 535 |
async function postReportToBackend(report) {
|
| 536 |
try {
|
| 537 |
+
const response = await fetch(api('save_report/'), {
|
| 538 |
method: 'POST',
|
| 539 |
headers: {
|
| 540 |
'Content-Type': 'application/json',
|
|
|
|
| 568 |
chat.scrollTop = chat.scrollHeight;
|
| 569 |
|
| 570 |
try {
|
| 571 |
+
const response = await fetch(api('chat/'), {
|
| 572 |
method: "POST",
|
| 573 |
headers: {
|
| 574 |
"Content-Type": "application/json",
|
web/past_data.html
CHANGED
|
@@ -114,8 +114,31 @@
|
|
| 114 |
onAuthStateChanged(auth, async (user) => {
|
| 115 |
if (user) {
|
| 116 |
statusEl.textContent = `Signed in as ${user.email || user.uid}`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
const q = query(
|
| 120 |
collection(db, "users", user.uid, "analyses"),
|
| 121 |
orderBy("createdAt", "desc")
|
|
@@ -150,7 +173,7 @@
|
|
| 150 |
} catch (error) {
|
| 151 |
console.error('Error fetching analyses:', error);
|
| 152 |
recsEl.innerHTML = '<p class="text-sm text-red-500">Error loading analyses.</p>';
|
| 153 |
-
}
|
| 154 |
|
| 155 |
} else {
|
| 156 |
statusEl.textContent = "Not signed in.";
|
|
|
|
| 114 |
onAuthStateChanged(auth, async (user) => {
|
| 115 |
if (user) {
|
| 116 |
statusEl.textContent = `Signed in as ${user.email || user.uid}`;
|
| 117 |
+
|
| 118 |
+
async function getPastReports() {
|
| 119 |
+
try {
|
| 120 |
+
const url = api('reports/', { user_id: user.email });
|
| 121 |
+
const response = await fetch(url, {
|
| 122 |
+
method: 'GET',
|
| 123 |
+
headers: {
|
| 124 |
+
'Content-Type': 'application/json',
|
| 125 |
+
},
|
| 126 |
+
});
|
| 127 |
+
if (!response.ok) {
|
| 128 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
| 129 |
+
}
|
| 130 |
+
const data = await response.json();
|
| 131 |
+
console.log('Report successfully sent to backend:', data);
|
| 132 |
+
recsEl.innerHTML = data.map(doc => renderAnalysis(doc)).join("");
|
| 133 |
+
} catch (error) {
|
| 134 |
+
console.error('Error sending report to backend:', error);
|
| 135 |
+
recsEl.innerHTML = '<p class="text-sm text-gray-500">No saved analyses yet.</p>';
|
| 136 |
+
}
|
| 137 |
+
}
|
| 138 |
+
getPastReports();
|
| 139 |
+
|
| 140 |
|
| 141 |
+
/* try {
|
| 142 |
const q = query(
|
| 143 |
collection(db, "users", user.uid, "analyses"),
|
| 144 |
orderBy("createdAt", "desc")
|
|
|
|
| 173 |
} catch (error) {
|
| 174 |
console.error('Error fetching analyses:', error);
|
| 175 |
recsEl.innerHTML = '<p class="text-sm text-red-500">Error loading analyses.</p>';
|
| 176 |
+
}*/
|
| 177 |
|
| 178 |
} else {
|
| 179 |
statusEl.textContent = "Not signed in.";
|
web/script.js
CHANGED
|
@@ -27,6 +27,7 @@
|
|
| 27 |
: API_BASE + (path.startsWith("/") ? path : "/" + path);
|
| 28 |
|
| 29 |
const url = new URL(full);
|
|
|
|
| 30 |
if (params && typeof params === "object") {
|
| 31 |
for (const [k, v] of Object.entries(params)) {
|
| 32 |
if (v === undefined || v === null) continue;
|
|
|
|
| 27 |
: API_BASE + (path.startsWith("/") ? path : "/" + path);
|
| 28 |
|
| 29 |
const url = new URL(full);
|
| 30 |
+
console.log("Calling api :",url);
|
| 31 |
if (params && typeof params === "object") {
|
| 32 |
for (const [k, v] of Object.entries(params)) {
|
| 33 |
if (v === undefined || v === null) continue;
|