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}%")