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 
 
 
 
 
 
 
 
 
 
 
 
 
 
b895059
b73051f
 
2d51bc9
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534



# General
import os
import kagglehub
import pandas as pd
import json
from typing import Literal
from datasets import load_dataset
import random

#Markdown
from IPython.display import Markdown, display, Image

# Image
from PIL import Image

# langchain for llms
from langchain_groq import ChatGroq

# Langchain
from langchain.prompts import PromptTemplate, ChatPromptTemplate
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.messages import HumanMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import END, START, StateGraph, MessagesState
from langgraph.prebuilt import ToolNode
from langchain_core.tools import tool

# Hugging Face
from transformers import AutoModelForImageClassification, AutoProcessor

from langchain_huggingface import HuggingFaceEmbeddings


# Extra libraries
from pydantic import BaseModel, Field, model_validator

# Advanced RAG
from langchain_core.documents import Document
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.retrievers.multi_query import MultiQueryRetriever
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser


# ## APIs



os.environ["SERPER_API_KEY"] = os.getenv("SERPER_API_KEY")
os.environ["GROQ_API_KEY"] = os.getenv("GROQ_API_KEY")
os.environ["HF_TOKEN"] = os.getenv("HF_TOKEN")

GROQ_API_KEY = os.environ["GROQ_API_KEY"]
HF_TOKEN = os.environ["HF_TOKEN"]


# ## Setup LLM (Llama 3.3 via Groq)

# Note: Model 3.2 70b is not available on Groq any more
# We will be using 3.3 from Now on



os.environ["GROQ_API_KEY"] = GROQ_API_KEY

#model_3_2 = 'llama-3.2-11b-text-preview' => his model has been removed from Groq platform
model_3_2_small = 'llama-3.1-8b-instant' # Smaller Model 3 Billion parameters if you need speed
model_3_3 ='llama-3.3-70b-versatile' # Very Large and Versatile Model with 70 Billion parameters

llm = ChatGroq(
    model= model_3_3, #
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
   # groq_api_key=os.getenv("GROQ_API_KEY")
    # other params...
)

# A test message
# new text:
response = llm.invoke("hi, Please generate 10 unique Dutch names for both male and female?")
response



display(Markdown(response.content))


# # First Agent: Chatbot Agent




from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages


class ChatState(TypedDict):
    # Messages have the type "list". The `add_messages` function
    # in the annotation defines how this state key should be updated
    # (in this case, it appends messages to the list, rather than overwriting them)
    messages: Annotated[list, add_messages]


chat_graph = StateGraph(ChatState)

def chatbot_agent(state: ChatState):
    return {"messages": [llm.invoke(state["messages"])]}

# The first argument is the unique node name
# The second argument is the function or object that will be called whenever
# the node is used.
chat_graph.add_node("chatbot_agent", chatbot_agent)
chat_graph.add_edge(START, "chatbot_agent")
chat_graph.add_edge("chatbot_agent", END)

# Finally, we'll want to be able to run our graph. To do so, call "compile()"
# We basically now give our AI Agent
graph_app = chat_graph.compile()

# Persistent state to maintain conversation history
persistent_state = {"messages": []}  # Start with an empty message list




from IPython.display import Image, display
display(Image(graph_app.get_graph(xray=True).draw_mermaid_png()))




from typing import Annotated
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from IPython.display import display, Markdown

class ChatState(TypedDict):
    messages: Annotated[list, add_messages]

chat_graph = StateGraph(ChatState)

def chatbot_agent(state: ChatState):
    # Assuming `llm` is your language model that can handle the conversation history
    return {"messages": [llm.invoke(state["messages"])]}

chat_graph.add_node("chatbot_agent", chatbot_agent)
chat_graph.add_edge(START, "chatbot_agent")
chat_graph.add_edge("chatbot_agent", END)

graph_app = chat_graph.compile()

# Persistent state to maintain conversation history
persistent_state = {"messages": []}  # Start with an empty message list

def stream_graph_updates(user_input: str):
    global persistent_state
    # Append the user's message to the persistent state
    persistent_state["messages"].append(("user", user_input))

    is_finished = False
    for event in graph_app.stream(persistent_state):
        for value in event.values():
            last_msg = value["messages"][-1]
            display(Markdown("Assistant: " + last_msg.content))

            # Append the assistant's response to the persistent state
            persistent_state["messages"].append(("assistant", last_msg.content))

            finish_reason = last_msg.response_metadata.get("finish_reason")
            if finish_reason == "stop":
                is_finished = True
                break
        if is_finished:
            break

while True:
    try:
        user_input = input('User:')
        if user_input.lower() in ["quit", "exit", "q"]:
            print("Thank you and Goodbye!")
            break

        stream_graph_updates(user_input)
    except Exception as e:
        print(f"An error occurred: {e}")
        break






# # Second Agent: Add Search to Chatbot to make it Stronger


from langchain_community.tools import GoogleSerperResults
from typing import List, Annotated
from langchain_core.messages import BaseMessage
from langgraph.prebuilt import ToolNode, create_react_agent
import operator
import functools

class ChatState(TypedDict):
    # Messages have the type "list". The `add_messages` function
    # in the annotation defines how this state key should be updated
    # (in this case, it appends messages to the list, rather than overwriting them)
    messages: Annotated[list, add_messages]

def agent_node(state, agent, name):
    result = agent.invoke(state)
    return {
        "messages": [HumanMessage(content=result["messages"][-1].content, name=name)]
    }


class SearchState(TypedDict):
    # A message is added after each team member finishes
    messages: Annotated[List[BaseMessage], operator.add]

# Search Tool

serper_tool = GoogleSerperResults(
    num_results=5,
    # how many Google results to return
)

search_agent = create_react_agent(llm, tools=[serper_tool])
search_node = functools.partial(agent_node,
                                agent=search_agent,
                                name="search_agent")


# The first argument is the unique node name
# The second argument is the function or object that will be called whenever
# the node is used.
search_graph = StateGraph(SearchState)
search_graph.add_node("search_agent", search_node)
search_graph.add_edge(START, "search_agent")
search_graph.add_edge("search_agent", END)

