Running alternat in 5 minutes¶
Alternat can run in the following mode:
Application Mode: In application mode, users use Command Line Interface (CLI) to run the alternat. We have created a sample app.py which will run the application via CLI command.
Library Mode: In Library mode, users install alternat from pip (python package installer) and can use in new or existing application via the library.
Service Mode: In Service mode, REST API endpoint is exposed where a POST request can be submitted with a JSON request to get the alt-text generated by alternat.
Below is the 5-minute guide to run alternat in the modes described above:
Application Mode:
Collection:
Use case : collect and store images from a URL and store them in a folder
python app.py collect --url="https://page_url" --output-dir-path="sample/images/test"
Generation:
Use case: generate alt-text for images in input folder and save result in a directory)
python app.py generate --input-dir-path="sample/images_with_text" --output-dir-path="results"
Use case: generate alt-text for single image and save result json in directory
python app.py generate --input-image-file-path="sample/images_with_text/sample1.png" --output-dir-path="results"
Library Mode:
Collection
# import the alternat library from alternat.collection import Collector # instantiate the collector collector = Collector() # Download images from url and saves image files in output_dir_path # Optional parameters, download_recursive if True crawls whole site mentioned in # url by visiting each link recursively and downloads images # collect_using_apify in future more crawlers will be supported this parameter # ensures that apify crawler is used. collector.process(url, output_dir_path, download_recursive, collect_using_apify)
Generation:
# import the alternat library from alternat.generation import Generator # instantiate the generator generator = Generator() # generate alt text from file (file at location sample/images_with_text/sample1.png # and results saved at location folder results) generator.generate_alt_text_from_file("sample/images_with_text/sample1.png", "results") # OR # generate alt text from base64 image generator.generate_alt_text_from_base64(base64_image_string)
Service Mode:
In this mode, alternat exposes web API to generate alt-text for an image. Alternat use python based API framework - fastAPI to create APIs. fastAPI comes with a lightweight python server uvicorn which is used to expose the API. To start the server :
# Go to api folder cd api # run this command to start the service uvicorn message_processor:app --port 8080 --host 0.0.0.0 --reload
The following web APIs are available:
# send a post request with base64 image to the Web Server URL: http://localhost:8080/generate_text_base64 body: { base64: “base64_image_str”} # send a post request with URL of the image to the Web Server URL: http://localhost:8080/generate_text_url body: { url: “url_of_the_image”}