DrSyedFaizan commited on
Commit
0552035
·
verified ·
1 Parent(s): 7dc2485

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,9 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ data/docs/FIRST[[:space:]]AID[[:space:]]IN[[:space:]]COMMON[[:space:]]EMERGENCY[[:space:]]CONDITIONS.pdf filter=lfs diff=lfs merge=lfs -text
37
+ data/docs_2/FIRST[[:space:]]AID[[:space:]]IN[[:space:]]COMMON[[:space:]]EMERGENCY[[:space:]]CONDITIONS.pdf filter=lfs diff=lfs merge=lfs -text
38
+ data/vectordb/processed/chroma/chroma.sqlite3 filter=lfs diff=lfs merge=lfs -text
39
+ images/AI_RT.png filter=lfs diff=lfs merge=lfs -text
40
+ images/RAGGPT[[:space:]]UI.png filter=lfs diff=lfs merge=lfs -text
41
+ src/FIRST[[:space:]]AID[[:space:]]IN[[:space:]]COMMON[[:space:]]EMERGENCY[[:space:]]CONDITIONS.pdf filter=lfs diff=lfs merge=lfs -text
Post_Test_Quiz_Updated.pdf ADDED
Binary file (3.3 kB). View file
 
Pre_Test_Quiz_Updated.pdf ADDED
Binary file (3.16 kB). View file
 
data/docs/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf CHANGED
Binary files a/data/docs/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf and b/data/docs/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf differ
 
data/docs_2/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf CHANGED
Binary files a/data/docs_2/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf and b/data/docs_2/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf differ
 
data/vectordb/processed/chroma/chroma.sqlite3 CHANGED
Binary files a/data/vectordb/processed/chroma/chroma.sqlite3 and b/data/vectordb/processed/chroma/chroma.sqlite3 differ
 
images/AI_RT.png CHANGED

Git LFS Details

  • SHA256: 667977223681f24e3c45e89eb64a4e480e0d2796aec0ddaa4ea80ef1190261c5
  • Pointer size: 131 Bytes
  • Size of remote file: 835 kB
images/RAGGPT UI.png CHANGED

Git LFS Details

  • SHA256: 6c64f65123ed37104ff5fecc2eb82c74d5ef845750c29efd92a599f564f145b0
  • Pointer size: 131 Bytes
  • Size of remote file: 124 kB
src/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf CHANGED
Binary files a/src/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf and b/src/FIRST AID IN COMMON EMERGENCY CONDITIONS.pdf differ
 
src/eval.py CHANGED
@@ -1,36 +1,27 @@
1
  import os
2
  from dotenv import load_dotenv
3
- import openai
4
  from gradio_client import Client
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Load API Key from .env file
7
  load_dotenv()
8
  api_key = os.getenv("OPENAI_API_KEY")
9
- print(f"Using OpenAI API Key: {api_key[:5]}****{api_key[-3:]}")
10
- openai.api_key = api_key
11
-
12
- # ---- STEP 1: Load First Aid Contextual Data ----
13
- from langchain_community.document_loaders import ArxivLoader
14
- from langchain.text_splitter import RecursiveCharacterTextSplitter
15
- from langchain_openai import OpenAIEmbeddings
16
- from langchain_community.vectorstores import Chroma
17
- from langchain.prompts import ChatPromptTemplate
18
- from langchain_openai import ChatOpenAI
19
- import wandb
20
- import pandas as pd
21
-
22
- # Load medical and first aid papers from ArXiv
23
- first_aid_docs = ArxivLoader(query="first aid treatment", load_max_docs=5).load()
24
-
25
- # Split documents for indexing
26
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=250)
27
- docs = text_splitter.split_documents(first_aid_docs)
28
 
29
- # Create vectorstore
30
- vectorstore = Chroma.from_documents(docs, OpenAIEmbeddings())
31
- retriever = vectorstore.as_retriever(search_kwargs={"k": 2})
32
 
