Skip to content

Instantly share code, notes, and snippets.

@inoccu
Last active August 3, 2022 22:51
Show Gist options
  • Save inoccu/bc9fae2b71644a63ccc80107e8ef9047 to your computer and use it in GitHub Desktop.
Save inoccu/bc9fae2b71644a63ccc80107e8ef9047 to your computer and use it in GitHub Desktop.

Pythonの基本構文

for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print('Fizz Buzz!')
    elif i % 3 == 0:
        print('Fizz!')
    elif i % 5 == 0:
        print('Buzz!')
    else:
        print(i)

ラベルCSVファイルの作成

TRAIN,gs://-vcm/dogs/dog01.jpg,dog
TRAIN,gs://-vcm/dogs/dog02.jpg,dog
TRAIN,gs://-vcm/dogs/dog03.jpg,dog
TRAIN,gs://-vcm/dogs/dog04.jpg,dog
TRAIN,gs://-vcm/dogs/dog05.jpg,dog
TRAIN,gs://-vcm/dogs/dog06.jpg,dog
TRAIN,gs://-vcm/dogs/dog07.jpg,dog
TRAIN,gs://-vcm/dogs/dog08.jpg,dog
VALIDATION,gs://-vcm/dogs/dog09.jpg,dog
TEST,gs://-vcm/dogs/dog10.jpg,dog
TRAIN,gs://-vcm/cats/cat01.jpg,cat
TRAIN,gs://-vcm/cats/cat02.jpg,cat
TRAIN,gs://-vcm/cats/cat03.jpg,cat
TRAIN,gs://-vcm/cats/cat04.jpg,cat
TRAIN,gs://-vcm/cats/cat05.jpg,cat
TRAIN,gs://-vcm/cats/cat06.jpg,cat
TRAIN,gs://-vcm/cats/cat07.jpg,cat
TRAIN,gs://-vcm/cats/cat08.jpg,cat
VALIDATION,gs://-vcm/cats/cat09.jpg,cat
TEST,gs://-vcm/cats/cat10.jpg,cat

AutoML SDKのインストール

!pip install --upgrade google-cloud-automl

AutoML APIに接続する

from google.oauth2 import service_account
from google.cloud import automl
import json

automl_credential_path = '/content/<アップロードしたJSON鍵のファイル名>.json'
with open(automl_credential_path, 'r') as f:
  service_account_info = json.load(f)
credentials = service_account.Credentials.from_service_account_info(service_account_info)

client = automl.AutoMlClient(credentials=credentials)

データセットの作成

project_location = 'projects/%s/locations/us-central1' % service_account_info['project_id']
metadata = automl.ImageClassificationDatasetMetadata(
    classification_type=automl.ClassificationType.MULTICLASS
)
dataset = automl.Dataset(
    display_name='dog_cat_dataset',
    image_classification_dataset_metadata=metadata,
)

response = client.create_dataset(parent=project_location, dataset=dataset, timeout=300)

created_dataset = response.result()

print('Dataset name: %s' % created_dataset.name)
dataset_id = created_dataset.name.split("/")[-1]
print('Dataset id: %s' % dataset_id)

画像データのインポート

csv_path = 'gs://-vcm/labels.csv'

dataset_full_id = client.dataset_path(service_account_info['project_id'], "us-central1", dataset_id)
input_uris = csv_path.split(',')
gcs_source = automl.GcsSource(input_uris=input_uris)
input_config = automl.InputConfig(gcs_source=gcs_source)
response = client.import_data(name=dataset_full_id, input_config=input_config)

print('Processing import...')
print('Data imported. %s' % response.result())

モデルのトレーニング

metadata = automl.ImageClassificationModelMetadata(
    train_budget_milli_node_hours=8000
)
model = automl.Model(
    display_name='dog_cat_model',
    dataset_id=dataset_id,
    image_classification_model_metadata=metadata,
)

response = client.create_model(parent=project_location, model=model)
training_operation_name = response.operation.name

print('Training operation name: %s' % training_operation_name)
print('Training started...')

画像の分類(1)

