薬手帳のコピーから薬の名前・用法・用量のみを抽出する（Python+AzureOCR+ChatGPT）

```
!pip install azure-cognitiveservices-vision-computervision
import tkinter as tk
from tkinter import filedialog, Text, Scrollbar
import os
from azure.cognitiveservices.vision.computervision import ComputerVisionClient
from azure.cognitiveservices.vision.computervision.models import VisualFeatureTypes, OperationStatusCodes
from msrest.authentication import CognitiveServicesCredentials
import time
import openai

# Azureのキーとエンドポイントを設定
key = ""
endpoint = ""

# GPTのAPIのキーを設定
openai.api_key = ''

credentials = CognitiveServicesCredentials(key)
client = ComputerVisionClient(endpoint, credentials)

# GUIを作成してアップロードする
def upload_file():
    file_path = filedialog.askopenfilename()
    if not file_path:
        return

    # SDK call
    with open(file_path, "rb") as image_stream:
        rawHttpResponse = client.read_in_stream(image_stream, language="ja", raw=True)

    # Get ID from returned headers
    operationLocation = rawHttpResponse.headers["Operation-Location"]
    numberOfCharsInOperationId = 36
    idLocation = len(operationLocation) - numberOfCharsInOperationId
    operationId = operationLocation[idLocation:]

    # Wait for the operation to complete (this can be adjusted based on your needs)
    time.sleep(3)

    # SDK call
    result = client.get_read_result(operationId)

    # Clear the text box
    text_box.delete(1.0, tk.END)

    # Check the status
    if result.status == OperationStatusCodes.succeeded:        
        ocr_text = '\n'.join([line.text for line in result.analyze_result.read_results[0].lines])
        formatted_text = format_ocr_text(ocr_text)
        text_box.insert(tk.END, formatted_text)
    else:
        text_box.insert(tk.END, "Operation status: " + str(result.status))
    
    root.update_idletasks()

# GPTでOCRで読み取ったテキストを整形する関数
def format_ocr_text(ocr_text):
    prompt ='''
        #Prompt
        You're a top-notch assistant working alongside a physician. Presented below are details from a prescription's OCR. From this data, we need you to extract the medicine's name, the daily dosage and the amount taken at once, and the recommended time for administration.

        #Instructions:

        Represent each medication on a single line, consolidating the medicine's name, its dosage, and the timing.
        Exclude the manufacturer's name when referencing the medicine's name.
        Rectify any typos or missing parts in the medicine's name.
        The format for daily dosage and amount taken at once should be like: "1日2錠(1回1錠)". This is merely an illustrative example.
        Specify only the specific time for administration, such as "after breakfast", without any additional instructions or commands like "please take".
        Provide the answer in Japanese.
    ''' 
    messages = [
        {"role": "system", "content": prompt},
        {"role": "user", "content": ocr_text},
    ]
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=messages,
        max_tokens=500  # 必要に応じて最大トークン数を調整してください
    )

    formatted_text = response['choices'][0]['message']['content'].strip()
    return formatted_text

root = tk.Tk()
root.title("Azure OCR Uploader")

upload_button = tk.Button(root, text="Upload Image", command=upload_file)
upload_button.pack(pady=20)

# Create a text box with a scrollbar
text_box = Text(root, wrap=tk.WORD, width=50, height=15)
scrollbar = Scrollbar(root, command=text_box.yview)
text_box.config(yscrollcommand=scrollbar.set)
text_box.pack(pady=20)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

root.mainloop()    

```


普通に現場で使えそうです。