Spaces:
Runtime error
Runtime error
| # Copyright 2020 Google LLC | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # https://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| # [START aiplatform_predict_custom_trained_model_sample] | |
| from typing import Dict, List, Union | |
| from google.cloud import aiplatform | |
| from google.protobuf import json_format | |
| from google.protobuf.struct_pb2 import Value | |
| import os | |
| content = os.environ['API_KEY'] | |
| with open('key.json', 'w') as file: | |
| file.write(content) | |
| os.environ['GOOGLE_APPLICATION_CREDENTIALS']= './key.json' | |
| def predict_custom_trained_model_sample( | |
| project: str, | |
| endpoint_id: str, | |
| instances: Union[Dict, List[Dict]], | |
| location: str = "us-central1", | |
| api_endpoint: str = "us-central1-aiplatform.googleapis.com", | |
| ): | |
| """ | |
| `instances` can be either single instance of type dict or a list | |
| of instances. | |
| """ | |
| # The AI Platform services require regional API endpoints. | |
| client_options = {"api_endpoint": api_endpoint} | |
| # Initialize client that will be used to create and send requests. | |
| # This client only needs to be created once, and can be reused for multiple requests. | |
| client = aiplatform.gapic.PredictionServiceClient(client_options=client_options) | |
| # The format of each instance should conform to the deployed model's prediction input schema. | |
| instances = instances if type(instances) == list else [instances] | |
| instances = [ | |
| json_format.ParseDict(instance_dict, Value()) for instance_dict in instances | |
| ] | |
| parameters_dict = {} | |
| parameters = json_format.ParseDict(parameters_dict, Value()) | |
| endpoint = client.endpoint_path( | |
| project=project, location=location, endpoint=endpoint_id | |
| ) | |
| response = client.predict( | |
| endpoint=endpoint, instances=instances, parameters=parameters | |
| ) | |
| print("response") | |
| print(" deployed_model_id:", response.deployed_model_id) | |
| # The predictions are a google.protobuf.Value representation of the model's predictions. | |
| predictions = response.predictions | |
| for prediction in predictions: | |
| print(" prediction:", dict(prediction)) | |
| return predictions[0] | |
| # [END aiplatform_predict_custom_trained_model_sample] | |
| import base64 | |
| import os | |
| from datetime import datetime | |
| from io import BytesIO | |
| import numpy as np | |
| import requests | |
| from google.cloud import aiplatform | |
| from PIL import Image | |
| def download_image(url): | |
| response = requests.get(url) | |
| return Image.open(BytesIO(response.content)).convert("RGB") | |
| def image_to_base64(image, format="JPEG"): | |
| # Convert numpy array to PIL Image | |
| image_pil = Image.fromarray((image * 255).astype(np.uint8)) | |
| buffer = BytesIO() | |
| image_pil.save(buffer, format=format) | |
| image_str = base64.b64encode(buffer.getvalue()).decode("utf-8") | |
| return image_str | |
| import gradio as gr | |
| def predict (image, text): | |
| if len(text) == 0: | |
| return "No prompt provided" | |
| response = predict_custom_trained_model_sample( | |
| instances=[{ "image": image_to_base64(image),"text":text}], | |
| project="1018963165306", | |
| endpoint_id="5638185676072550400", | |
| location="us-central1" | |
| ) | |
| print(dict(response)) | |
| return dict(response)['answer'] | |
| demo = gr.Interface(fn=predict, inputs=["image","text"],outputs="text") | |
| demo.launch() |