# Finally, we'll want to be able to run our graph. To do so, call "compile()"
# We basically now give our AI Agent
search_app = search_graph.compile()






from IPython.display import Image, display
display(Image(search_app.get_graph(xray=True).draw_mermaid_png()))




from langchain_community.tools import GoogleSerperResults
from typing import List, Annotated
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.prebuilt import ToolNode, create_react_agent
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from IPython.display import display, Markdown
import operator
import functools

class ChatState(TypedDict):
    messages: Annotated[List[BaseMessage], operator.add]

def agent_node(state, agent, name):
    result = agent.invoke(state)
    return {
        "messages": [HumanMessage(content=result["messages"][-1].content, name=name)]
    }

class SearchState(TypedDict):
    messages: Annotated[List[BaseMessage], operator.add]

# Search Tool
serper_tool = GoogleSerperResults(num_results=5)  # how many Google results to return

search_agent = create_react_agent(llm, tools=[serper_tool])
search_node = functools.partial(agent_node, agent=search_agent, name="search_agent")

# Create the search graph
search_graph = StateGraph(SearchState)
search_graph.add_node("search_agent", search_node)
search_graph.add_edge(START, "search_agent")
search_graph.add_edge("search_agent", END)

# Compile the search graph
search_app = search_graph.compile()

# Persistent state to maintain conversation history
persistent_state = {"messages": []}  # Start with an empty message list

def stream_graph_updates(user_input: str):
    global persistent_state
    # Append the user's message to the persistent state
    persistent_state["messages"].append(HumanMessage(content=user_input))

    # Display "Searching the Web Now..." message
    display(Markdown("**Assistant:** Searching the Web Now..."))

    is_finished = False
    for event in search_app.stream(persistent_state):
        for value in event.values():
            last_msg = value["messages"][-1]
            display(Markdown("**Assistant:** " + last_msg.content))

            # Append the assistant's response to the persistent state
            persistent_state["messages"].append(last_msg)

            finish_reason = last_msg.response_metadata.get("finish_reason")
            if finish_reason == "stop":
                is_finished = True
                break
        if is_finished:
            break

while True:
    try:
        user_input = input('User:')
        if user_input.lower() in ["quit", "exit", "q"]:
            print("Thank you and Goodbye!")
            break

        stream_graph_updates(user_input)
    except Exception as e:
        print(f"An error occurred: {e}")
        break


# # Step 1: Medical Database Preparation
# This step involves preparing and enhancing patient data to be used throughout the simulation.

# ## 1.1 Load Dataset

# ### 1.1.1  Disease Symptoms and Patient Profile Dataset
# Ensure you have downloaded it and placed it in your project directory.
# - https://www.kaggle.com/datasets/uom190346a/disease-symptoms-and-patient-profile-dataset




# Download latest version
path = kagglehub.dataset_download("uom190346a/disease-symptoms-and-patient-profile-dataset")
print("Path to dataset files:", path)




patient_df = pd.read_csv(path+'/Disease_symptom_and_patient_profile_dataset.csv')
patient_df.shape




patient_df.head()




# Calculate the counts of each gender
female_count = patient_df[patient_df['Gender'] == 'Female'].shape[0]
male_count = patient_df[patient_df['Gender'] == 'Male'].shape[0]

# Calculate the ratio
ratio = female_count / male_count
print(f"The ratio of Female to Male is {ratio}:1")





patient_df['Disease'].value_counts().head(20)


# **prepare_medical_dataset Code in One Plalce**



def prepare_medical_dataset(path, file_name):
  patient_df = pd.read_csv(path+file_name)
  return patient_df

path = kagglehub.dataset_download("uom190346a/disease-symptoms-and-patient-profile-dataset")
file_name = '/Disease_symptom_and_patient_profile_dataset.csv'
patient_df = prepare_medical_dataset(path, file_name)


# ### 1.1.2 Chest X-Ray Images (Pneumonia)
# 
# - https://huggingface.co/lxyuan/vit-xray-pneumonia-classification
# - https://huggingface.co/datasets/keremberke/chest-xray-classification
# 
# 



#from datasets import load_dataset
#patient_x_ray_path = "keremberke/chest-xray-classification"
#x_ray_ds = load_dataset(patient_x_ray_path, name="full")
from datasets import load_dataset
x_ray_ds = load_dataset("keremberke/chest-xray-classification", name="full")



random_index = random.randint(0, x_ray_ds['train'].shape[0] - 1)
patient_x_ray = random_row = x_ray_ds['train'][random_index]['image']

from datasets import load_dataset
x_ray_ds = load_dataset("keremberke/chest-xray-classification", name="full")




x_ray_ds['train'].shape[0]





# Assuming x_ray_ds['train'] is a dataset where we want to pick a random row
import random
random_index = random.randint(0, x_ray_ds['train'].shape[0] - 1)




patient_x_ray = x_ray_ds['train'][random_index]['image']
patient_x_ray




type(patient_x_ray)




#!pip install --upgrade accelerate==0.31.0
#!pip install --upgrade huggingface-hub>=0.23.0





from transformers import pipeline

# Model in Hugging Face: https://huggingface.co/lxyuan/vit-xray-pneumonia-classification
# vit-xray-pneumonia-classification
classifier = pipeline(model="lxyuan/vit-xray-pneumonia-classification")
patient_x_ray_results = classifier(patient_x_ray)
patient_x_ray_results




# Find the label with the highest score
patient_x_ray_label = max(patient_x_ray_results, key=lambda x: x['score'])['label']
print(patient_x_ray_label)



# Model in Hugging Face: https://huggingface.co/lxyuan/vit-xray-pneumonia-classification
# vit-xray-pneumonia-classification
classifier = pipeline(model="lxyuan/vit-xray-pneumonia-classification")
patient_x_ray_results = classifier(patient_x_ray)

