Search in sources :

Example 6 with TikaConfigException

use of org.apache.tika.exception.TikaConfigException in project tika by apache.

the class DL4JInceptionV3Net method initialize.

@Override
public void initialize(Map<String, Param> params) throws TikaConfigException {
    //STEP 1: resolve weights file, download if necessary
    if (modelWeightsPath.startsWith("http://") || modelWeightsPath.startsWith("https://")) {
        LOG.debug("Config instructed to download the weights file, doing so.");
        try {
            modelWeightsPath = cachedDownload(cacheDir, URI.create(modelWeightsPath)).getAbsolutePath();
        } catch (IOException e) {
            throw new TikaConfigException(e.getMessage(), e);
        }
    } else {
        File modelFile = retrieveFile(modelWeightsPath);
        if (!modelFile.exists()) {
            LOG.error("modelWeights does not exist at :: {}", modelWeightsPath);
            return;
        }
        modelWeightsPath = modelFile.getAbsolutePath();
    }
    //STEP 2: resolve model JSON
    File modelJsonFile = retrieveFile(modelJsonPath);
    if (modelJsonFile == null || !modelJsonFile.exists()) {
        LOG.error("Could not locate file {}", modelJsonPath);
        return;
    }
    modelJsonPath = modelJsonFile.getAbsolutePath();
    //STEP 3: Load labels map
    try (InputStream stream = retrieveResource(labelFile)) {
        this.labelMap = loadClassIndex(stream);
    } catch (IOException | ParseException e) {
        LOG.error("Could not load labels map", e);
        return;
    }
    //STEP 4: initialize the graph
    try {
        this.imageLoader = new NativeImageLoader(imgHeight, imgWidth, imgChannels);
        LOG.info("Going to load Inception network...");
        long st = System.currentTimeMillis();
        this.graph = KerasModelImport.importKerasModelAndWeights(modelJsonPath, modelWeightsPath, false);
        long time = System.currentTimeMillis() - st;
        LOG.info("Loaded the Inception model. Time taken={}ms", time);
    } catch (IOException | InvalidKerasConfigurationException | UnsupportedKerasConfigurationException e) {
        throw new TikaConfigException(e.getMessage(), e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TikaConfigException(org.apache.tika.exception.TikaConfigException) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) UnsupportedKerasConfigurationException(org.deeplearning4j.nn.modelimport.keras.UnsupportedKerasConfigurationException) File(java.io.File) InvalidKerasConfigurationException(org.deeplearning4j.nn.modelimport.keras.InvalidKerasConfigurationException) NativeImageLoader(org.datavec.image.loader.NativeImageLoader)

Example 7 with TikaConfigException

use of org.apache.tika.exception.TikaConfigException in project tika by apache.

the class TensorflowImageRecParser method initialize.

@Override
public void initialize(Map<String, Param> params) throws TikaConfigException {
    try {
        if (!modelFile.exists()) {
            modelFile.getParentFile().mkdirs();
            LOG.warn("Model doesn't exist at {}. Expecting the script to download it.", modelFile);
        }
        if (!scriptFile.exists()) {
            scriptFile.getParentFile().mkdirs();
            LOG.info("Copying script to : {}", scriptFile);
            try (InputStream sourceStream = getClass().getResourceAsStream(SCRIPT_FILE_NAME)) {
                try (OutputStream destStream = new FileOutputStream(scriptFile)) {
                    IOUtils.copy(sourceStream, destStream);
                }
            }
            LOG.debug("Copied..");
        }
        String[] availabilityCheckArgs = { executor, scriptFile.getAbsolutePath(), modelArg, modelFile.getAbsolutePath(), availabilityTestArgs };
        available = ExternalParser.check(availabilityCheckArgs);
        LOG.debug("Available? {}", available);
        if (!available) {
            return;
        }
        String[] parseCmd = { executor, scriptFile.getAbsolutePath(), modelArg, modelFile.getAbsolutePath(), imageArg, INPUT_FILE_TOKEN, "--out_file", //inserting output token to let external parser parse metadata
        OUTPUT_FILE_TOKEN };
        setCommand(parseCmd);
        HashMap<Pattern, String> patterns = new HashMap<>();
        patterns.put(Pattern.compile(outPattern), null);
        setMetadataExtractionPatterns(patterns);
        setIgnoredLineConsumer(IGNORED_LINE_LOGGER);
    } catch (Exception e) {
        throw new TikaConfigException(e.getMessage(), e);
    }
}
Also used : Pattern(java.util.regex.Pattern) HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) TikaConfigException(org.apache.tika.exception.TikaConfigException) TikaException(org.apache.tika.exception.TikaException) TikaConfigException(org.apache.tika.exception.TikaConfigException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 8 with TikaConfigException

use of org.apache.tika.exception.TikaConfigException in project tika by apache.

the class TensorflowRESTRecogniser method initialize.

@Override
public void initialize(Map<String, Param> params) throws TikaConfigException {
    try {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(new HttpGet(healthUri));
        available = response.getStatusLine().getStatusCode() == 200;
        LOG.info("Available = {}, API Status = {}", available, response.getStatusLine());
    } catch (Exception e) {
        available = false;
        throw new TikaConfigException(e.getMessage(), e);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) TikaConfigException(org.apache.tika.exception.TikaConfigException) HttpResponse(org.apache.http.HttpResponse) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TikaException(org.apache.tika.exception.TikaException) TikaConfigException(org.apache.tika.exception.TikaConfigException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

TikaConfigException (org.apache.tika.exception.TikaConfigException)8 IOException (java.io.IOException)4 TikaException (org.apache.tika.exception.TikaException)3 Test (org.junit.Test)3 SAXException (org.xml.sax.SAXException)3 File (java.io.File)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 TikaConfig (org.apache.tika.config.TikaConfig)2 Metadata (org.apache.tika.metadata.Metadata)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 Pattern (java.util.regex.Pattern)1 SentimentME (opennlp.tools.sentiment.SentimentME)1