readctrl / code /RL_model /unsloth_rl /health_classifier.py
shahidul034's picture
Add files using upload-large-folder tool
c7a6fe6 verified
import dspy
import json
from typing import Literal
# --- 1. LLM Configuration ---
def setup_dspy_classifier(save_path, api_key_path):
with open(api_key_path, "r") as f:
api_keys = json.load(f)
# Configure the LM
# Note: 'gpt-5-mini' is used per your configuration; ensure this matches your provider
openai_model = dspy.LM(model='gpt-5-mini', api_key=api_keys["openai"])
dspy.configure(lm=openai_model)
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)
# Initialize and load weights
classifier_instance = HealthLiteracyClassifier()
classifier_instance.load(save_path)
return classifier_instance
# Global instantiation (optional, or you can call setup in your main script)
API_FILE = "/home/mshahidul/api_new.json"
SAVE_PATH = "/home/mshahidul/readctrl/data/new_exp/optimized_health_classifier_gpt5-mini_v2.json"
# Create the instance to be imported
classifier = setup_dspy_classifier(SAVE_PATH, API_FILE)