# Find the label with the highest score and its score
highest = max(patient_x_ray_results, key=lambda x: x['score'])
highest_score_label = highest['label']
highest_score = highest['score'] * 100  # Convert to percentage

# Choose the correct verb based on the label
verb = "is" if highest_score_label == "NORMAL" else "has"

# Print the result dynamically
print(f"Patient {verb} {highest_score_label} with Probability of ca. {highest_score:.0f}%")


# ## 1.2 Generate Synthetic Data with LLMs
# Generate culturally appropriate Dutch names and unique alphanumeric IDs for each patient.

# ### 1.2.1 Generate Random Names and IDs for Patience

# This Code Goes Slower because of Llama 3.3 70b being very big and slow LLM
# comparing to llama 3.2 11b
# Switch to model_3_2_smal when running this code



# === Step 1: Define Response Schemas ===
# Define the structure of the expected JSON output.

# ResponseSchema for First_Name
first_name_schema = ResponseSchema(
    name="First_Name",
    description="The first name of the patient."
)

# ResponseSchema for Last_Name
last_name_schema = ResponseSchema(
    name="Last_Name",
    description="The last name of the patient."
)

# ResponseSchema for Patient_ID
patient_id_schema = ResponseSchema(
    name="Patient_ID",
    description="A unique 13-character alphanumeric patient identifier."
)

# ResponseSchema for Patient_ID
gender_schema = ResponseSchema(
    name="G_Gender",
    description="Indicate the first name you generate belong which Gender: Male or Female"
)

# Aggregate all response schemas
response_schemas = [
    first_name_schema,
    last_name_schema,
    patient_id_schema,
    gender_schema
]

# === Step 2: Set Up the Output Parser ===
# Initialize the StructuredOutputParser with the defined response schemas.

output_parser = StructuredOutputParser.from_response_schemas(response_schemas)

# Get the format instructions to include in the prompt
format_instructions = output_parser.get_format_instructions()

# === Step 3: Craft the Prompt ===
# Create a prompt that instructs the LLM to generate only the structured JSON data.

# Define the prompt template using ChatPromptTemplate
prompt_template = ChatPromptTemplate.from_template("""
you MUST Generate a list of {n} Dutch names along with a unique 13-character alphanumeric Patient_ID for each gender provided.
Always Use {genders} to generate a First_Name which belong to the right Gender, two category is possible: 'Male' or 'Female'.
Ensure the names are culturally appropriate for the Netherlands.
Generate unique names, no repetitions, and ensure diversity.
The ratio of Female to Male is {ratio}:1

{format_instructions}

Genders:
{genders}

**IMPORTANT:** Do not include any explanations, code, or additional text.
you MUST ALWAYS generate Dutch names and Patient_ID according {format_instructions}
and NEVER return empty values.
YOU MUST Provide only the JSON array as specified.
JSON array Should have exactly {n} rows and 3 columns
""")

# Determine the number of patients
n_patients = len(patient_df)
#n_patients = 120
# Calculate the counts of each gender
female_count = patient_df[patient_df['Gender'] == 'Female'].shape[0]
male_count = patient_df[patient_df['Gender'] == 'Male'].shape[0]

# Calculate the ratio
ratio = female_count / male_count

# Prepare the list of genders
genders = patient_df['Gender'].tolist()

# === Step 6: Generate the Prompt ===
# Format the prompt with the number of patients and their genders.

formatted_prompt = prompt_template.format(
    n=n_patients,
    ratio = ratio,
    genders=', '.join(genders),
    format_instructions=format_instructions
)

# Invoke the model with s Smaller Llama Model for Speed
model_3_2_small = 'llama-3.1-8b-instant' # if you need speed

llm = ChatGroq(
    model= model_3_2_small, #
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2
)

output = llm.invoke(formatted_prompt, timeout=1000)




display(Markdown(output.content))




output_parser = JsonOutputParser()
json_output = output_parser.invoke(output)
json_output





all_patients = []
generated_patients = pd.DataFrame(json_output)
generated_patients.head(5)





generated_patients.shape




# Adjusted LLM parameters (if supported)
llm.temperature = 0.9  # Increases randomness

all_patients_name_id = pd.DataFrame()
output_parser = JsonOutputParser()

while all_patients_name_id.shape[0] < n_patients:
  output = llm.invoke(formatted_prompt)
  json_output = output_parser.invoke(output)
  generated_patients = pd.DataFrame(json_output)
  all_patients_name_id = pd.concat([generated_patients, all_patients_name_id], axis = 0)
  print(f"len all_patients_name_id: {len(all_patients_name_id)}")
  all_patients_name_id =  all_patients_name_id.drop_duplicates()
  print(f"len all_patients_name_id after droping duplicates: {len(all_patients_name_id)}")





all_patients_name_id.rename(columns = {"G_Gender": "Gender"}, inplace= True)
all_patients_name_id.head(10)





gender_counts = patient_df['Gender'].value_counts()
gender_counts




all_patients_name_id['Gender'].value_counts()





# Step 1: Count the number of males and females in patient_df
gender_counts = patient_df['Gender'].value_counts()

# Step 2: Select the required number of unique males and females from all_patients_name_id
unique_males = all_patients_name_id[all_patients_name_id['Gender'] == 'Male'].drop_duplicates().head(gender_counts['Male'])
unique_females = all_patients_name_id[all_patients_name_id['Gender'] == 'Female'].drop_duplicates().head(gender_counts['Female'])


patient_male = patient_df[patient_df['Gender'] == 'Male'].reset_index(drop=True)
patient_female = patient_df[patient_df['Gender'] == 'Female'].reset_index(drop=True)


updated_male_patients = pd.concat([patient_male.reset_index(drop=True),
                                   unique_males[0:patient_male.shape[0]].reset_index(drop=True)],
                                   axis = 1)

updated_female_patients = pd.concat([patient_female.reset_index(drop=True),
                                   unique_females[0:patient_female.shape[0]].reset_index(drop=True)],
                                   axis = 1)

