Search in sources :

Example 36 with Dataset

use of com.google.cloud.automl.v1.Dataset in project java-automl by googleapis.

the class LanguageTextClassificationCreateDataset method createDataset.

// Create a dataset
static void createDataset(String projectId, String displayName) throws IOException, ExecutionException, InterruptedException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
        // A resource that represents Google Cloud Platform location.
        LocationName projectLocation = LocationName.of(projectId, "us-central1");
        // Specify the classification type
        // Types:
        // MultiLabel: Multiple labels are allowed for one example.
        // MultiClass: At most one label is allowed per example.
        ClassificationType classificationType = ClassificationType.MULTILABEL;
        // Specify the text classification type for the dataset.
        TextClassificationDatasetMetadata metadata = TextClassificationDatasetMetadata.newBuilder().setClassificationType(classificationType).build();
        Dataset dataset = Dataset.newBuilder().setDisplayName(displayName).setTextClassificationDatasetMetadata(metadata).build();
        OperationFuture<Dataset, OperationMetadata> future = client.createDatasetAsync(projectLocation, dataset);
        Dataset createdDataset = future.get();
        // Display the dataset information.
        System.out.format("Dataset name: %s\n", createdDataset.getName());
        // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
        // required for other methods.
        // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
        String[] names = createdDataset.getName().split("/");
        String datasetId = names[names.length - 1];
        System.out.format("Dataset id: %s\n", datasetId);
    }
}
Also used : Dataset(com.google.cloud.automl.v1.Dataset) OperationMetadata(com.google.cloud.automl.v1.OperationMetadata) ClassificationType(com.google.cloud.automl.v1.ClassificationType) TextClassificationDatasetMetadata(com.google.cloud.automl.v1.TextClassificationDatasetMetadata) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient) LocationName(com.google.cloud.automl.v1.LocationName)

Example 37 with Dataset

use of com.google.cloud.automl.v1.Dataset in project java-automl by googleapis.

the class ExportDataset method exportDataset.

// Export a dataset to a GCS bucket
static void exportDataset(String projectId, String datasetId, String gcsUri) throws IOException, ExecutionException, InterruptedException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
        // Get the complete path of the dataset.
        DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build();
        // Export the dataset to the output URI.
        OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
        System.out.println("Processing export...");
        Empty response = client.exportDataAsync(datasetFullId, outputConfig).get();
        System.out.format("Dataset exported. %s\n", response);
    }
}
Also used : Empty(com.google.protobuf.Empty) OutputConfig(com.google.cloud.automl.v1.OutputConfig) DatasetName(com.google.cloud.automl.v1.DatasetName) GcsDestination(com.google.cloud.automl.v1.GcsDestination) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Example 38 with Dataset

use of com.google.cloud.automl.v1.Dataset in project java-automl by googleapis.

the class GetDataset method getDataset.

// Get a dataset
static void getDataset(String projectId, String datasetId) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AutoMlClient client = AutoMlClient.create()) {
        // Get the complete path of the dataset.
        DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
        Dataset dataset = client.getDataset(datasetFullId);
        // Display the dataset information
        System.out.format("Dataset name: %s\n", dataset.getName());
        // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are
        // required for other methods.
        // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}`
        String[] names = dataset.getName().split("/");
        String retrievedDatasetId = names[names.length - 1];
        System.out.format("Dataset id: %s\n", retrievedDatasetId);
        System.out.format("Dataset display name: %s\n", dataset.getDisplayName());
        System.out.println("Dataset create time:");
        System.out.format("\tseconds: %s\n", dataset.getCreateTime().getSeconds());
        System.out.format("\tnanos: %s\n", dataset.getCreateTime().getNanos());
        // [END automl_language_sentiment_analysis_get_dataset]
        // [END automl_language_text_classification_get_dataset]
        // [END automl_translate_get_dataset]
        // [END automl_vision_classification_get_dataset]
        // [END automl_vision_object_detection_get_dataset]
        System.out.format("Text extraction dataset metadata: %s\n", dataset.getTextExtractionDatasetMetadata());
        // [END automl_language_entity_extraction_get_dataset]
        // [START automl_language_sentiment_analysis_get_dataset]
        System.out.format("Text sentiment dataset metadata: %s\n", dataset.getTextSentimentDatasetMetadata());
        // [END automl_language_sentiment_analysis_get_dataset]
        // [START automl_language_text_classification_get_dataset]
        System.out.format("Text classification dataset metadata: %s\n", dataset.getTextClassificationDatasetMetadata());
        // [END automl_language_text_classification_get_dataset]
        // [START automl_translate_get_dataset]
        System.out.println("Translation dataset metadata:");
        System.out.format("\tSource language code: %s\n", dataset.getTranslationDatasetMetadata().getSourceLanguageCode());
        System.out.format("\tTarget language code: %s\n", dataset.getTranslationDatasetMetadata().getTargetLanguageCode());
        // [END automl_translate_get_dataset]
        // [START automl_vision_classification_get_dataset]
        System.out.format("Image classification dataset metadata: %s\n", dataset.getImageClassificationDatasetMetadata());
        // [END automl_vision_classification_get_dataset]
        // [START automl_vision_object_detection_get_dataset]
        System.out.format("Image object detection dataset metadata: %s\n", dataset.getImageObjectDetectionDatasetMetadata());
    // [START automl_language_entity_extraction_get_dataset]
    // [START automl_language_sentiment_analysis_get_dataset]
    // [START automl_language_text_classification_get_dataset]
    // [START automl_translate_get_dataset]
    // [START automl_vision_classification_get_dataset]
    }
}
Also used : DatasetName(com.google.cloud.automl.v1.DatasetName) Dataset(com.google.cloud.automl.v1.Dataset) AutoMlClient(com.google.cloud.automl.v1.AutoMlClient)

Example 39 with Dataset

use of com.google.cloud.automl.v1.Dataset in project idempiere by idempiere.

the class ModelADServiceImpl method getList.