33
- # ---- Define First Aid Questions ----
34
  questions = [
35
  "What are the first aid measures for high fever in infants?",
36
  "What are the signs and symptoms of low blood sugar?",
@@ -44,85 +35,46 @@ questions = [
44
  "What are the First aid measures for taking care of a patient with insect stings and animal bites?"
45
  ]
46
 
47
- # ---- STEP 2: Generate Ground Truth Responses using ChatGPT ----
48
- llm = ChatOpenAI(model_name="gpt-4", temperature=0)
49
- prompt_template = """
50
- Generate a detailed and accurate first-aid response based on the given context.
51
-
52
- ### CONTEXT
53
- {context}
54
-
55
- ### QUESTION
56
- {question}
57
-
58
- ### RESPONSE
59
- """
60
- prompt = ChatPromptTemplate.from_template(prompt_template)
61
-
62
- ground_truth_responses = []
63
- for question in questions:
64
- retrieved_docs = retriever.invoke(question)
65
- context_text = "\n".join([doc.page_content for doc in retrieved_docs])
66
- generated_response = llm.invoke(prompt.format(context=context_text, question=question))
67
- ground_truth_responses.append(str(generated_response))
68
-
69
- # ---- STEP 3: Fetch Responses from Deployed Chatbot ----
70
- print("\n===== Fetching Responses from Chatbot =====")
71
 
72
- client = Client("DrSyedFaizan/First_Aid_Assistant")
73
- responses = []
 
74
 
75
  for question in questions:
76
  try:
77
  result = client.predict(chatbot=[], message=question, api_name="/respond")
78
- chat_history = result[1]
79
- chatbot_response = next((entry["content"] for entry in chat_history if entry["role"] == "assistant"), "[NO RESPONSE]")
80
  except Exception as e:
81
- chatbot_response = f"[ERROR: {e}]"
82
 
83
- responses.append(str(chatbot_response))
84
-
85
- # Save bot responses to a text file
86
- with open("bot_responses.txt", "w", encoding="utf-8") as f:
87
- for q, r in zip(questions, responses):
88
- f.write(f"Q: {q}\nA: {r}\n\n")
89
 
90
- # Print chatbot responses for debugging
91
- for q, r in zip(questions, responses):
92
- print(f"Q: {q}\nA: {r}\n")
93
-
94
- # ---- STEP 5: Evaluate Using RAGAS ----
95
- from datasets import Dataset
96
- import pandas as pd
97
- from tqdm import tqdm
98
- from ragas import evaluate
99
- from ragas.metrics import (
100
- answer_relevancy,
101
- faithfulness,
102
- context_recall,
103
- answer_correctness,
104
- answer_similarity
105
- )
106
 
107
- def create_ragas_dataset(eval_dataset):
108
- """Convert dataset to RAGAS format."""
109
- df = eval_dataset.to_pandas()
110
- rag_dataset = []
111
- for _, row in df.iterrows():
112
- rag_dataset.append(
113
- {
114
- "question": row["question"],
115
- "answer": row["answer"],
116
- "contexts": ["First aid medical references"],
117
- "ground_truths": [row["ground_truth"]],
118
- "reference": row["context"]
119
- }
120
- )
121
- rag_df = pd.DataFrame(rag_dataset)
122
- return Dataset.from_pandas(rag_df)
123
 
 
124
  def evaluate_ragas_dataset(ragas_dataset):
125
- """Run RAGAS evaluation with proper handling of required_columns."""
126
  try:
127
  result = evaluate(
128
  ragas_dataset,
@@ -139,48 +91,15 @@ def evaluate_ragas_dataset(ragas_dataset):
139
  print("⚠️ RAGAS Error:", e)
140
  raise e
141
 
142
- # Create ground truth dataset
143
- ground_truth_qac_set = pd.DataFrame({
144
- "question": questions,
145
- "answer": responses,
146
- "context": ["First aid medical references"] * len(questions),
147
- "ground_truth": [str(response) for response in ground_truth_responses],
148
- "reference": ["First aid medical references"] * len(questions)
149
- })
150
-
151
- eval_dataset = Dataset.from_pandas(ground_truth_qac_set.astype(str))
152
-
153
- # Save evaluation datasets
154
- eval_dataset.to_csv("groundtruth_eval_dataset.csv")
155
- basic_qa_ragas_dataset = create_ragas_dataset(eval_dataset)
156
- basic_qa_ragas_dataset.to_csv("basic_qa_ragas_dataset.csv")
157
-
158
- # Run evaluation
159
  basic_qa_result = evaluate_ragas_dataset(basic_qa_ragas_dataset)
160
 
161
- print("\n===== Evaluation Results =====")
162
- print(basic_qa_result)
163
-
164
  evaluation_results = basic_qa_result.to_pandas()
 
165
 
166
- # Save evaluation results as log
167
-
168
- # ---- STEP 6: Log Results to WandB ----
169
-
170
- import wandb
171
- import pandas as pd
172
-
173
- # ✅ Convert `eval_dataset` (Dataset) to Pandas DataFrame
174
- eval_df = eval_dataset.to_pandas()
175
-
176
- # ✅ Convert `basic_qa_ragas_dataset` (Dataset) to Pandas DataFrame
177
- ragas_df = basic_qa_ragas_dataset.to_pandas()
178
-
179
- # ✅ Save DataFrames as CSV
180
- eval_df.to_csv("groundtruth_eval_dataset.csv", index=False)
181
- ragas_df.to_csv("basic_qa_ragas_dataset.csv", index=False)
182
-
183
- # ✅ Initialize WandB
184
  wandb.init(
185
  project="first-aid-tutor",
186
  entity="drsyedfaizan1987-northeastern-university",
@@ -189,10 +108,7 @@ wandb.init(
189
  tags=["first-aid", "evaluation", "ragas"]
190
  )
191
 
192
- # ✅ Log DataFrames to WandB as Tables
193
- wandb.log({"basic_qa_ragas_dataset": wandb.Table(dataframe=evaluation_results)})
194
- wandb.log({"groundtruth_eval_dataset": wandb.Table(dataframe=eval_df)})
195
- wandb.log({"basic_qa_ragas_dataset": wandb.Table(dataframe=ragas_df)})
196
-
197
- # ✅ Finish WandB run
198
  wandb.finish()
 
 
 
1
  import os
2
  from dotenv import load_dotenv
 
3
  from gradio_client import Client
4
+ from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
5
+ import pandas as pd
6
+ from datasets import Dataset
7
+ from ragas import evaluate
8
+ from ragas.metrics import (
9
+ answer_relevancy,
10
+ faithfulness,
11
+ context_recall,
12
+ answer_correctness,
13
+ answer_similarity
14
+ )
15
+ import wandb
16
 
17
  # Load API Key from .env file
18
  load_dotenv()
19
  api_key = os.getenv("OPENAI_API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ # Initialize Gradio Client
22
+ client = Client("DrSyedFaizan/First_Aid_Assistant")
 
23
 
24
+ # Questions and Ground Truth Answers
25
  questions = [
26
  "What are the first aid measures for high fever in infants?",
27
  "What are the signs and symptoms of low blood sugar?",
 
35
  "What are the First aid measures for taking care of a patient with insect stings and animal bites?"
36
  ]
37
 
38
+ ground_truth_responses = [
39
+ "For infants aged 0-3 months with a rectal temperature of 100.4oF (38oC) or higher, refer to the doctor even if the child does not have any other signs or symptoms. For infants aged 3-6 months with a rectal temperature up to 102oF (38.9oC), encourage the child to rest and drink plenty of fluids without the need for medication. If the child seems unusually irritable, lethargic, or uncomfortable, refer to the doctor.",
40
+ "Irritability, confusion, tremors, blurring of vision, coma, seizures, tachycardia, hypotension, and cold and clammy skin are signs and symptoms of low blood sugar.",
41
+ "RICE stands for: R: Rest the injured part, I: Ice the area, C: Compress with a bandage, E: Elevate the injured part to divert the blood flow away from the area",
42
+ "Control bleeding with direct pressure, cover wound with sterile dressing or clean cloth, diaper, or sanitary napkin, place gloved hand over dressing, press firmly, continue pressure until bleeding stops. DO NOT remove dressing. If soaked through, add more material and continue pressure. If bleeding does not slow after 5 minutes of direct pressure, have victim lie down, continue direct pressure on wound, and apply pressure to pressure point between wound and heart.",
43
+ "Douse the burnt area with cool water within 10 minutes of injury. This may halt burn process in the tissues which may minimise burn depth and reduce pain considerably. Drink plenty of water and remove jewellery from the burned area.",
44
+ "Signs and symptoms of a stroke include a sudden feeling of weakness or numbness of the face, arm, or leg on one side of the body, loss of vision or dimming in one or both eyes, loss of speech, difficulty in talking or understanding what others are saying, and a sudden, severe headache with no known cause.",
45
+ "The treatment of a snake bite includes: a) Lay the patient down. b) Give him complete rest. c) Calm and reassure him. d) Do not make him walk. e) Tie immediately a piece of cloth or a tourniquet, tightly above the bite to prevent the venous blood return. It should be loosened for a few seconds at a regular interval of about 10 minutes. f) Wash cuts gently with normal saline or antiseptic lotion if available otherwise with soapy water. Apply a clean dressing. Immobilize the affected limb. Apply Ice packs on the wound. Shift the patient to the hospital immediately. Take the killed snake, if available for identification.",
46
+ "To provide first aid for choking in an adult: Stand behind the victim and wrap your arms around their waist. Grasp your fist with your other hand and place your thumb against the abdomen slightly above the navel and below the rib cage. Perform a quick upward thrust on the abdomen with your fist. Repeat this several times until the foreign body is expelled.",
47
+ "Immediate steps to treat a fainting patient include making the person lie flat for at least 15 minutes or sit if lightheadedness is present, opening the airway and assessing for breathing, raising the person's legs above the level of their heart if breathing, loosening any restrictive clothing around the neck or waist, checking for breathing and heart rate if the person remains unconscious, and calling an ambulance for immediate assessment and treatment if needed.",
48
+ "The first aid measures for taking care of a patient with insect stings are: 1) Apply pressure with a clean bandage or towel to stop bleeding. 2) Clean and scrap the area to remove carefully the wings of the insect. 3) Give immediately a dose of tetanus toxide. 4) Apply antihistamine ointment to reduce itching, swelling, and pain."
49
+ ]
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
+ # Fetch Responses from the Chatbot
52
+ print("\nFetching Responses from the Chatbot...\n")
53
+ bot_responses = []
54
 
55
  for question in questions:
56
  try:
57
  result = client.predict(chatbot=[], message=question, api_name="/respond")
58
+ chat_history = result[1]
59
+ response = next((entry["content"] for entry in chat_history if entry["role"] == "assistant"), "[NO RESPONSE]")
60
  except Exception as e:
61
+ response = f"[ERROR: {e}]"
62
 
63
+ bot_responses.append(response)
 
 
 
 
 
64
 
65
+ # ---- Create Dataset for RAGAS ----
66
+ ground_truth_qac_set = pd.DataFrame({
67
+ "question": questions,
68
+ "answer": bot_responses,
69
+ "context": ["First aid medical references"] * len(questions),
70
+ "ground_truth": ground_truth_responses,
71
+ "reference": ["First aid medical references"] * len(questions)
72
+ })
 
 
 
 
 
 
 
 
73
 
74
+ eval_dataset = Dataset.from_pandas(ground_truth_qac_set.astype(str))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ # ---- RAGAS Evaluation ----
77
  def evaluate_ragas_dataset(ragas_dataset):
 
78
  try:
79
  result = evaluate(
80
  ragas_dataset,
 
91
  print("⚠️ RAGAS Error:", e)
92
  raise e
93
 
94
+ # Run Evaluation
95
+ basic_qa_ragas_dataset = eval_dataset
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  basic_qa_result = evaluate_ragas_dataset(basic_qa_ragas_dataset)
97
 
98
+ # Save Evaluation Results
 
 
99
  evaluation_results = basic_qa_result.to_pandas()
100
+ evaluation_results.to_html("ragas_evaluation_results.html", index=False)
101
 
102
+ # ---- Log Results to WandB ----
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  wandb.init(
104
  project="first-aid-tutor",
105
  entity="drsyedfaizan1987-northeastern-university",
 
108
  tags=["first-aid", "evaluation", "ragas"]
109
  )
110
 
111
+ wandb.log({"ragas_evaluation_results": wandb.Table(dataframe=evaluation_results)})
 
 
 
 
 
112
  wandb.finish()
113
+
114
+ print("\n===== Evaluation Results Saved to 'ragas_evaluation_results.html' =====")