# Step 3: Concatenate patient_df with the selected rows from all_patients_name_id
updated_patient_df = pd.concat([updated_male_patients, updated_female_patients], axis = 0)




updated_patient_df.shape[0]





# Display the final concatenated dataframe
updated_patient_df





updated_patient_df = updated_patient_df.loc[:, ~updated_patient_df.columns.duplicated()]
updated_patient_df




updated_patient_df['Gender'].value_counts()


# #### 1.2.1.1 Select a Random Patient




# Pick a Random Patient: A female between 20 and 29 and with Pneumonia as Positive so that later we can check X-Ray Agent
mask = (updated_patient_df['Gender'] == 'Female') & \
       (updated_patient_df["Age"].between(20, 29)) & \
        (updated_patient_df['Difficulty Breathing'] == 'Yes') & \
         (updated_patient_df['Outcome Variable'] == 'Positive')
selected_patients = updated_patient_df[mask].reset_index(drop=True)
selected_patients.head()





selected_patient = selected_patients.iloc[0]
selected_patient


# # Step 2: Create IDentity Photo for the Front Desk Agent

# ## 2.1 Build the Vision Model for Gender Classification (Image Classification Task)

# In[46]:


# Use a pipeline as a high-level helper
from transformers import pipeline

pipe = pipeline("image-classification", model="rizvandwiki/gender-classification")


# In[47]:


# Load model directly
from transformers import AutoImageProcessor, AutoModelForImageClassification

processor = AutoImageProcessor.from_pretrained("rizvandwiki/gender-classification")
model = AutoModelForImageClassification.from_pretrained("rizvandwiki/gender-classification")


# In machine learning, particularly in classification tasks, logits are the raw, unnormalized outputs produced by a model's final layer before any activation function is applied. These outputs represent the model's confidence scores for each class and are essential for subsequent probability calculations.

# In[48]:


from transformers import AutoModelForImageClassification, AutoProcessor
from PIL import Image
import requests

# Load the model and processor
model_name = "rizvandwiki/gender-classification"
model = AutoModelForImageClassification.from_pretrained(model_name)
processor = AutoProcessor.from_pretrained(model_name)

# Load the image from URL or local path
image_url = "https://thispersondoesnotexist.com"
image = Image.open(requests.get(image_url, stream=True).raw)

# Prepare the image for the model
inputs = processor(images=image, return_tensors="pt")

# Perform inference
outputs = model(**inputs)
logits = outputs.logits
predicted_class = logits.argmax(-1).item()

# Map prediction to class label
classes = model.config.id2label
gender_label = classes[predicted_class]

print(f"Predicted Gender: {gender_label}")





import matplotlib.pyplot as plt

# Display the image and prediction
plt.imshow(image)
plt.axis('off')  # Hide axes
plt.title(f"Predicted Gender: {gender_label}")
plt.show()


# ## 2.2 Build the Vision Model for Age Classification (Image Classification Task)



# Load age classification model
age_model_name = "nateraw/vit-age-classifier"
age_model = AutoModelForImageClassification.from_pretrained(age_model_name)
age_processor = AutoProcessor.from_pretrained(age_model_name)





# Age Prediction
age_inputs = age_processor(images=image, return_tensors="pt")
age_outputs = age_model(**age_inputs)
age_logits = age_outputs.logits
age_prediction = age_logits.argmax(-1).item()
age_label = age_model.config.id2label[age_prediction]
age_label




# Display the image with both predictions
plt.imshow(image)
plt.axis('off')
plt.title(f"Predicted Gender: {gender_label}, Predicted Age: {age_label}")
plt.show()


# # Step 3: Start Building Multi-Agents
# 
# Define Each AI Agent
# We'll define agents for:
# 
# * Administration Front Desk
# * Physician for General Health Examination + Blood Laboratory
# * X-Ray Image Department

# ## 3.1 Hospital Front Desk Agent
# 
# 

# **--IMPORTANT NOTE--** <br>
# 1. Don't forget to save one photo from https://thispersondoesnotexist.com/
# <br>  as female.jpg and save it to this Path "/content/sample_data/'
# <br> which is standard path within your Google Colab
# 
# ---
# 2. Don't Forget to Save one of the images from the x-ray-dataset <br>**Load Dataset in this way:** <br>
# patient_x_ray_path = "keremberke/chest-xray-classification" <br>
# x_ray_ds = load_dataset(patient_x_ray_path, name="full")
# <br> Then save one image labelled as x-ray-chest.jpg to the path "/content/sample_data/'




patient_x_ray_path = "keremberke/chest-xray-classification"
x_ray_ds = load_dataset(patient_x_ray_path, name="full")




from typing import List, Tuple, Dict, Any, Sequence, Annotated, Literal
from typing_extensions import TypedDict
from langchain_core.messages import BaseMessage
import operator
import functools
from langchain_core.messages import HumanMessage
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import END, START, StateGraph, MessagesState
from langgraph.prebuilt import ToolNode, create_react_agent
from langchain_core.tools import tool
from transformers import AutoModelForImageClassification, AutoProcessor
from PIL import Image
from pydantic import BaseModel

from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_core.prompts import ChatPromptTemplate

# Annotated in python allows developers to declare the type of a reference and provide additional information related to it.
# Literal, after that the value are exact and literal





#----------------- Build Fucntions that Agents use ----------------------

