Giter Club home page Giter Club logo

Comments (7)

ajar19 avatar ajar19 commented on August 17, 2024

Hello @evanlaf, Parsing the first string takes longer as we load the ML model in the backend. This is the expected behaviour of the ML model. The suggested approach is to load the model once and keep the object in RAM to speed up subsequent inferences.

from hawking.

evanlaf avatar evanlaf commented on August 17, 2024

Hi @ajar19 - understood. Where is the model being loaded? I'm wondering if I can write a method to load the model then actually only parse a string when I'm ready, rather than loading the model the first time that the string is being parsed.

from hawking.

ajar19 avatar ajar19 commented on August 17, 2024

@evanlaf For now, we load the model on the first hit. We suggest warming up the model by giving it any basic sentence; that will just do the trick.

private static final HawkingTimeParser PARSER = new HawkingTimeParser(); PARSER.parse("what do you do this month", new Date(), new HawkingConfiguration(),"eng");

from hawking.

evanlaf avatar evanlaf commented on August 17, 2024

@ajar19 Could you clarify where the model is being saved / why this is not working? My steps:

  1. When application opens, check if config, parser, and parser class is persisted in shared preferences.
  2. If yes: Pull them and deserialize.
  3. If no: Initialize objects, parse a test statement, then push objects to shared preferences.

My code:

Step 1: Check if objects are persisted. If yes, restore them (Step 2). If no, call createAndPersistModel()

    private fun restoreState() {
        // Restore Parser objects
        val dateTimeParserJson = application
            .getSharedPreferences(Constants.SHARED_PREFERENCES_MODEL_NAME, Context.MODE_PRIVATE)
            .getString("dateTimeParser", null)

        val parserConfigJson = application
            .getSharedPreferences(Constants.SHARED_PREFERENCES_MODEL_NAME, Context.MODE_PRIVATE)
            .getString("parserConfig", null)

        val parserJson = application
            .getSharedPreferences(Constants.SHARED_PREFERENCES_MODEL_NAME, Context.MODE_PRIVATE)
            .getString("parser", null)

        if (dateTimeParserJson == null || parserConfigJson == null) {
            createAndPersistModel()
        }
        else {
            val gs = Gson()

            dateTimeParser = gs.fromJson(dateTimeParserJson, HawkingTimeParser::class.java)
            parserConfig = gs.fromJson(parserConfigJson, HawkingConfiguration::class.java)
            parser = gs.fromJson(parserJson, HawkingParser::class.java)
        }
    ...
    }

Step 3: Create and persist the model:

    private fun createAndPersistModel() {
        dateTimeParser = HawkingTimeParser()
        parserConfig = HawkingConfiguration()
        parser = HawkingParser() // the class I created to parse strings

        parserConfig!!.dateFormat = "mm/dd/yyyy"
        val gs = Gson()

        GlobalScope.launch {
            async(Dispatchers.IO) {
                parser?.initParser(dateTimeParser, parserConfig)

                application.getSharedPreferences(Constants.SHARED_PREFERENCES_MODEL_NAME, Context.MODE_PRIVATE)
                    .edit()
                    .putString("dateTimeParser", gs.toJson(dateTimeParser))
                    .putString("parserConfig", gs.toJson(parserConfig))
                    .putString("parser", gs.toJson(parser))
                    .apply()
            }
        }
    }

And in HawkingParser.java, my code:

    public void initParser(HawkingTimeParser dateTimeParser, HawkingConfiguration parserConfig) {
        Date referenceDate = new Date();
        dateTimeParser.parse("Meeting tomorrow at 2pm", referenceDate, parserConfig, "eng");
    }

I would have thought that once the model is loaded once, I could save the HawkingTimeParser object and continuously use that each time the app is opened without having to continuously "warm up the model".

I'm looking in HawkingTimeParser.java and it's also not explicitly clear where the model is being warmed up -- i'm guessing it's in the abstractLanguage.predict(...) call? What if I were to create an instance of abstractLanguage in a separate method, pass it into HawkingTimeParser.parse(...), and write abstractLanguage to shared preferences to avoid delays?

    public DatesFound parse(String inputSentence, Date referenceDate, HawkingConfiguration config, String lang) {
        Configuration configuration = new Configuration(config);
        ConfigurationConstants.setConfiguration(configuration);
        List<ParserOutput> parserOutputs = new ArrayList<>();
        List<DateGroup> dateGroups = new ArrayList<>();
        DatesFound datesFound = new DatesFound();

        AbstractLanguage abstractLanguage = LanguageFactory.getLanguageImpl(lang);
        assert abstractLanguage != null;
        List<DateTimeProperties> dateList = abstractLanguage.predict(inputSentence, referenceDate, config);
        for (DateTimeProperties date : dateList) {
            parserOutputs.add(date.getParserOutput());
            dateGroups.add(date.getDateGroup());
        }
        parserOutputs = DateTimeProperties.addDefaultTime(parserOutputs, config.getDayhourStart(), config.getDayhourEnd());
        datesFound.setParserOutputs(parserOutputs);
        datesFound.setDateGroups(dateGroups);
        LOGGER.info(datesFound.toString());
        return DateTimeProperties.emptyDatesRemover(datesFound);
    }

from hawking.

evanlaf avatar evanlaf commented on August 17, 2024

@ajar19 Hi, just wanted to bump the question above. Thanks

from hawking.

ajar19 avatar ajar19 commented on August 17, 2024

I'm sorry, @evanlaf, for not getting back to you sooner. What you raised was a valid one. We'll add the option for users to load the model independently. Currently, the model loading process is deeply integrated into our system. We will write a util where users can give the model object and sentence together to make a prediction.

from hawking.

evanlaf avatar evanlaf commented on August 17, 2024

No worries, I ended up going the original workaround you suggested (warming up the model with a test sentence), since I couldn't get the shared preferences to work. I'll keep an eye out for that util when it's created. Thanks!

from hawking.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.