public WindowTabDataDocument getList(ModelGetListRequestDocument req) {
    try {
        getCompiereService().connect();
        WindowTabDataDocument resdoc = WindowTabDataDocument.Factory.newInstance();
        WindowTabData res = resdoc.addNewWindowTabData();
        DataSet ds = res.addNewDataSet();
        ModelGetList modelGetList = req.getModelGetListRequest().getModelGetList();
        String serviceType = modelGetList.getServiceType();
        int cnt = 0;
        ADLoginRequest reqlogin = req.getModelGetListRequest().getADLoginRequest();
        String err = login(reqlogin, webServiceName, "getList", serviceType);
        if (err != null && err.length() > 0) {
            res.setError(err);
            res.setErrorInfo(err);
            res.setSuccess(false);
            return resdoc;
        }
        int roleid = reqlogin.getRoleID();
        // Validate parameters
        try {
            modelGetList.setADReferenceID(validateParameter("AD_Reference_ID", modelGetList.getADReferenceID()));
        } catch (XmlValueOutOfRangeException e) {
            // Catch the exception when the Reference ID is not an Integer
            String refUU = getUUIDValue(modelGetList.xgetADReferenceID());
            if (refUU == null) {
                throw e;
            }
            modelGetList.setADReferenceID(validateParameter("AD_Reference_ID", 0, refUU));
        }
        modelGetList.setFilter(validateParameter("Filter", modelGetList.getFilter()));
        int ref_id = modelGetList.getADReferenceID();
        String filter = modelGetList.getFilter();
        if (filter == null || filter.length() == 0)
            filter = "";
        else
            filter = " AND " + filter;
        CompiereService m_cs = getCompiereService();
        Properties ctx = m_cs.getCtx();
        MReference ref = MReference.get(ctx, ref_id);
        String sql = null;
        ArrayList<String> listColumnNames = new ArrayList<String>();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        MWebServiceType m_webservicetype = getWebServiceType();
        if (MReference.VALIDATIONTYPE_ListValidation.equals(ref.getValidationType())) {
            // Fill List Reference
            String ad_language = Env.getAD_Language(ctx);
            boolean isBaseLanguage = Env.isBaseLanguage(ad_language, "AD_Ref_List");
            sql = isBaseLanguage ? "SELECT AD_Ref_List.AD_Ref_List_ID, AD_Ref_List.Value, AD_Ref_List.Name, AD_Ref_List.Description " + "FROM AD_Ref_List " + "WHERE AD_Ref_List.AD_Reference_ID=? AND AD_Ref_List.IsActive='Y' " + filter + " ORDER BY AD_Ref_List.Name" : "SELECT AD_Ref_List.AD_Ref_List_ID, AD_Ref_List.Value, AD_Ref_List_Trl.Name, AD_Ref_List_Trl.Description " + "FROM AD_Ref_List, AD_Ref_List_Trl " + "WHERE AD_Ref_List.AD_Reference_ID=? AND AD_Ref_List.IsActive='Y' AND AD_Ref_List_Trl.AD_Language=? AND AD_Ref_List.AD_Ref_List_ID=AD_Ref_List_Trl.AD_Ref_List_ID " + filter + " ORDER BY AD_Ref_List_Trl.Name";
            listColumnNames.add("AD_Ref_List_ID");
            listColumnNames.add("Value");
            listColumnNames.add("Name");
            listColumnNames.add("Description");
            try {
                pstmt = DB.prepareStatement(sql, null);
                pstmt.setInt(1, ref_id);
                if (!isBaseLanguage)
                    pstmt.setString(2, ad_language);
                rs = pstmt.executeQuery();
            } catch (SQLException e) {
                res.setError(e.getMessage());
                res.setErrorInfo(sql);
                res.setSuccess(false);
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
                throw new IdempiereServiceFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
            }
        } else if (MReference.VALIDATIONTYPE_TableValidation.equals(ref.getValidationType())) {
            // Fill values from a reference table
            MRole role = MRole.get(ctx, roleid);
            MRefTable rt = MRefTable.get(ctx, ref_id);
            if (rt == null || rt.get_ID() == 0)
                throw new IdempiereServiceFault("Web service type " + m_webservicetype.getValue() + ": reference table " + ref_id + " not found", new QName("getList"));
            MTable table = new MTable(ctx, rt.getAD_Table_ID(), null);
            MColumn column = new MColumn(ctx, rt.getAD_Key(), null);
            // TODO: if any value or identifier column is translated, then get them from trl table (and client has multilanguage documents enabled)
            sql = "SELECT " + column.getColumnName();
            listColumnNames.add(column.getColumnName());
            if (rt.isValueDisplayed()) {
                sql += ",Value";
                listColumnNames.add("Value");
            }
            String sqlident = "SELECT ColumnName FROM AD_Column WHERE AD_Table_ID=? AND IsActive='Y' AND IsIdentifier='Y' ORDER BY SeqNo";
            PreparedStatement pstmtident = null;
            ResultSet rsident = null;
            try {
                pstmtident = DB.prepareStatement(sqlident, null);
                pstmtident.setInt(1, rt.getAD_Table_ID());
                rsident = pstmtident.executeQuery();
                while (rsident.next()) {
                    String colnameident = rsident.getString("ColumnName");
                    if (rt.isValueDisplayed() && colnameident.equalsIgnoreCase("Value")) {
                    // Value already added
                    } else {
                        sql += "," + colnameident;
                        listColumnNames.add(colnameident);
                    }
                }
            } catch (Exception e) {
            // ignore this exception
            } finally {
                DB.close(rsident, pstmtident);
                rsident = null;
                pstmtident = null;
            }
            sql += " FROM " + table.getTableName() + " WHERE IsActive='Y'";
            sql = role.addAccessSQL(sql, table.getTableName(), true, true);
            sql += filter;
            if (rt.getWhereClause() != null && rt.getWhereClause().length() > 0)
                sql += " AND " + rt.getWhereClause();
            if (rt.getOrderByClause() != null && rt.getOrderByClause().length() > 0)
                sql += " ORDER BY " + rt.getOrderByClause();
            try {
                pstmt = DB.prepareStatement(sql, null);
                rs = pstmt.executeQuery();
            } catch (SQLException e) {
                res.setError(e.getMessage());
                res.setErrorInfo(sql);
                res.setSuccess(false);
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
                throw new IdempiereServiceFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
            }
        } else {
        // Don't fill - wrong type
        }
        if (rs != null) {
            try {
                while (rs.next()) {
                    cnt++;
                    // Add values to the dataset
                    DataRow dr = ds.addNewDataRow();
                    for (String listColumnName : listColumnNames) {
                        if (m_webservicetype.isOutputColumnNameAllowed(listColumnName)) {
                            DataField dfid = dr.addNewField();
                            dfid.setColumn(listColumnName);
                            dfid.setVal(rs.getString(listColumnName));
                        }
                    }
                }
                res.setSuccess(true);
            } catch (SQLException e) {
                res.setError(e.getMessage());
                res.setErrorInfo(sql);
                res.setSuccess(false);
                throw new IdempiereServiceFault(e.getClass().toString() + " " + e.getMessage() + " sql=" + sql, e.getCause(), new QName("getList"));
            } finally {
                DB.close(rs, pstmt);
                rs = null;
                pstmt = null;
            }
        }
        res.setRowCount(cnt);
        res.setNumRows(cnt);
        res.setTotalRows(cnt);
        res.setStartRow(1);
        return resdoc;
    } finally {
        getCompiereService().disconnect();
    }
}
Also used : ADLoginRequest(org.idempiere.adInterface.x10.ADLoginRequest) MWebServiceType(org.idempiere.webservices.model.MWebServiceType) DataSet(org.idempiere.adInterface.x10.DataSet) SQLException(java.sql.SQLException) MRole(org.compiere.model.MRole) ArrayList(java.util.ArrayList) ModelGetList(org.idempiere.adInterface.x10.ModelGetList) Properties(java.util.Properties) DataRow(org.idempiere.adInterface.x10.DataRow) MRefTable(org.compiere.model.MRefTable) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) ResultSet(java.sql.ResultSet) MReference(org.compiere.model.MReference) MColumn(org.compiere.model.MColumn) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) QName(javax.xml.namespace.QName) WindowTabData(org.idempiere.adInterface.x10.WindowTabData) PreparedStatement(java.sql.PreparedStatement) XmlValueOutOfRangeException(org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException) SQLException(java.sql.SQLException) XmlValueOutOfRangeException(org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException) MTable(org.compiere.model.MTable) DataField(org.idempiere.adInterface.x10.DataField)

