Giter VIP home page Giter VIP logo

kylecorry31 / ml4k-ai-extension Goto Github PK

View Code? Open in Web Editor NEW
19.0 10.0 15.0 5.13 MB

Use machine learning in AppInventor, with easy training using text, images, or numbers through the Machine Learning for Kids website.

License: MIT License

Java 68.77% Python 0.51% Makefile 0.52% Batchfile 0.54% Shell 0.77% HTML 0.94% JavaScript 27.94%
machine-learning ml4k appinventor appinventorextension aix text-classification classification classifier image-classification text-classifier

ml4k-ai-extension's Introduction

ML4K AppInventor Extension

Use machine learning in AppInventor, with easy training using text, images, or numbers through the Machine Learning for Kids website.

Download (.aix)

Example

Text classification

ML4K Text Example

Image classification

ML4K Image Example

Note: The ClassifyImage block takes the path to an image, which you can get from the Selected property of an ImagePicker.

Numbers classification

ML4K Number Example

Live examples

See example .aia projects in the examples directory of this repo (Created by Joe Mazzone). Look at the code blocks for where to add your API key (API key is not included), they are set in the click method - though you can set it anywhere you choose as long as it is before the classification occurs.

Installation

Download the latest extension file (.aix) from the releases page and follow section "2. How to use extensions components" of this website to add the extension to your App Inventor project.

Guide


If you received this extension from the ML4K website, your API key will be set for you and you don't need the block to set it - skip to step 1 and 2.


  1. After installing the extension, you need to get an API key, which can be obtained from Machine Learning for Kids. This API Key is not the IBM Watson API keys used to create your Machine Learning for Kids account. This key is created by Machine Learning for Kids and is specific to your project. It can be found on the App Inventor project page in the unique URL for your project.

       Or, the key can be found on the Python project page.

      Do not use the keys displayed in the images above. Use the keys from your project pages.

  1. Copy and paste the API Key into the ML4K component’s “Key” property on the Designer screen or use the "set Key" block on the Blocks screen. Note: API Key must be set before you can use any of the ML4K extension blocks for classification. If you choose to set the key using the “set Key” block, be sure to set the key in the Screen.Initialize event or any time before you use a classification method (purple block).