def patient_verification_tool(image_Path, selected_patient_data,  updated_patient_df) -> str:
    """Detects the gender from an image provided as a file path."""
    from PIL import Image
    print(image_Path)
    model = AutoModelForImageClassification.from_pretrained("rizvandwiki/gender-classification")
    processor = AutoProcessor.from_pretrained("rizvandwiki/gender-classification")
    image = Image.open(image_Path)
    inputs = processor(images=image, return_tensors="pt")
    outputs = model(**inputs)
    predicted_class = outputs.logits.argmax(-1).item()
    print(f"Predicted Gender Of Patient is : {model.config.id2label[predicted_class]}")
    predicted_gender = model.config.id2label[predicted_class]

    from PIL import Image
    model = AutoModelForImageClassification.from_pretrained("nateraw/vit-age-classifier")
    processor = AutoProcessor.from_pretrained("nateraw/vit-age-classifier")
    image = Image.open(image_Path)
    inputs = processor(images=image, return_tensors="pt")
    outputs = model(**inputs)
    predicted_class = outputs.logits.argmax(-1).item()
    print(f"predicted Age Class: {model.config.id2label[predicted_class]}")
    predicted_age_range = model.config.id2label[predicted_class]

    # Parse the age range string (e.g., "20-29")
    age_min, age_max = map(int, predicted_age_range.split('-'))
    print(f"age_mi: {age_min}, age_max: {age_max}")

    # Verify against the DataFrame
    matching_row = updated_patient_df[
        (updated_patient_df["First_Name"] == selected_patient["First_Name"]) &
        (updated_patient_df["Last_Name"] == selected_patient["Last_Name"]) &
        (updated_patient_df["Patient_ID"] == selected_patient["Patient_ID"]) &
        (updated_patient_df["Gender"].str.lower() == predicted_gender) &
        (updated_patient_df["Age"].between(age_min, age_max))
    ]
    print(f"matching_row {matching_row} ")
    if not matching_row.empty:
        patient_verification = f'''Verification successful.
                        Patient is : {selected_patient["First_Name"]} {selected_patient["Last_Name"]}
                        with ID {selected_patient["Patient_ID"]}
                        which is {predicted_gender} in age range of {predicted_age_range} can proceed to the physician.'''
    else:
      patient_verification = "ID not verified. Patient cannot proceed."
    return patient_verification

#------------------- Define Agents-----------------------------

class AgentState(TypedDict):
    initial_prompt : str
    messages: Annotated[List[BaseMessage], operator.add]
    patient_verification : str

def front_desk_agent(state, image_Path, selected_patient_data,  updated_patient_df):
    initial_prompt = state["initial_prompt"]
    # Call function
    patient_verification =  patient_verification_tool(image_Path, selected_patient_data,  updated_patient_df)
    print(patient_verification)
    return {"patient_verification": patient_verification}

#-----------------------------------------------------------------
#               Build the LangGraph for Hospital Front Desk     #
#-----------------------------------------------------------------

image_Path = "female.jpg"
selected_patient_data = selected_patient.to_dict()
updated_patient_df


front_desk_agent_node = functools.partial(front_desk_agent,
                                          image_Path = image_Path,
                                          selected_patient_data=selected_patient_data,
                                          updated_patient_df =updated_patient_df)

# 6. Set up the Langgraph state graph
FrontDeskGraph = StateGraph(AgentState)

# Define nodes for workflow
FrontDeskGraph.add_node("front_desk_agent", front_desk_agent_node)
FrontDeskGraph.add_edge(START, "front_desk_agent")
FrontDeskGraph.add_edge("front_desk_agent", END)


# Initialize memory to persist state between graph runs
FrontDeskWorkflow = FrontDeskGraph.compile()

from IPython.display import Markdown, display, Image
display(Image(FrontDeskWorkflow.get_graph(xray=True).draw_mermaid_png()))





initial_prompt = "You are Front Desk Administrator in an Hospital in the Netherlands. Start Verification of the following Patient:"


# Run the workflow
inputs = {"initial_prompt" : initial_prompt
          }
output = FrontDeskWorkflow.invoke(inputs)
output





display(Markdown(output['patient_verification']))


# ## 3.2 Pysician Agent




def question_patient_symptoms(selected_patient_data) -> str:
    """Asks the patient about symptoms, generates responses, and summarizes the answers based on patient data."""
    symptoms_questions = {
        "Cough": "\nAre you coughing?\n",
        "Fatigue": "\nDo you feel fatigue?\n",
        "\nDifficulty Breathing": "Do you have difficulty breathing?\n"
    }

    conversation = []

    for symptom, question in symptoms_questions.items():
        conversation.append(f"\nPhysician: {question}")
        response = selected_patient_data.get(symptom, "No")
        answer = "Yes" if response == "Yes" else "No"
        conversation.append(f"\nPatient: {answer}")

    first_name = selected_patient_data.get("First_Name", "")
    last_name = selected_patient_data.get("Last_Name", "")
    patient_id = selected_patient_data.get("Patient_ID", "")
    gender = selected_patient_data.get("Gender", "")
    age = selected_patient_data.get("Age", "")

    profile = f"\nYou are {first_name} {last_name}, a {age} years old {gender} with Patient ID: {patient_id}."
    summary = profile +"I gathered that you are experiencing the following: "
    summaries = []
    for symptom in symptoms_questions.keys():
        response = selected_patient_data.get(symptom, "No")
        if response == "Yes":
            summaries.append(f"you are experiencing {symptom.lower()}")
        else:
            summaries.append(f"\nI am glad you are not experiencing {symptom.lower()}")
    summary += "; ".join(summaries) + "."

    conversation.append(f"\nPhysician: {summary}")

    return "\n".join(conversation)

def perform_examination(selected_patient_data) -> str:
    """Performs examination by reporting fever, blood pressure, and cholesterol level from patient data."""
    fever = selected_patient_data.get("Fever", "Unknown")
    blood_pressure = selected_patient_data.get("Blood Pressure", "Unknown")
    cholesterol = selected_patient_data.get("Cholesterol Level", "Unknown")
    return f"Examination Results: Fever - {fever}, Blood Pressure - {blood_pressure}, Cholesterol Level - {cholesterol}"

def diagnose_patient(selected_patient_data) -> str:
    """Provides diagnosis based on Disease and Outcome columns in patient data."""
    disease = selected_patient_data.get("Disease", "Unknown Disease")
    outcome = selected_patient_data.get("Outcome Variable", "Unknown Outcome")
    if outcome == 'Positive':
      diagnosis = 'Make X-Ray from Chest'
    else:
      diagnosis = 'Rest to Recover'
    return f"Diagnosis: {disease}. Test Result: {outcome}. Final Diagnosis: {diagnosis}", diagnosis