Example 40 with Dataset

use of com.google.cloud.automl.v1.Dataset in project idempiere by idempiere.

the class ModelADServiceImpl method readData.

// updateData
public WindowTabDataDocument readData(ModelCRUDRequestDocument req) {
    try {
        getCompiereService().connect();
        WindowTabDataDocument ret = WindowTabDataDocument.Factory.newInstance();
        WindowTabData resp = ret.addNewWindowTabData();
        ModelCRUD modelCRUD = req.getModelCRUDRequest().getModelCRUD();
        String serviceType = modelCRUD.getServiceType();
        int cnt = 0;
        ADLoginRequest reqlogin = req.getModelCRUDRequest().getADLoginRequest();
        String err = login(reqlogin, webServiceName, "readData", serviceType);
        if (err != null && err.length() > 0) {
            resp.setError(err);
            return ret;
        }
        // Validate parameters vs service type
        try {
            validateCRUD(modelCRUD);
        } catch (IdempiereServiceFault e) {
            resp.setError(e.getMessage());
            return ret;
        }
        CompiereService m_cs = getCompiereService();
        MWebServiceType m_webservicetype = getWebServiceType();
        // start a trx
        String trxName = localTrxName;
        Properties ctx = m_cs.getCtx();
        String tableName = modelCRUD.getTableName();
        String recordIDVar = modelCRUD.getRecordIDVariable();
        int recordID = modelCRUD.getRecordID();
        if (recordIDVar != null && recordIDVar.startsWith("@")) {
            Integer retVal = (Integer) parseVariable(recordIDVar, null, null, getRequestCtx());
            if (retVal == null) {
                resp.setError("Cannot resolve variable: " + recordIDVar);
                return ret;
            }
            recordID = retVal;
        }
        // get the PO for the tablename and record ID
        MTable table = MTable.get(ctx, tableName);
        if (table == null)
            throw new IdempiereServiceFault("Web service type " + m_webservicetype.getValue() + ": table " + tableName + " not found", new QName("readData"));
        PO po = table.getPO(recordID, trxName);
        if (po == null) {
            resp.setSuccess(false);
            resp.setRowCount(cnt);
            resp.setNumRows(cnt);
            resp.setTotalRows(cnt);
            resp.setStartRow(0);
            return ret;
        }
        cnt = 1;
        POInfo poinfo = POInfo.getPOInfo(ctx, table.getAD_Table_ID());
        DataSet ds = resp.addNewDataSet();
        DataRow dr = ds.addNewDataRow();
        for (int i = 0; i < poinfo.getColumnCount(); i++) {
            String columnName = poinfo.getColumnName(i);
            if (m_webservicetype.isOutputColumnNameAllowed(columnName)) {
                DataField dfid = dr.addNewField();
                dfid.setColumn(columnName);
                if (po.get_Value(i) != null) {
                    if (po.get_Value(i) instanceof byte[]) {
                        dfid.setVal(new String(Base64.encodeBase64((byte[]) po.get_Value(i))));
                    } else if (po.get_Value(i) instanceof Boolean) {
                        dfid.setVal((Boolean) po.get_Value(i) ? "Y" : "N");
                    } else {
                        dfid.setVal(po.get_Value(i).toString());
                    }
                } else
                    dfid.setVal(null);
            }
        }
        resp.setSuccess(true);
        resp.setRowCount(cnt);
        resp.setNumRows(cnt);
        resp.setTotalRows(cnt);
        resp.setStartRow(1);
        return ret;
    } finally {
        getCompiereService().disconnect();
    }
}
Also used : ADLoginRequest(org.idempiere.adInterface.x10.ADLoginRequest) MWebServiceType(org.idempiere.webservices.model.MWebServiceType) DataSet(org.idempiere.adInterface.x10.DataSet) IdempiereServiceFault(org.idempiere.webservices.fault.IdempiereServiceFault) QName(javax.xml.namespace.QName) WindowTabData(org.idempiere.adInterface.x10.WindowTabData) Properties(java.util.Properties) DataRow(org.idempiere.adInterface.x10.DataRow) POInfo(org.compiere.model.POInfo) WindowTabDataDocument(org.idempiere.adInterface.x10.WindowTabDataDocument) MTable(org.compiere.model.MTable) DataField(org.idempiere.adInterface.x10.DataField) ModelCRUD(org.idempiere.adInterface.x10.ModelCRUD) PO(org.compiere.model.PO)

