|
import logging |
|
import json |
|
import subprocess |
|
import time |
|
from typing import Dict, Any |
|
import runpod |
|
|
|
def handler(job: Dict[str, Any]) -> Dict[str, Any]: |
|
""" |
|
Handler function for running `llm_evaluation.py` in a RunPod serverless environment. |
|
|
|
Args: |
|
job (Dict[str, Any]): The job input containing parameters for evaluation. |
|
|
|
Returns: |
|
Dict[str, Any]: Evaluation results or error details. |
|
""" |
|
start_time = time.time() |
|
logging.info("Evaluation handler started.") |
|
|
|
|
|
job_input = job.get('input', {}) |
|
model_name = job_input.get('model_name') |
|
|
|
if not model_name: |
|
logging.error("Missing required field: 'model_name'") |
|
return { |
|
"status": "error", |
|
"error": "Missing required field: 'model_name'" |
|
} |
|
|
|
try: |
|
|
|
evaluation_input = json.dumps({"model_name": model_name}) |
|
logging.info(f"Starting evaluation for model: {model_name}") |
|
|
|
|
|
result = subprocess.run( |
|
['python3', 'llm_evaluation.py', evaluation_input], |
|
capture_output=True, |
|
text=True, |
|
check=True |
|
) |
|
logging.info("Model evaluation completed successfully.") |
|
|
|
|
|
try: |
|
evaluation_results = json.loads(result.stdout) |
|
except json.JSONDecodeError: |
|
evaluation_results = {"raw_output": result.stdout} |
|
|
|
return { |
|
"status": "success", |
|
"model_name": model_name, |
|
"processing_time": time.time() - start_time, |
|
"evaluation_results": evaluation_results |
|
} |
|
|
|
except subprocess.CalledProcessError as e: |
|
logging.error(f"Evaluation process failed: {e.stderr}") |
|
return { |
|
"status": "error", |
|
"error": f"Evaluation process failed: {e.stderr}", |
|
"stdout": e.stdout, |
|
"stderr": e.stderr |
|
} |
|
except Exception as e: |
|
logging.error(f"Unhandled error during evaluation: {str(e)}") |
|
return { |
|
"status": "error", |
|
"error": str(e) |
|
} |
|
|
|
if __name__ == "__main__": |
|
|
|
logging.basicConfig( |
|
level=logging.INFO, |
|
format="%(asctime)s - %(levelname)s - %(message)s" |
|
) |
|
|
|
|
|
runpod.serverless.start({"handler": handler}) |
|
|
|
|