class AgentState(TypedDict):
    initial_prompt : str
    messages: Annotated[List[BaseMessage], operator.add]
    question_patient_symptoms: str
    examination_patient: str
    diagnosis_patient: str
    diagnosis : str


def physician_agent(state, selected_patient_data):
    question_patient= question_patient_symptoms(selected_patient_data)
    examination = perform_examination(selected_patient_data)
    diagnosis_report, diagnosis  = diagnose_patient(selected_patient_data)
    return {"question_patient_symptoms": question_patient,
            "examination_patient": examination,
            "diagnosis_patient": diagnosis_report,
            "diagnosis": diagnosis}


selected_patient_data = selected_patient.to_dict()

physician_agent_node = functools.partial(physician_agent,
                                          selected_patient_data=selected_patient_data)


# 6. Set up the Langgraph state graph
PhysicianGraph = StateGraph(AgentState)

# Define nodes for workflow
PhysicianGraph.add_node("physician_agent", physician_agent_node)
PhysicianGraph.add_edge(START, "physician_agent")
PhysicianGraph.add_edge("physician_agent", END)


# Initialize memory to persist state between graph runs
PhysicianWorkflow = PhysicianGraph.compile()

display(Image(PhysicianWorkflow.get_graph(xray=True).draw_mermaid_png()))





initial_prompt = "You are a Very Experience Doctor in an Hospital in the Netherlands. Start a conversation with the patient and determine \
                   symptoms and give diagnosis"


# Run the workflow
inputs = {"initial_prompt" : initial_prompt
          }
output = PhysicianWorkflow.invoke(inputs)
output





display(Markdown(output['question_patient_symptoms']))
display(Markdown(output['examination_patient']))
display(Markdown(output['diagnosis_patient']))


# ## 3.3 Radiologist




def examine_X_ray_image(patient_x_ray_path) -> str:
    """Use Vision Models to recognise if the X-Ray Image of Patient is NORMAL or PNEUMONIA"""
    # Model in Hugging Face: https://huggingface.co/lxyuan/vit-xray-pneumonia-classification
    # vit-xray-pneumonia-classification
    x_ray_ds = load_dataset(patient_x_ray_path, name="full")
    random_index = random.randint(0, x_ray_ds['train'].shape[0] - 1)
    patient_x_ray_image  = x_ray_ds['train'][random_index]['image']
    classifier = pipeline(model="lxyuan/vit-xray-pneumonia-classification")
    patient_x_ray_results = classifier(patient_x_ray_image)

    # Find the label with the highest score and its score
    highest = max(patient_x_ray_results, key=lambda x: x['score'])
    highest_score_label = highest['label']
    highest_score = highest['score'] * 100  # Convert to percentage

    # Choose the correct verb based on the label
    verb = "is" if highest_score_label == "NORMAL" else "has"

    return f"Patient {verb} {highest_score_label} with Probability of ca. {highest_score:.0f}%"

class AgentState(TypedDict):
    initial_prompt : str
    messages: Annotated[List[BaseMessage], operator.add]
    pneumonia_detection: str



def radiologist_agent(state, patient_x_ray_path):
    pneumonia_detection = examine_X_ray_image(patient_x_ray_path)
    return {"pneumonia_detection": pneumonia_detection}

patient_x_ray_path = "keremberke/chest-xray-classification"

radiologist_agent_node = functools.partial(radiologist_agent,
                                          patient_x_ray_path=patient_x_ray_path)

# 6. Set up the Langgraph state graph
RadiologistGraph = StateGraph(AgentState)

# Define nodes for workflow
RadiologistGraph.add_node("radiologist_agent", radiologist_agent_node)
RadiologistGraph.add_edge(START, "radiologist_agent")
RadiologistGraph.add_edge("radiologist_agent", END)

# Initialize memory to persist state between graph runs
RadiologistWorkflow = RadiologistGraph.compile()

display(Image(RadiologistWorkflow.get_graph(xray=True).draw_mermaid_png()))



initial_prompt = "You are a Very Experienced Radiologist in an Hospital in the Netherlands. Diagnose if the patient has pneumonia"


# Run the workflow
inputs = {"initial_prompt" : initial_prompt
          }
output = RadiologistWorkflow.invoke(inputs)
output



display(Markdown(output['pneumonia_detection']))


# # Step 4: Putting All Agents in One Graph




from langchain_core.output_parsers import StrOutputParser, JsonOutputParser
from langchain_core.prompts import ChatPromptTemplate

selected_patient_data = selected_patient.to_dict()
image_Path = "female.jpg"
patient_x_ray_image = patient_x_ray

def patient_verification_tool(image_Path, selected_patient_data,  updated_patient_df) -> str:
    """Detects the gender from an image provided as a file path."""
    from PIL import Image
    print(image_Path)
    model = AutoModelForImageClassification.from_pretrained("rizvandwiki/gender-classification")
    processor = AutoProcessor.from_pretrained("rizvandwiki/gender-classification")
    image = Image.open(image_Path)
    inputs = processor(images=image, return_tensors="pt")
    outputs = model(**inputs)
    predicted_class = outputs.logits.argmax(-1).item()
    print(f"Predicted Gender Of Patient is : {model.config.id2label[predicted_class]}")
    predicted_gender = model.config.id2label[predicted_class]

    from PIL import Image
    model = AutoModelForImageClassification.from_pretrained("nateraw/vit-age-classifier")
    processor = AutoProcessor.from_pretrained("nateraw/vit-age-classifier")
    image = Image.open(image_Path)
    inputs = processor(images=image, return_tensors="pt")
    outputs = model(**inputs)
    predicted_class = outputs.logits.argmax(-1).item()
    print(f"predicted Age Class: {model.config.id2label[predicted_class]}")
    predicted_age_range = model.config.id2label[predicted_class]

    # Parse the age range string (e.g., "20-29")
    age_min, age_max = map(int, predicted_age_range.split('-'))
    print(f"age_mi: {age_min}, age_max: {age_max}")

    # Verify against the DataFrame
    matching_row = updated_patient_df[
        (updated_patient_df["First_Name"] == selected_patient["First_Name"]) &
        (updated_patient_df["Last_Name"] == selected_patient["Last_Name"]) &
        (updated_patient_df["Patient_ID"] == selected_patient["Patient_ID"]) &
        (updated_patient_df["Gender"].str.lower() == predicted_gender) &
        (updated_patient_df["Age"].between(age_min, age_max))
    ]
    print(f"matching_row {matching_row} ")
    if not matching_row.empty:
        patient_verification = f'''Verification successful.
                        Patient is : {selected_patient["First_Name"]} {selected_patient["Last_Name"]}
                        with ID {selected_patient["Patient_ID"]}
                        which is {predicted_gender} in age range of {predicted_age_range} can proceed to the physician.'''
    else:
      patient_verification = "ID not verified. Patient cannot proceed."
    return patient_verification

