File size: 2,135 Bytes
c7a6fe6 | 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 | import dspy
import json
from typing import Literal
# --- 1. LLM Configuration (OpenAI Only) ---
api_file = "/home/mshahidul/api_new.json"
with open(api_file, "r") as f:
api_keys = json.load(f)
# Configure OpenAI for Inference
# Note: Use 'gpt-4o' or 'gpt-4-turbo' as 'gpt-5' is not a standard identifier yet.
openai_model = dspy.LM(model='gpt-5-mini', api_key=api_keys["openai"])
dspy.configure(lm=openai_model)
# --- 2. Program Architecture (Must match your training structure) ---
class HealthLiteracySignature(dspy.Signature):
"""
Judge the health literacy level of a generated medical summary.
Identify if the language is suitable for a layperson (low) or requires medical expertise (proficient).
"""
summary_text: str = dspy.InputField(desc="The generated medical summary to be analyzed.")
reasoning: str = dspy.OutputField(desc="Analysis of jargon, acronyms, and sentence complexity.")
label: Literal["low_health_literacy", "intermediate_health_literacy", "proficient_health_literacy"] = dspy.OutputField()
class HealthLiteracyClassifier(dspy.Module):
def __init__(self):
super().__init__()
self.predictor = dspy.ChainOfThought(HealthLiteracySignature)
def forward(self, summary_text):
return self.predictor(summary_text=summary_text)
# --- 3. Load Trained Logic ---
classifier = HealthLiteracyClassifier()
save_path = "/home/mshahidul/readctrl/data/new_exp/optimized_health_classifier_gpt5-mini_v2.json"
classifier.load(save_path)
accuracy_count = 0
path="/home/mshahidul/readctrl/data/new_exp/test_health_literacy_data_manual_edit.json"
with open(path,'r') as f:
test_data = json.load(f)
for item in test_data:
expected_label = item['label']
text = item['gen_text']
result = classifier(summary_text=text)
if (result.label == expected_label):
accuracy_count += 1
print(f"Correctly classified: {expected_label} ✅")
else:
print(f"Misclassified. Expected: {expected_label}, Got: {result.label} ❌")
accuracy_score = (accuracy_count / len(test_data)) * 100
print(f"\nFinal Accuracy: {accuracy_score:.2f}%") |