Aggregations

IOException (java.io.IOException)14 AutoMlClient (com.google.cloud.automl.v1.AutoMlClient)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 PrintStream (java.io.PrintStream)10 Before (org.junit.Before)10 Dataset (com.google.cloud.datalabeling.v1beta1.Dataset)9 ArrayList (java.util.ArrayList)9 Dataset (com.google.cloud.automl.v1.Dataset)8 LocationName (com.google.cloud.automl.v1.LocationName)7 OperationMetadata (com.google.cloud.automl.v1.OperationMetadata)7 DataLabelingServiceClient (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient)7 ProjectName (com.google.cloud.datalabeling.v1beta1.ProjectName)7 CreateDatasetOperationMetadata (com.google.cloud.aiplatform.v1.CreateDatasetOperationMetadata)6 Dataset (com.google.cloud.aiplatform.v1.Dataset)6 DatasetServiceClient (com.google.cloud.aiplatform.v1.DatasetServiceClient)6 DatasetServiceSettings (com.google.cloud.aiplatform.v1.DatasetServiceSettings)6 LocationName (com.google.cloud.aiplatform.v1.LocationName)6 AutoMlClient (com.google.cloud.automl.v1beta1.AutoMlClient)6 Dataset (com.google.cloud.automl.v1beta1.Dataset)6 LocationName (com.google.cloud.automl.v1beta1.LocationName)6