def question_patient_symptoms(selected_patient_data) -> str:
    """Asks the patient about symptoms, generates responses, and summarizes the answers based on patient data."""
    symptoms_questions = {
        "Cough": "\nAre you coughing?\n",
        "Fatigue": "\nDo you feel fatigue?\n",
        "\nDifficulty Breathing": "Do you have difficulty breathing?\n"
    }

    conversation = []

    for symptom, question in symptoms_questions.items():
        conversation.append(f"\nPhysician: {question}")
        response = selected_patient_data.get(symptom, "No")
        answer = "Yes" if response == "Yes" else "No"
        conversation.append(f"\nPatient: {answer}")

    first_name = selected_patient_data.get("First_Name", "")
    last_name = selected_patient_data.get("Last_Name", "")
    patient_id = selected_patient_data.get("Patient_ID", "")
    gender = selected_patient_data.get("Gender", "")
    age = selected_patient_data.get("Age", "")

    profile = f"\nYou are {first_name} {last_name}, a {age} years old {gender} with Patient ID: {patient_id}."
    summary = profile +"I gathered that you are experiencing the following: "
    summaries = []
    for symptom in symptoms_questions.keys():
        response = selected_patient_data.get(symptom, "No")
        if response == "Yes":
            summaries.append(f"you are experiencing {symptom.lower()}")
        else:
            summaries.append(f"\nI am glad you are not experiencing {symptom.lower()}")
    summary += "; ".join(summaries) + "."

    conversation.append(f"\nPhysician: {summary}")

    return "\n".join(conversation)

def perform_examination(selected_patient_data) -> str:
    """Performs examination by reporting fever, blood pressure, and cholesterol level from patient data."""
    fever = selected_patient_data.get("Fever", "Unknown")
    blood_pressure = selected_patient_data.get("Blood Pressure", "Unknown")
    cholesterol = selected_patient_data.get("Cholesterol Level", "Unknown")
    return f"Examination Results: Fever - {fever}, Blood Pressure - {blood_pressure}, Cholesterol Level - {cholesterol}"

def diagnose_patient(selected_patient_data) -> str:
    """Provides diagnosis based on Disease and Outcome columns in patient data."""
    disease = selected_patient_data.get("Disease", "Unknown Disease")
    outcome = selected_patient_data.get("Outcome Variable", "Unknown Outcome")
    if outcome == 'Positive':
      diagnosis = 'Make X-Ray from Chest'
    else:
      diagnosis = 'Rest to Recover'
    return f"Diagnosis: {disease}. Test Result: {outcome}. Final Diagnosis: {diagnosis}", diagnosis

def examine_X_ray_image(patient_x_ray_path) -> str:
    """Use Vision Models to recognise if the X-Ray Image of Patient is NORMAL or PNEUMONIA"""
    # Model in Hugging Face: https://huggingface.co/lxyuan/vit-xray-pneumonia-classification
    # vit-xray-pneumonia-classification
    x_ray_ds = load_dataset(patient_x_ray_path, name="full")
    random_index = random.randint(0, x_ray_ds['train'].shape[0] - 1)
    patient_x_ray_image  = x_ray_ds['train'][random_index]['image']
    classifier = pipeline(model="lxyuan/vit-xray-pneumonia-classification")
    patient_x_ray_results = classifier(patient_x_ray_image)

    # Find the label with the highest score and its score
    highest = max(patient_x_ray_results, key=lambda x: x['score'])
    highest_score_label = highest['label']
    highest_score = highest['score'] * 100  # Convert to percentage

    # Choose the correct verb based on the label
    verb = "is" if highest_score_label == "NORMAL" else "has"

    return f"Patient {verb} {highest_score_label} with Probability of ca. {highest_score:.0f}%"

# The agent state is the input to each node in the graph
class AgentState(TypedDict):
    # The annotation tells the graph that new messages will always
    # be added to the current states
    initial_prompt : str
    messages: Annotated[List[BaseMessage], operator.add]
    patient_verification : str
    question_patient_symptoms: str
    examination_patient: str
    diagnosis_patient: str
    diagnosis : str
    pneumonia_detection: str

def front_desk_agent(state, image_Path, selected_patient_data,  updated_patient_df):
    initial_prompt = state["initial_prompt"]
    patient_verification =  patient_verification_tool(image_Path, selected_patient_data,  updated_patient_df)
    print(patient_verification)
    return {"patient_verification": patient_verification}

def physician_agent(state, selected_patient_data):
    question_patient= question_patient_symptoms(selected_patient_data)
    examination = perform_examination(selected_patient_data)
    diagnosis_report, diagnosis  = diagnose_patient(selected_patient_data)
    pneumonia_detection = examine_X_ray_image(patient_x_ray_path)
    return {"question_patient_symptoms": question_patient,
            "examination_patient": examination,
            "diagnosis_patient": diagnosis_report,
            "diagnosis": diagnosis}

def radiologist_agent(state, patient_x_ray_path):
    pneumonia_detection = examine_X_ray_image(patient_x_ray_path)
    return {"pneumonia_detection": pneumonia_detection}

