MohitGupta41 commited on
Commit
bc5bc44
·
1 Parent(s): adb9295

Initial Commit

Browse files
Files changed (1) hide show
  1. Src/api/fastapi_app.py +4 -286
Src/api/fastapi_app.py CHANGED
@@ -1,289 +1,3 @@
1
- # # from fastapi import FastAPI, Query
2
- # # from pydantic import BaseModel
3
- # # from datetime import datetime
4
- # # from ..services.db import init_db, seed_data, get_connection
5
- # # from ..services.patient_service import register_patient as save_patient
6
- # # from ..services.medicine_service import check_medicine_availability
7
- # # from ..rag.rag_pipeline import rag_query_multimodal
8
- # # from ..services.doctor_assignment import assign_doctor_with_gemma
9
-
10
- # # app = FastAPI()
11
-
12
- # # # ----------------------------
13
- # # # Startup: Init DB & Seed
14
- # # # ----------------------------
15
- # # @app.on_event("startup")
16
- # # def startup_event():
17
- # # init_db()
18
- # # seed_data()
19
-
20
- # # # ----------------------------
21
- # # # 1. Chatbot Endpoint (RAG)
22
- # # # ----------------------------
23
- # # @app.get("/query")
24
- # # def query_bot(q: str = Query(..., description="Medical question")):
25
- # # answer, references = rag_query_multimodal(q, k=5)
26
- # # return {"answer": answer, "references": references}
27
-
28
- # # # ----------------------------
29
- # # # 2. Register Patient + Assign Doctor
30
- # # # ----------------------------
31
- # # class PatientData(BaseModel):
32
- # # name: str
33
- # # age: int
34
- # # reason: str
35
-
36
- # # @app.post("/register_patient")
37
- # # def register_patient_api(data: PatientData):
38
- # # # Assign doctor using Gemma
39
- # # doctor, reasoning = assign_doctor_with_gemma(data.reason)
40
-
41
- # # if not doctor:
42
- # # return {"message": "No suitable doctor found. Please try again later."}
43
-
44
- # # # Save patient and doctor_id to DB
45
- # # patient_id = save_patient(data.name, data.age, data.reason, doctor["id"])
46
-
47
- # # # Convert doctor row to dict
48
- # # doctor_dict = {key: doctor[key] for key in doctor.keys()} if doctor else None
49
-
50
- # # return {
51
- # # "patient_id": patient_id,
52
- # # "assigned_doctor": doctor_dict,
53
- # # "reasoning": reasoning
54
- # # }
55
-
56
- # # # ----------------------------
57
- # # # 3. Check Registration & Confirm Appointment
58
- # # # ----------------------------
59
- # # class AppointmentData(BaseModel):
60
- # # name: str # Patient name
61
-
62
- # # @app.post("/check_registration_status")
63
- # # def check_registration_status(data: AppointmentData):
64
- # # conn = get_connection()
65
- # # cur = conn.cursor()
66
-
67
- # # # Get patient and assigned doctor
68
- # # cur.execute("""
69
- # # SELECT p.id AS patient_id, p.name AS patient_name, d.id AS doctor_id,
70
- # # d.name AS doctor_name, d.specialization, d.available
71
- # # FROM patients p
72
- # # LEFT JOIN doctors d ON p.doctor_id = d.id
73
- # # WHERE p.name = ?
74
- # # """, (data.name,))
75
- # # record = cur.fetchone()
76
-
77
- # # if not record:
78
- # # conn.close()
79
- # # return {"message": "Patient not registered. Please register first."}
80
-
81
- # # if not record["doctor_id"]:
82
- # # conn.close()
83
- # # return {"message": "No doctor assigned yet. Please register again."}
84
-
85
- # # # Mark doctor as unavailable if available
86
- # # if record["available"] == 1:
87
- # # cur.execute(
88
- # # "UPDATE doctors SET available=0, last_booked_at=? WHERE id=?",
89
- # # (datetime.now().isoformat(), record["doctor_id"])
90
- # # )
91
- # # conn.commit()
92
-
93
- # # conn.close()
94
-
95
- # # return {
96
- # # "message": f"Appointment confirmed with {record['doctor_name']} ({record['specialization']}) for {record['patient_name']}."
97
- # # }
98
-
99
- # # # ----------------------------
100
- # # # 4. Medicine Availability
101
- # # # ----------------------------
102
- # # @app.get("/medicine_availability")
103
- # # def medicine_availability_api(name: str = Query(..., description="Medicine name")):
104
- # # result = check_medicine_availability(name)
105
- # # return {"message": result}
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
- # # Src/api/fastapi_app.py
117
-
118
- # from fastapi import FastAPI, Query, Header, HTTPException
119
-
120
- # from pydantic import BaseModel
121
- # from datetime import datetime
122
-
123
- # # DB and services
124
- # from ..services.db import init_db, seed_data, get_connection
125
- # from ..services.patient_service import register_patient as save_patient, get_patient_full_case
126
- # from ..services.medicine_service import check_medicine_availability
127
- # from ..services.doctor_assignment import assign_doctor_with_gemma
128
- # from ..services.summarizer import summarize_patient_case
129
- # # RAG pipeline
130
- # from ..rag.rag_pipeline import rag_query_multimodal
131
-
132
- # # Agent system
133
- # from ..agent.orchestrator import orchestrate_query
134
- # from ..agent.agent_executor import get_agent_executor
135
-
136
- # # Initialize FastAPI app
137
- # app = FastAPI()
138
-
139
- # # ----------------------------
140
- # # Startup: Init DB & Seed
141
- # # ----------------------------
142
- # @app.on_event("startup")
143
- # def startup_event():
144
- # init_db()
145
- # seed_data()
146
-
147
- # # ----------------------------
148
- # # 1. Medical RAG Chatbot
149
- # # ----------------------------
150
- # @app.get("/query")
151
- # def query_bot(q: str = Query(..., description="Medical question"), authorization: str = Header(...)):
152
- # """
153
- # Query the medical knowledge base (PDF + captions) using RAG.
154
- # """
155
- # hf_token = authorization.replace("Bearer ", "")
156
- # answer, references = rag_query_multimodal(q, k=10, hf_token=hf_token)
157
- # return {"answer": answer, "references": references}
158
-
159
- # # ----------------------------
160
- # # 2. Register Patient + Assign Doctor
161
- # # ----------------------------
162
- # class PatientData(BaseModel):
163
- # name: str
164
- # age: int
165
- # reason: str
166
-
167
- # @app.post("/register_patient")
168
- # def register_patient_api(data: PatientData, authorization: str = Header(...)):
169
- # """
170
- # Register patient and assign doctor using Gemma LLM.
171
- # """
172
- # # Assign doctor via LLM
173
- # hf_token = authorization.replace("Bearer ", "")
174
- # doctor, reasoning = assign_doctor_with_gemma(data.reason, hf_token=hf_token)
175
-
176
- # if not doctor:
177
- # return {"message": "No suitable doctor found. Please try again later."}
178
-
179
- # # Save patient record with doctor_id
180
- # patient_record = save_patient({"name": data.name, "age": data.age, "reason": data.reason}, doctor["id"])
181
-
182
- # return {
183
- # "patient_id": patient_record["id"],
184
- # "assigned_doctor": doctor,
185
- # "reasoning": reasoning
186
- # }
187
-
188
- # # ----------------------------
189
- # # 3. Check Registration & Confirm Appointment
190
- # # ----------------------------
191
- # class AppointmentData(BaseModel):
192
- # name: str # Patient name
193
-
194
- # @app.post("/check_registration_status")
195
- # def check_registration_status(data: AppointmentData):
196
- # """
197
- # Confirm appointment for registered patient with already assigned doctor.
198
- # """
199
- # conn = get_connection()
200
- # cur = conn.cursor()
201
-
202
- # # Get patient + doctor details
203
- # cur.execute("""
204
- # SELECT p.id AS patient_id, p.name AS patient_name, d.id AS doctor_id,
205
- # d.name AS doctor_name, d.specialization, d.available
206
- # FROM patients p
207
- # LEFT JOIN doctors d ON p.doctor_id = d.id
208
- # WHERE p.name = ?
209
- # """, (data.name,))
210
- # record = cur.fetchone()
211
-
212
- # if not record:
213
- # conn.close()
214
- # return {"message": "Patient not registered. Please register first."}
215
-
216
- # if not record["doctor_id"]:
217
- # conn.close()
218
- # return {"message": "No doctor assigned yet. Please register again."}
219
-
220
- # # Mark doctor unavailable if available
221
- # if record["available"] == 1:
222
- # cur.execute(
223
- # "UPDATE doctors SET available=0, last_booked_at=? WHERE id=?",
224
- # (datetime.now().isoformat(), record["doctor_id"])
225
- # )
226
- # conn.commit()
227
-
228
- # conn.close()
229
-
230
- # return {
231
- # "message": f"Appointment confirmed with {record['doctor_name']} ({record['specialization']}) for {record['patient_name']}."
232
- # }
233
-
234
- # # ----------------------------
235
- # # 4. Medicine Availability
236
- # # ----------------------------
237
- # @app.get("/medicine_availability")
238
- # def medicine_availability_api(name: str = Query(..., description="Medicine name")):
239
- # """
240
- # Check if a medicine is available in the pharmacy stock.
241
- # """
242
- # result = check_medicine_availability(name)
243
- # return {"message": result}
244
-
245
- # # ----------------------------
246
- # # 5. Summarize Patient case
247
- # # ----------------------------
248
- # @app.get("/summarize_case/{patient_id}")
249
- # def summarize_case_api(patient_id: int):
250
- # patient_data = get_patient_full_case(patient_id)
251
- # if not patient_data:
252
- # return {"message": "Patient not found"}
253
- # summary = summarize_patient_case(patient_data)
254
- # return {"summary": summary}
255
-
256
-
257
- # # ----------------------------
258
- # # 6. Agent Endpoint (Full LangChain Agent)
259
- # # ----------------------------
260
- # @app.get("/agent_query")
261
- # def agent_query(q: str = Query(..., description="General hospital or medical query")):
262
- # """
263
- # Use full LangChain agent (tools + RAG) to process queries.
264
- # """
265
- # agent = get_agent_executor()
266
- # response = agent.run(q)
267
- # return {"response": response}
268
-
269
- # # ----------------------------
270
- # # 6. Simple Orchestrator Endpoint
271
- # # ----------------------------
272
- # @app.get("/orchestrator_query")
273
- # def orchestrator_query(q: str = Query(..., description="Simple classified query")):
274
- # """
275
- # Use lightweight orchestrator to classify and execute tools or RAG.
276
- # """
277
- # result, references = orchestrate_query(q)
278
- # return {"result": result, "references": references}
279
-
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
  from fastapi import FastAPI, Query, Header, HTTPException
288
  from pydantic import BaseModel
289
  from datetime import datetime
@@ -312,6 +26,10 @@ def startup_event():
312
  init_db()
313
  seed_data()
314
 
 
 
 
 
315
  # ----------------------------
316
  # 1. Medical RAG Chatbot
317
  # ----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, Query, Header, HTTPException
2
  from pydantic import BaseModel
3
  from datetime import datetime
 
26
  init_db()
27
  seed_data()
28
 
29
+ @app.get("/")
30
+ def read_root():
31
+ return {"Hello": "World!"}
32
+
33
  # ----------------------------
34
  # 1. Medical RAG Chatbot
35
  # ----------------------------