Set Key

  1. Classify the text, image, or numbers:
    a. If classifying text, use the "ClassifyText" block with the text to classify.

    b. If classifying images, use the "ClassifyImage" block with the image path to classify.

    c. If classifying numbers, use the "ClassifyNumbers" block with a list of numbers to classify.

  2. Use the "GotClassification" event block to retrieve the classification once it is completed.

  3. Use the "GotError" event block to retrieve any errors which occur during classification.

  4. Add data to your machine learning project with code:
    a. To add data to a text project use the "AddTextTrainingData" block, identifying the text data to add and label to add it to.

    b. To add data to an image project use the "AddImageTrainingData" block, identifying the image file to add and label to add it to.

    c. To add data to a numbers project use the "AddNumbersTrainingData" block, identifying the list of numbers and label to add it to.

  5. Use the "GetModelStatus" block to see if your model is ready to use or still in the process of training.

  6. Use the "GotStatus" event block to retrieve the status of the model.

    • statusCode 2 - "Ready" - The model is trained and ready to use.
    • statusCode 1 - "Training in progress" - The model is still training and cannot be used.
    • statusCode 0 - Something went wrong (or there isn't a model) - the 'message' variable will contain information on the issue.
  7. Use the "TrainNewModel" block to train your machine learning model with App Inventor.

    • Note: Using this block is optional for text or numbers projects, as models can be trained on the Machine Learning for Kids server from the "Learn & Test" page. However for Image projects, using this block is essential and must be used to create a model before an ML model can be used (because models are created and used on the mobile device)

Handling Errors

Upon an error, the "GotError" event block will be called with the error that occurred. Please use this event block fro debugging.

Building with preset API key

To build the extension, open a terminal and navigate to the release folder. Run the build_aix.py script, passing in the API key.

cd release
python build_aix.py <API KEY>

This will generate a ML4K.aix file which contains a preset API key.

To do this without the Python script, the file com.kylecorry.ml4k/assets/api.txt needs to be modified to have the API key in it. Then the whole folder (com.kylecorry.ml4k directory needs to be present in the top level of the zip file) needs to be zipped and renamed to have the .aix extension instead of .zip.

Building from source

Unix (Linux / Mac)

To build the extension from sources, you can use the makefile.

Requirements

  • make
  • ant
  • git

Building

make

This will create a directory called _build/dist which will contain the .aix file (without an API key).

Windows

To build the extension from sources, you can use the build.bat.

Requirements

  • ant
  • git

Building

.\build.bat

This will create a directory called _build/dist which will contain the .aix file (without an API key).

FAQ

The extension crashes while using an emulator: See this issue

License

This project is licensed under the MIT License.

Featured projects

Here are some community contributed projects which demonstrate awesome uses of the ML4K extension. All credits belong to the creators of these projects:

Credits

Contribute

Please feel free to contribute to this extension, or if you find an issue be sure to report it under issues.

ml4k-ai-extension's People

Contributors

dalelane avatar kylecorry31 avatar mrmazzone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ml4k-ai-extension's Issues

Bad response from server:404

Hello, I tried to follow some tricks from this post below, hence the reference.
I am trying to solve a similar issue but I cannot get it to work.
I am trying the example CirclesAndRectangles.aia
Actually I did everything that was needed on ML4K site, and got the URL for the extension. I understand from another issue that the API Key was included in the extension.
Anyway I added the API key in the Screen.Initialize block
But whatever I do I get "Bad response by server:404"
I did it on 2 different phones once with the companion and once with .apk, all with the same result.
What could I have done wrong?
Thanks


Hello Kyle, now it works!

I'm not sure if solution was new version, or problem was that I made so many tests and so many apps with different mi4k projects that my phone (two phones, actually) had charged in memory an incorrect version of extension. In case it is usefull, I tell you about the process

I have tested an app make by Dale Lane with my key, and at first It didn't work in my phones. I have installed this app in a third phone (with no other app with MI4K installed) ... and it worked!

I had to uninstall from my phones all different apps and versions I generated with app inventor and ML4K extension, I've reboot my phone and installed only one version (new extension?)....and it runned!

This is a project of my son and some friends, and I was helping them to solve the problem.

Thank you very much!

Originally posted by @gsebastia in #20 (comment)

how to get the API KEY ( error : API key isn't a machine learning for kids key )

Hello, i've try many things but i don't arrive to have an apikey who work.

Wheni'm going on : https://machinelearningforkids.co.uk/#!/teacher/apikeys
They say : no apiket need for images cmlassification
I've been on : https://cloud.ibm.com/
I've create an account and get an apikey like it's say here :
https://raw.githubusercontent.com/IBM/taxinomitis-docs/master/docs/pdf/machinelearningforkids-apikeys.pdf
With this apikey in app inventor :
the block error return the error : API key isn't a machine learning for kids key

What should i do ? and how
Thanks a lot for the extension

Feature Request: ML Model Training Blocks

With these newly implemented training blocks for Scratch 3 projects, teachers are beginning to imagine how useful this could be with App Inventor projects. Highly requested after CSTA conference workshop and presentation.
blocks

Nothing published with Python yet on ML for Kids site. Would need to work with Dale to implement and understand Web API.

Does image data need to be URLEncoded?

String dataStr = "{\"data\": " + "\"" + URLEncoder.encode(imageData, "UTF-8") + "\"}";

Since this is a POST request, I'm not sure if the image data needs to be URLEncoded, it may need to be JSONified though. The Python code shows a JSON object being passed to the Python requests.post method - look there to see if it is being encoded.

Message "Classification for this project is only available in the browser"

Hi,
I am going back to an old project I built for an App inventor workshop I am running soon and the app returns this message "class is only available in the browser".
I went back to the three steps to be sure the model was trained and updated. the key is still the same .
When I connected it through the AI companion I have the same message , including the fact that it showed the status =2 notification message that I had prepared.
I even went to import the extension again and needed to rebuild part of the app but got the same problem again.
Any idea of what's happening?
thanks
Bernard

Apk not traininig, but AI companion works fine

Hi,
I have an app inventor project with ml4k plugin and it works fine using AI companion connection.
But when I generate the apk, I get an error that said the model has not been trained. I've used "Train new model" block and I've programmed a delay after that to wait until the model has been trained
Could you help me?
Thank you

Error 403 while importing to App Inventor

Hi,

For the past few days, I've tried to import my Machine Learning for Kids model to App Inventor, but I get error 403:
image

I'm not sure why I get this error. This has never happened before. I tried with new models and creating new App inventor projects, but so far, no luck. Any suggestions would be appreciated.

Only one model out of three is trained

Hello, I have trained three separate image recognition models on machinelearningforkids.co.uk and put them in an app inventor app. However, when I try to classify an image, only one machine learning model is functional, and the other two displays a 'not trained' warning. The code for all three models are the same, and have been trained with the train new model block, but it just isn't working.

Support for sound projects

I think we should be able to get sound projects working, in the same way that we've got image projects working with TensorFlow.js in an embedded WebView.

I tried to lay the groundwork for that in the way I added image support, so it should... in theory... be an incremental step on that.

The bit I'm not sure about is whether the browser running in the WebView will get access to the device microphone (I didn't try doing that for images - using the native AppInventor camera support and throwing the data at the webview instead).

It will be much more difficult if we can't do that, as all of the audio encoding stuff is in the JavaScript library - we won't want to re-invent the audio encoding on the Android/Java side, so the feasibility of doing sound project support will come down to whether we can let a WebView play with the phone microphone.

Assuming that is possible, the rest should be easy.... I think?
Worth a try at least.

Classification for this project is only available in the browser error

It is a really pity to have this issue in this moment. Working with children (10-12 years) and everything worked fine.
Some features were removed from ML4K and using KEY it worked but today I thing major changes have been done and I get this error:

Classification for this project is only available in the browser error

I received same data I had sent but I don't get the classification I am requesting from the model.
Imagen de WhatsApp 2024-04-06 a las 21 39 44_3796aee6

I think this project is unsupported but I would appreciate if there is some workaround to fix it.

APP error 0 not trained

Hi, I have trained an image classificator in ml4kids and I have imported the model to app inventor. I have programmed the mobile app following your instructions, but when I try to use it, and classify an image I got the error "0: not trained". I have tested the model in ml4kids, scratch 3 and python and it works.
Also I have checked text models and they work properly.

App Inventor emulator issues

Unable to use this extension on App inventor with the Emulator. I can get it working as a regular .apk but receive error messages when trying to test with the emulator.

When using the example app CirclesAndRectangles

When emulator starts, receive error message:

'Runtime Error: Error from Companion: java.lang.NoSuchMethod.Error: com.google.appinventor.components.runtime.Form.openAssetForExtension'

When running the app through the emulator, receive error message:

'Runtime Error: invoke: no method named `ClassifyImage' in class java.lang.Boolean
Note: You will not see another error reported for 5 seconds.'

Any possible resolution?

AddNumbersTrainingData

Can you give an example to AddNumbersTrainingData?

When I try GotError gives

train
invalid data

Thanks

Image classification fails calling .ClassifyImage

Hello. I am trying with my first app with MI4K but I've never got a version that gives me back an image classification. I have tried different ways to send the image, but now the application fails. I don't know if the problem is the key, the way i send the image...

To simplify my tests, I leaved my project and I took the example CirclesAndRectangles, I created a project in machine learning for kids, I generated the model, and I copied the key from the url:

https://machinelearningforkids.co.uk/api/appinventor/b27f9f70-1694-11e9-b8e9-6f11f826e259e313c6de-ffb5-492e-8340-2390b8ae4ee4/extension

but when executing it on my phone it fails (see error message below) just like my project ... what am I doing wrong?

Thank you very much!

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/JsonParser;
at com.kylecorry.ml4k.ML4K$Classification.fromJson(ML4K.java:426)
at com.kylecorry.ml4k.ML4K$Classification.access$400(ML4K.java:414)
at com.kylecorry.ml4k.ML4K$1.run(ML4K.java:118)
at java.lang.Thread.run(Thread.java:760)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.gson.JsonParser" on path: DexPathList[[zip file "/data/app/appinventor.ai_gsebastia.eFAP_1_c-2/base.apk"],nativeLibraryDirectories=[/data/app/appinventor.ai_gsebastia.eFAP_1_c-2/lib/arm64, /system/lib64, /vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:74)
at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 4 more

many thanks

Presetting the API key

Is there a way that the API key can be read from the aix file, to save the need for students to use the API key block?

If the API key could be read from a properties/config file inside the aix file, then I could get ML4K to dynamically generate that (start from a default aix file of ML4K-AI-Extension, unzip it, modify the properties/config file with the API key, zip it back up, serve that patched aix file for download).

I think that'd be a helpful simplification... what do you think?

Update ML4K logo

The ML4K logo has changed, and the AppInventor extension should reflect that.

Requires Android Oreo or later

Tried running something on a second-hand Android device I was able to get hold of, and it crashed with:

12-09 17:03:16.756 11271 11443 E AndroidRuntime: FATAL EXCEPTION: Thread-8
12-09 17:03:16.756 11271 11443 E AndroidRuntime: Process: edu.mit.appinventor.aicompanion3, PID: 11271
12-09 17:03:16.756 11271 11443 E AndroidRuntime: java.lang.NoSuchMethodError: No virtual method toPath()Ljava/nio/file/Path; in class Ljava/io/File; or its super classes (declaration of 'java.io.File' appears in /system/framework/core-oj.jar)
12-09 17:03:16.756 11271 11443 E AndroidRuntime: 	at com.kylecorry.ml4k.ML4K.getImageData(ML4K.java:310)
12-09 17:03:16.756 11271 11443 E AndroidRuntime: 	at com.kylecorry.ml4k.ML4K.access$100(ML4K.java:39)
12-09 17:03:16.756 11271 11443 E AndroidRuntime: 	at com.kylecorry.ml4k.ML4K$1.run(ML4K.java:86)
12-09 17:03:16.756 11271 11443 E AndroidRuntime: 	at java.lang.Thread.run(Thread.java:761)
12-09 17:03:16.766   548  1332 W ActivityManager:   Force finishing activity edu.mit.appinventor.aicompanion3/.Screen1

According to threads like osiegmar/FastCSV#16 and https://bitbucket.org/ijabz/jaudiotagger/issues/113/cannot-run-under-android-could-not-find it's because that method isn't available on the version of Android that I have (Android 7.1).

I don't know if there is a way to code around this. If not, it might be worth a warning somewhere to say that the image block needs Android 8 or later.

Payload too large

@dalelane Sometimes the ML4K web service returns "Payload too large" when passing a large image file (taken directly from phone's camera). Tested on the classify endpoint

Crash in older Android version

This was sent to me by email by a user. I'm just passing it on, and haven't had a chance to attempt to recreate myself.

Attempts to use ML4K extension blocks crashed the App Inventor app, with the following:

E/AndroidRuntime(  283): FATAL EXCEPTION: Thread-17
E/AndroidRuntime(  283): java.lang.NoSuchMethodError: java.lang.String.isEmpty
E/AndroidRuntime(  283):        at com.kylecorry.ml4k.ML4K.setAPIKey(ML4K.java:48)
E/AndroidRuntime(  283):        at com.kylecorry.ml4k.ML4K.<init>(ML4K.java:39)
E/AndroidRuntime(  283):        at com.kylecorry.ml4k.ML4KComponent$2.run(ML4KComponent.java:88)
E/AndroidRuntime(  283):        at java.lang.Thread.run(Thread.java:1096)

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.