def decide_on_radiologist(state):
  if state["diagnosis"] == 'Make X-Ray from Chest':
    return 'radiologist'
  else:
    return ''


image_Path = "female.jpg"
selected_patient_data = selected_patient.to_dict()
updated_patient_df
patient_x_ray_path = "keremberke/chest-xray-classification"

front_desk_agent_node = functools.partial(front_desk_agent,
                                          image_Path = image_Path,
                                          selected_patient_data=selected_patient_data,
                                          updated_patient_df =updated_patient_df)
physician_agent_node = functools.partial(physician_agent,
                                          selected_patient_data=selected_patient_data)

radiologist_agent_node = functools.partial(radiologist_agent,
                                          patient_x_ray_path=patient_x_ray_path)

def decide_on_radiologist(state):
  if state["diagnosis"] == 'Make X-Ray from Chest':
    return 'radiologist'
  else:
    return 'end'

# 6. Set up the Langgraph state graph
HospitalGraph = StateGraph(AgentState)

# Define nodes for workflow
HospitalGraph.add_node("front_desk_agent", front_desk_agent_node)
HospitalGraph.add_node("physician_agent", physician_agent_node)
HospitalGraph.add_node("radiologist_agent", radiologist_agent_node)

HospitalGraph.add_edge(START, "front_desk_agent")
HospitalGraph.add_edge("front_desk_agent", "physician_agent")
HospitalGraph.add_conditional_edges("physician_agent",
                                    decide_on_radiologist,
                                    {'radiologist': "radiologist_agent",
                                     'end': END})


# Initialize memory to persist state between graph runs
HospitalWorkflow = HospitalGraph.compile()

display(Image(HospitalWorkflow.get_graph(xray=True).draw_mermaid_png()))





initial_prompt = "Start with the following Patient"


# Run the workflow
inputs = {"initial_prompt" : initial_prompt
          }
output = HospitalWorkflow.invoke(inputs)
output




display(Markdown(output['patient_verification']))





display(Markdown(output['question_patient_symptoms']))
display(Markdown(output['examination_patient']))
display(Markdown(output['diagnosis_patient']))




display(Markdown(output['pneumonia_detection']))


# # Step 5: Gradio Dashboard

# ## 5.1 Build the Hospital Dashboard APP

# In[69]:


x_ray_image_path = 'x-ray-chest.png'

import gradio as gr
info = (
        f"**First Name:** {selected_patient_data['First_Name']}\n\n"
        f"**Last Name:** {selected_patient_data['Last_Name']}\n\n"
        f"**Patient ID:** {selected_patient_data['Patient_ID']}"
    )

def verify_age_gender():
    """
    Function to verify age and gender.
    """
    # Placeholder logic: In a real scenario, perform necessary checks or computations
    initial_prompt = "You are Front Desk Administrator in an Hospital in the Netherlands. Start Verification of the following Patient:"
    inputs = {"initial_prompt" : initial_prompt
          }
    output = FrontDeskWorkflow.invoke(inputs)
    verification_message = 'โœ… ' + output['patient_verification']
    return verification_message, gr.update(visible=True)

def physician_examination():
    initial_prompt = "You are a Very Experience Doctor in an Hospital in the Netherlands. Start a conversation with the patient and determine \
                   symptoms and give diagnosis"
    # Run the workflow
    inputs = {"initial_prompt" : initial_prompt
          }
    output = PhysicianWorkflow.invoke(inputs)
    output_all = f''' ๐Ÿฉบ {output['question_patient_symptoms']}\n
                     ๐Ÿ’“ {output['examination_patient']}\n
                     ๐ŸŒฌ๏ธ {output['diagnosis_patient']}'''
    return output_all, gr.update(visible=True)

def pneumonia_detection():
    initial_prompt = "You are a Very Experienced Radiologist in an Hospital in the Netherlands. Diagnose if the patient has pneumonia"
    inputs = {"initial_prompt" : initial_prompt
          }
    output = RadiologistWorkflow.invoke(inputs)
    pneumonia_detection = 'From X-Ray Image ๐Ÿ–ผ๏ธ ' + output['pneumonia_detection']
    return pneumonia_detection

def take_xray_image():

    return gr.update(visible=True), gr.update(visible=True)

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown(info)
            # Add a Button below the Markdown
            verify_button = gr.Button("Verify Age and Gender")
            # Add an output component to display verification status
            verification_output = gr.Textbox(label="Verification Status", interactive=False, lines=5, max_lines=None)
            # Add a Button below the Markdown
            physician_button = gr.Button("Get Examination at Physician", visible=False)
            physician_output = gr.Textbox(label="Examination by Physician Placeholder", interactive=False, lines=35, max_lines=None)
            x_ray_button = gr.Button("Take Chest X-Ray Image", visible=False)
            # Display X-Ray Image (Initially Hidden)
            xray_image_display = gr.Image(value=x_ray_image_path, label="X-Ray Image", visible=False)
            radiologist_button = gr.Button("Go to Radiologist", visible=False)
            # Add an output component to display verification status
            radiologist_output = gr.Textbox(label="Radiologist Placeholder", interactive=False, lines=5, max_lines=None)

        with gr.Column(scale=1):
            gr.Image(value=image_Path, label="Static Image", show_label=True)

    # Define the button's action: When clicked, call verify_age_gender and display the result
    verify_button.click(fn=verify_age_gender, inputs=None, outputs=[verification_output, physician_button])
    physician_button.click(fn=physician_examination, inputs=None, outputs=[physician_output, x_ray_button])
    x_ray_button.click(fn=take_xray_image, inputs=None, outputs=[xray_image_display, radiologist_button])
    radiologist_button.click(fn=pneumonia_detection, inputs=None, outputs=[radiologist_output])


# ## 5.2 Run the App



# Launch the app
#demo.launch(share=True, debug=False) 
#demo.launch(share=True, debug=False, allowed_paths=[dataDir], ssr_mode=False)
demo.launch(share=True, debug=False, ssr_mode=False)