Spaces:
Running
Running
Update backend.py
Browse files- backend.py +27 -52
backend.py
CHANGED
@@ -24,7 +24,8 @@ 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 |
-
|
|
|
28 |
|
29 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
30 |
df = pd.read_csv("measurement.csv")
|
@@ -42,21 +43,7 @@ api = APIRouter(prefix="/api")
|
|
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,13 +82,15 @@ try:
|
|
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,7 +158,16 @@ def ocr_text_from_image(image_bytes: bytes) -> str:
|
|
169 |
print(response_text)
|
170 |
|
171 |
return response_text
|
172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,35 +178,8 @@ def get_past_reports_from_firestore(user_id: str):
|
|
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,7 +205,6 @@ async def analyze(
|
|
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,19 +225,22 @@ async def analyze(
|
|
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:",
|
271 |
ranges = analyze_measurements(ocr_full, df)
|
272 |
|
273 |
|
@@ -301,6 +274,7 @@ async def analyze(
|
|
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,11 +288,12 @@ async def analyze(
|
|
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(),
|
|
|
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 datetime
|
28 |
+
from typing import Optional, List
|
29 |
|
30 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
31 |
df = pd.read_csv("measurement.csv")
|
|
|
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 |
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 |
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 |
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 |
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 |
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 |
+
print(f"DETECTED DISEASES: {detected_diseases}")
|
241 |
|
242 |
+
print("OCR TEXT:", ocr_text)
|
243 |
+
print("Detected diseases:", found_diseases)
|
244 |
ranges = analyze_measurements(ocr_full, df)
|
245 |
|
246 |
|
|
|
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 |
"info_link": link_range
|
289 |
})
|
290 |
|
291 |
+
print(ocr_full)
|
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(),
|