{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "header",
   "metadata": {},
   "source": [
    "# PaddleOCR Hyperparameter Optimization via REST API\n",
    "\n",
    "Uses Ray Tune + Optuna to find optimal PaddleOCR parameters.\n",
    "\n",
    "## Prerequisites\n",
    "\n",
    "```bash\n",
    "cd src/paddle_ocr\n",
    "docker compose -f docker-compose.workers.yml up  # GPU workers on 8001-8002\n",
    "# or: docker compose -f docker-compose.workers.yml --profile cpu up\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "deps",
   "metadata": {},
   "outputs": [],
   "source": "# Pin Ray version for API stability (tune.report takes dict, not kwargs in 2.x)\n%pip install -q \"ray[tune]==2.53.0\" optuna requests pandas"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "setup",
   "metadata": {},
   "outputs": [],
   "source": "from raytune_ocr import (\n    check_workers, create_trainable, run_tuner, analyze_results, correlation_analysis,\n    paddle_ocr_payload, PADDLE_OCR_SEARCH_SPACE, PADDLE_OCR_CONFIG_KEYS,\n)\n\n# Worker ports (3 workers to avoid OOM)\nPORTS = [8001, 8002, 8003]\n\n# Check workers are running\nhealthy = check_workers(PORTS, \"PaddleOCR\")"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "tune",
   "metadata": {},
   "outputs": [],
   "source": "# Create trainable and run tuning\ntrainable = create_trainable(PORTS, paddle_ocr_payload)\n\nresults = run_tuner(\n    trainable=trainable,\n    search_space=PADDLE_OCR_SEARCH_SPACE,\n    num_samples=128,\n    num_workers=len(healthy),\n)"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "analysis",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Analyze results\n",
    "df = analyze_results(\n",
    "    results,\n",
    "    prefix=\"raytune_paddle\",\n",
    "    config_keys=PADDLE_OCR_CONFIG_KEYS,\n",
    ")\n",
    "\n",
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "correlation",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Correlation analysis\n",
    "correlation_analysis(df, PADDLE_OCR_CONFIG_KEYS)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}