Switch to unified view

a b/experiment/ocr_gemini.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 7,
6
   "metadata": {},
7
   "outputs": [
8
    {
9
     "name": "stdout",
10
     "output_type": "stream",
11
     "text": [
12
      "C:\\Users\\hoang\\OneDrive\\Desktop\\nghich_prj\\Prescription-Text-Extraction-Pharmacy-Information-Retrieval\n"
13
     ]
14
    }
15
   ],
16
   "source": [
17
    "%cd C:\\Users\\hoang\\OneDrive\\Desktop\\nghich_prj\\Prescription-Text-Extraction-Pharmacy-Information-Retrieval"
18
   ]
19
  },
20
  {
21
   "cell_type": "code",
22
   "execution_count": 11,
23
   "metadata": {},
24
   "outputs": [],
25
   "source": [
26
    "import google.generativeai as genai\n",
27
    "import os\n",
28
    "from dotenv import load_dotenv\n",
29
    "from PIL import Image\n",
30
    "\n",
31
    "\n",
32
    "load_dotenv()\n",
33
    "genai.configure(api_key=os.environ[\"GEMINI_API_KEY\"])"
34
   ]
35
  },
36
  {
37
   "cell_type": "code",
38
   "execution_count": 9,
39
   "metadata": {},
40
   "outputs": [],
41
   "source": [
42
    "model = genai.GenerativeModel(\"gemini-1.5-flash\")"
43
   ]
44
  },
45
  {
46
   "cell_type": "code",
47
   "execution_count": 10,
48
   "metadata": {},
49
   "outputs": [],
50
   "source": [
51
    "PROMPT= \"\"\"\n",
52
    "Extract information for all medications and instructions listed in the given prescription in the following format:\n",
53
    "[\n",
54
    "    {\n",
55
    "        name: \"medication name\",\n",
56
    "        \"amount\": \"dosage\",\n",
57
    "        \"guide\": \"usage instructions, e.g., 1 tablet in the morning\",\n",
58
    "        \"note\": \"additional instructions\"\n",
59
    "    }\n",
60
    "]\n",
61
    "\n",
62
    "Just return the desired format.\n",
63
    "\"\"\""
64
   ]
65
  },
66
  {
67
   "cell_type": "code",
68
   "execution_count": 14,
69
   "metadata": {},
70
   "outputs": [],
71
   "source": [
72
    "img = genai.upload_file(path=r\"C:\\Users\\hoang\\OneDrive\\Desktop\\nghich_prj\\Prescription-Text-Extraction-Pharmacy-Information-Retrieval\\assets\\ocr_sample\\bo-y-te-lui-lo-trinh-bat-buoc-benh-vien-hang-3-tro-len-phai-bo-ke-don-thuoc-viet-tay-202424227.jpg\", display_name=\"Prescription\")\n",
73
    "res = model.generate_content([img, PROMPT])"
74
   ]
75
  },
76
  {
77
   "cell_type": "code",
78
   "execution_count": 15,
79
   "metadata": {},
80
   "outputs": [
81
    {
82
     "name": "stdout",
83
     "output_type": "stream",
84
     "text": [
85
      "[\n",
86
      "  {\n",
87
      "    \"name\": \"Bitex beules\",\n",
88
      "    \"amount\": \"800mg\",\n",
89
      "    \"guide\": \"4 viên/ ngày\",\n",
90
      "    \"note\": \"sáng 2 viên, tối 2 viên\"\n",
91
      "  },\n",
92
      "  {\n",
93
      "    \"name\": \"Keralian\",\n",
94
      "    \"amount\": \"600\",\n",
95
      "    \"guide\": \"4 viên/ ngày\",\n",
96
      "    \"note\": \"sáng 2 viên, tối 2 viên\"\n",
97
      "  },\n",
98
      "  {\n",
99
      "    \"name\": \"Debelecol\",\n",
100
      "    \"amount\": \"600mg\",\n",
101
      "    \"guide\": \"15 ml\",\n",
102
      "    \"note\": \"trước bữa sáng\"\n",
103
      "  }\n",
104
      "]\n"
105
     ]
106
    }
107
   ],
108
   "source": [
109
    "print(res.text)"
110
   ]
111
  }
112
 ],
113
 "metadata": {
114
  "kernelspec": {
115
   "display_name": "base",
116
   "language": "python",
117
   "name": "python3"
118
  },
119
  "language_info": {
120
   "codemirror_mode": {
121
    "name": "ipython",
122
    "version": 3
123
   },
124
   "file_extension": ".py",
125
   "mimetype": "text/x-python",
126
   "name": "python",
127
   "nbconvert_exporter": "python",
128
   "pygments_lexer": "ipython3",
129
   "version": "3.9.13"
130
  }
131
 },
132
 "nbformat": 4,
133
 "nbformat_minor": 2
134
}