from google.cloud import automl

model_id = '<モデルID>'
file_path = '/content/<分類する画像ファイル名>'

automl_credential_path = '/content/<アップロードしたJSON鍵のファイル名>.json'
with open(automl_credential_path, 'r') as f:
  service_account_info = json.load(f)
credentials = service_account.Credentials.from_service_account_info(service_account_info)
project_id = service_account_info['project_id']

prediction_client = automl.PredictionServiceClient(credentials=credentials)
model_full_id = automl.AutoMlClient.model_path(project_id, 'us-central1', model_id)

with open(file_path, 'rb') as content_file:
    content = content_file.read()
image = automl.Image(image_bytes=content)
payload = automl.ExamplePayload(image=image)

params = {'score_threshold': '0.8'}
request = automl.PredictRequest(name=model_full_id, payload=payload, params=params)
response = prediction_client.predict(request=request)

result = response.payload[0]
print('Predicted class name: %s' % result.display_name)
print('Predicted class score: %s' % result.classification.score)

Cloud Functionsで関数を作成する(4)

from datetime import datetime as dt

def reserve(request):
    request_json = request.get_json()
    print(request_json)

    reserve_date = request_json['queryResult']['outputContexts'][0]['parameters']['date']
    date_dt = dt.strptime(reserve_date, '%Y-%m-%dT%H:%M:%S%z')
    reserve_time = request_json['queryResult']['outputContexts'][0]['parameters']['time']
    time_dt = dt.strptime(reserve_time, '%Y-%m-%dT%H:%M:%S%z')    
    number_of_people = request_json['queryResult']['outputContexts'][0]['parameters']['number-integer']

    return {
        'fulfillmentMessages': [
            {
                'text': {
                    'text': [
                        '%sの%sに、%s人での予約を承りました。' % (
                            date_dt.strftime('%m月%d日'),
                            time_dt.strftime('%H時%M分'),
                            str(int(number_of_people)))
                    ]
                }
            }
        ]
    }

Dialogflow SDKのインストール

!pip install --upgrade google-cloud-dialogflow

Dialogflowに接続する

from google.oauth2 import service_account
from google.cloud import dialogflow_v2beta1 as dialogflow
import json
import uuid

dialogflow_credential_path = '/content/<アップロードしたJSON鍵のファイル名>.json'
location = 'global'

# セッションを識別するためUUIDでセッションIDを作成
session_id = uuid.uuid4()

with open(dialogflow_credential_path, 'r') as f:
    service_account_info = json.load(f)

credentials = service_account.Credentials.from_service_account_info(service_account_info)
session_client = dialogflow.SessionsClient(
    credentials=credentials,
    client_options={
        'api_endpoint': '%s-dialogflow.googleapis.com:443' % (location,)
    }
)
project_id = '%s/locations/%s' % (service_account_info['project_id'], location,)
session_path = session_client.session_path(project_id, session_id)

WELCOMEインテントの呼び出し

event_input = dialogflow.EventInput(name='WELCOME', language_code='ja')
query_input = dialogflow.QueryInput(event=event_input)
res = session_client.detect_intent({
    'session': session_path,
    'query_input': query_input
})
response = res.query_result
print(response)
print(response.fulfillment_messages[0].text.text[0])

会話のやり取り(1)

text_input = dialogflow.TextInput(text='明日の19時に4人で予約したい', language_code='ja')
query_input = dialogflow.QueryInput(text=text_input)
res = session_client.detect_intent({
    'session': session_path,
    'query_input': query_input
})
response = res.query_result
print(response)
print(response.fulfillment_messages[0].text.text[0])

会話のやり取り(2)

text_input = dialogflow.TextInput(text='はい', language_code='ja')
query_input = dialogflow.QueryInput(text=text_input)
query_parameter = dialogflow.QueryParameters(contexts=response.output_contexts)
res = session_client.detect_intent({
    'session': session_path,
    'query_input': query_input,
    'query_params': query_parameter
})
response = res.query_result
print(response)
print(response.fulfillment_messages[0].text.text[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment