Package: clf-inference-intelcomp
Inference system for hierarchical text classificationAllows using a cascade of text classifiers based on a given hierarchy of language models.This python package has been developed for the IntelComp project, as part of deliverable 3.4.Setuppip install clf-inference-intelcompUsageToy example commands:fromclf_inference_intelcompimportClassificationclf=Classification()res=clf.classify(taxonomy="ipc",text="This is a test")clf.classify("fos","This is another test")Output:Running IPC classification at level 0... (model: intelcomp/ipc_level0)
Running IPC classification at level 1... (model: intelcomp/ipc_level1_G)
####### LEVEL 0:
                - Class: G
                - Full class name:  PHYSICS
                - Confidence score: 0.9405
####### LEVEL 1:
                - Class: 09
                - Full class name:  EDUCATING; CRYPTOGRAPHY; DISPLAY; ADVERTISING; SEALS
                - Confidence score: 0.3386


Running FOS classification at level 0... (model: intelcomp/fos_level0)
####### LEVEL 0:
                - Class: Medicine
                - Full class name:  None
                - Confidence score: 0.5535
{0: ('Medicine', None, 0.5535141229629517)}As it can be seen, theclassify()method returns a dictionary object with the taxonomy levels as keys,
where the values are triplets that contain the classification output.Additionally, the methodclassify_batch()can be used in a similar manner to perform classification by batches,
giving it a list of texts rather than a single string to classify. Typehelp(clf.classify_batch)for more details.Customizable attributesAfter instantiating an object from the Classification class, you can access the following attributes:WORKING_DIR: Location where the package files are located. Change only if you want to read YAML files from somewhere else.CACHE_DIR: Path where models will be cached for faster inference in subsequent calls. Defaults to~/.cache/huggingface/intelcomp.DEVICE: Either "cuda" or "cpu", based on the availability of GPUs.models: Dictionary with the paths to the different models, hierarchically organized.classes: Dictionary to map class codes to class names, hierarchically organized as well.avail_taxonomies: List of taxonomies available indata/models.yaml. Notice that this is a private attribute that
cannot be directly edited by the user, although it can be updated after modifying the YAML files.CachingBy default, models are downloaded on-the-fly at inference time. This means that whenever a model is used for the first
time, it will have to be downloaded first. In subsequent calls, inference will be faster since the model will already be
loaded in memory. For a smooth and uninterrupted inference, it is possible to load all models at once (or only the ones
belonging to a specific taxonomy) with the functioncache_models(). By doing this, the desired models will be cached
in the directory specified by the class attributeCACHE_DIR, which can be modified if needed.For instance, to classify models according to the IPC taxonomy, all IPC models can be loaded beforehand as follows:fromclf_inference_intelcompimportClassificationclf=Classification()clf.CACHE_DIR="~/Downloads/intelcomp/models"clf.cache_models("ipc")res=clf.classify("ipc","This will not wait for model downloads.")Similarly, in order to load ALL available models, no matter the taxonomy, simply callclf.cache_models()with no arguments.Adding/Removing new taxonomiesTo add and/or remove new taxonomies, the YAML files within thedatadirectory must be updated accordingly.
It is important to always follow the original structure, which is showcased in the next section.The Classification object will be initialized based on the YAML files content at creation time, but if such files are
modified afterwards, it is possible to update the taxonomy details by simply calling theupdate_taxonomies()method.fromclf_inference_intelcompimportClassificationclf=Classification()print(clf.avail_taxonomies)# ['fos', 'ipc', 'nace2']# YAML FILES BEING MODIFIED...clf.update_taxonomies()print(clf.avail_taxonomies)# ['fos', 'ipc', 'nace2', 'NEW_TAXONOMY']YAML filesThedatadirectory contains 2 YAML files that can be edited by the user to add new taxonomies:models.yaml:Lists the models to be used for each taxonomy, with a different indentation for each of its levels.Can either contain the names of models from the HuggingFace Hub or paths to local repositories.The first level (i.e. 0) of any taxonomy only contains a single model, which would be the root node of the
classification tree, while deeper levels have a list of models for each possible outcome from the prior level.
This is why, for the non-zero levels, it is important to make sure that the model key (value preceding ":")
matches some label from the previous model in the chain.classes.yaml:Lists the possible outputs of all classifiers frommodels.yaml, following the same format.It basically maps class codes to their corresponding names, for a better interpretability of the results.Note:It is important to ensure that the class names (keys of inner dictionaries) are read as strings.The reason is that some taxonomies (e.g. IPC) have numeric codes that are preceded by a number zero (e.g. "01").In these cases, YAML reads the key as an integer and, thus, the preceding zero gets lost.
The library makes sure to convert all keys to strings for consistency, but these specific cases with leading zeros
are hard to handle since there is no way to tell if there was a zero or not in the original file.In order to prevent that, please make sure to surround this kind of numeric codes with double quotes.ExamplesBelow are the default YAML files, for reference. Notice how no quotation marks are required at all inmodels.yaml,
since none of the taxonomies contains "problematic" codes and the function used to process YAML files already reads all
keys as strings.# data/models.yamlfos:0:intelcomp/fos_level0ipc:0:intelcomp/ipc_level01:A:intelcomp/ipc_level1_AB:intelcomp/ipc_level1_BC:intelcomp/ipc_level1_CD:intelcomp/ipc_level1_DE:intelcomp/ipc_level1_EF:intelcomp/ipc_level1_FG:intelcomp/ipc_level1_GH:intelcomp/ipc_level1_Hnace2:0:intelcomp/nace2_level01:20:intelcomp/nace2_level1_2025:intelcomp/nace2_level1_2526:intelcomp/nace2_level1_2627:intelcomp/nace2_level1_2728:intelcomp/nace2_level1_2829:intelcomp/nace2_level1_2942:intelcomp/nace2_level1_42#data/classes.yamlfos:0:0:ART1:BIOLOGY2:BUSINESS3:CHEMISTRY4:COMPUTER SCIENCE5:ECONOMICS6:ENGINEERING7:ENVIRONMENTAL SCIENCE8:GEOGRAPHY9:GEOLOGY10:HISTORY11:MATERIALS SCIENCE12:MATHEMATICS13:MEDICINE14:PHILOSOPHY15:PHYSICS16:POLITICAL SCIENCE17:PSYCHOLOGY18:SOCIOLOGYipc:0:A:HUMAN NECESSITIESB:PERFORMING OPERATIONS, TRANSPORTINGC:CHEMISTRY, METALLURGYD:TEXTILES, PAPERE:FIXED CONSTRUCTIONSF:MECHANICAL ENGINEERING, LIGHTING, HEATING, WEAPONSG:PHYSICSH:ELECTRICITY1:A:"01":AGRICULTURE; FORESTRY; ANIMAL HUSBANDRY; HUNTING; TRAPPING; FISHING"21":BAKING; EQUIPMENT FOR MAKING OR PROCESSING DOUGHS; DOUGHS FOR BAKING [2006.01]"22":BUTCHERING; MEAT TREATMENT; PROCESSING POULTRY OR FISH"23":FOODS OR FOODSTUFFS; TREATMENT THEREOF, NOT COVERED BY OTHER CLASSES"24":TOBACCO; CIGARS; CIGARETTES; SIMULATED SMOKING DEVICES; SMOKERS' REQUISITES"41":WEARING APPAREL"42":HEADWEAR"43":FOOTWEAR"44":HABERDASHERY; JEWELLERY"45":HAND OR TRAVELLING ARTICLES"46":BRUSHWARE"47":FURNITURE; DOMESTIC ARTICLES OR APPLIANCES; COFFEE MILLS; SPICE MILLS; SUCTION CLEANERS IN GENERAL"61":MEDICAL OR VETERINARY SCIENCE; HYGIENE"62":LIFE-SAVING; FIRE-FIGHTING"63":SPORTS; GAMES; AMUSEMENTS"99":SUBJECT MATTER NOT OTHERWISE PROVIDED FOR IN THIS SECTION [2006.01]B:"01":PHYSICAL OR CHEMICAL PROCESSES OR APPARATUS IN GENERAL"02":CRUSHING, PULVERISING, OR DISINTEGRATING; PREPARATORY TREATMENT OF GRAIN FOR MILLING"03":SEPARATION OF SOLID MATERIALS USING LIQUIDS OR USING PNEUMATIC TABLES OR JIGS; MAGNETIC OR ELECTROSTATIC SEPARATION OF SOLID MATERIALS FROM SOLID MATERIALS OR FLUIDS; SEPARATION BY HIGH-VOLTAGE ELECTRIC FIELDS [5]"04":CENTRIFUGAL APPARATUS OR MACHINES FOR CARRYING-OUT PHYSICAL OR CHEMICAL PROCESSES"05":SPRAYING OR ATOMISING IN GENERAL; APPLYING FLUENT MATERIALS TO SURFACES, IN GENERAL [2]"06":GENERATING OR TRANSMITTING MECHANICAL VIBRATIONS IN GENERAL"07":SEPARATING SOLIDS FROM SOLIDS; SORTING"08":CLEANING"09":DISPOSAL OF SOLID WASTE; RECLAMATION OF CONTAMINATED SOIL [6]"21":MECHANICAL METAL-WORKING WITHOUT ESSENTIALLY REMOVING MATERIAL; PUNCHING METAL"22":CASTING; POWDER METALLURGY"23":MACHINE TOOLS; METAL-WORKING NOT OTHERWISE PROVIDED FOR"24":GRINDING; POLISHING"25":HAND TOOLS; PORTABLE POWER-DRIVEN TOOLS; HANDLES FOR HAND IMPLEMENTS; WORKSHOP EQUIPMENT; MANIPULATORS"26":HAND CUTTING TOOLS; CUTTING; SEVERING"27":WORKING OR PRESERVING WOOD OR SIMILAR MATERIAL; NAILING OR STAPLING MACHINES IN GENERAL"28":WORKING CEMENT, CLAY, OR STONE"29":WORKING OF PLASTICS; WORKING OF SUBSTANCES IN A PLASTIC STATE IN GENERAL"30":PRESSES"31":MAKING ARTICLES OF PAPER, CARDBOARD OR MATERIAL WORKED IN A MANNER ANALOGOUS TO PAPER; WORKING PAPER, CARDBOARD OR MATERIAL WORKED IN A MANNER ANALOGOUS TO PAPER"32":LAYERED PRODUCTS"33":ADDITIVE MANUFACTURING TECHNOLOGY [2015.01]"41":PRINTING; LINING MACHINES; TYPEWRITERS; STAMPS [4]"42":BOOKBINDING; ALBUMS; FILES; SPECIAL PRINTED MATTER"43":WRITING OR DRAWING IMPLEMENTS; BUREAU ACCESSORIES"44":DECORATIVE ARTS"60":VEHICLES IN GENERAL"61":RAILWAYS"62":LAND VEHICLES FOR TRAVELLING OTHERWISE THAN ON RAILS"63":SHIPS OR OTHER WATERBORNE VESSELS; RELATED EQUIPMENT"64":AIRCRAFT; AVIATION; COSMONAUTICS"65":CONVEYING; PACKING; STORING; HANDLING THIN OR FILAMENTARY MATERIAL"66":HOISTING; LIFTING; HAULING"67":OPENING OR CLOSING BOTTLES, JARS OR SIMILAR CONTAINERS; LIQUID HANDLING"68":SADDLERY; UPHOLSTERY"81":MICROSTRUCTURAL TECHNOLOGY [7]"82":NANOTECHNOLOGY [7]"99":SUBJECT MATTER NOT OTHERWISE PROVIDED FOR IN THIS SECTION [2006.01]C:"01":INORGANIC CHEMISTRY"02":TREATMENT OF WATER, WASTE WATER, SEWAGE, OR SLUDGE"03":GLASS; MINERAL OR SLAG WOOL"04":CEMENTS; CONCRETE; ARTIFICIAL STONE; CERAMICS; REFRACTORIES [4]"05":FERTILISERS; MANUFACTURE THEREOF [4]"06":EXPLOSIVES; MATCHES"07":ORGANIC CHEMISTRY [2]"08":ORGANIC MACROMOLECULAR COMPOUNDS; THEIR PREPARATION OR CHEMICAL WORKING-UP; COMPOSITIONS BASED THEREON"09":DYES; PAINTS; POLISHES; NATURAL RESINS; ADHESIVES; COMPOSITIONS NOT OTHERWISE PROVIDED FOR; APPLICATIONS OF MATERIALS NOT OTHERWISE PROVIDED FOR"10":PETROLEUM, GAS OR COKE INDUSTRIES; TECHNICAL GASES CONTAINING CARBON MONOXIDE; FUELS; LUBRICANTS; PEAT"11":ANIMAL OR VEGETABLE OILS, FATS, FATTY SUBSTANCES OR WAXES; FATTY ACIDS THEREFROM; DETERGENTS; CANDLES"12":BIOCHEMISTRY; BEER; SPIRITS; WINE; VINEGAR; MICROBIOLOGY; ENZYMOLOGY; MUTATION OR GENETIC ENGINEERING"13":SUGAR INDUSTRY [4]"14":SKINS; HIDES; PELTS OR LEATHER"21":METALLURGY OF IRON"22":METALLURGY; FERROUS OR NON-FERROUS ALLOYS; TREATMENT OF ALLOYS OR NON-FERROUS METALS"23":COATING METALLIC MATERIAL; COATING MATERIAL WITH METALLIC MATERIAL; CHEMICAL SURFACE TREATMENT; DIFFUSION TREATMENT OF METALLIC MATERIAL; COATING BY VACUUM EVAPORATION, BY SPUTTERING, BY ION IMPLANTATION OR BY CHEMICAL VAPOUR DEPOSITION, IN GENERAL; INHIBITING CORROSION OF METALLIC MATERIAL OR INCRUSTATION IN GENERAL [2]"25":ELECTROLYTIC OR ELECTROPHORETIC PROCESSES; APPARATUS THEREFOR [4]"30":CRYSTAL GROWTH [3]"40":COMBINATORIAL TECHNOLOGY [2006.01]"99":SUBJECT MATTER NOT OTHERWISE PROVIDED FOR IN THIS SECTION [2006.01]D:"01":NATURAL OR MAN-MADE THREADS OR FIBRES; SPINNING"02":YARNS; MECHANICAL FINISHING OF YARNS OR ROPES; WARPING OR BEAMING"03":WEAVING"04":BRAIDING; LACE-MAKING; KNITTING; TRIMMINGS; NON-WOVEN FABRICS"05":SEWING; EMBROIDERING; TUFTING"06":TREATMENT OF TEXTILES OR THE LIKE; LAUNDERING; FLEXIBLE MATERIALS NOT OTHERWISE PROVIDED FOR"07":ROPES; CABLES OTHER THAN ELECTRIC"21":PAPER-MAKING; PRODUCTION OF CELLULOSE"99":SUBJECT MATTER NOT OTHERWISE PROVIDED FOR IN THIS SECTION [2006.01]E:"01":CONSTRUCTION OF ROADS, RAILWAYS, OR BRIDGES"02":HYDRAULIC ENGINEERING; FOUNDATIONS; SOIL-SHIFTING"03":WATER SUPPLY; SEWERAGE"04":BUILDING"05":LOCKS; KEYS; WINDOW OR DOOR FITTINGS; SAFES"06":DOORS, WINDOWS, SHUTTERS, OR ROLLER BLINDS, IN GENERAL; LADDERS"21":EARTH OR ROCK DRILLING; MINING"99":SUBJECT MATTER NOT OTHERWISE PROVIDED FOR IN THIS SECTION [2006.01]F:"01":MACHINES OR ENGINES IN GENERAL; ENGINE PLANTS IN GENERAL; STEAM ENGINES"02":COMBUSTION ENGINES; HOT-GAS OR COMBUSTION-PRODUCT ENGINE PLANTS"03":MACHINES OR ENGINES FOR LIQUIDS; WIND, SPRING, OR WEIGHT MOTORS; PRODUCING MECHANICAL POWER OR A REACTIVE PROPULSIVE THRUST, NOT OTHERWISE PROVIDED FOR"04":POSITIVE-DISPLACEMENT MACHINES FOR LIQUIDS; PUMPS FOR LIQUIDS OR ELASTIC FLUIDS"15":FLUID-PRESSURE ACTUATORS; HYDRAULICS OR PNEUMATICS IN GENERAL"16":ENGINEERING ELEMENTS OR UNITS; GENERAL MEASURES FOR PRODUCING AND MAINTAINING EFFECTIVE FUNCTIONING OF MACHINES OR INSTALLATIONS; THERMAL INSULATION IN GENERAL"17":STORING OR DISTRIBUTING GASES OR LIQUIDS"21":LIGHTING"22":STEAM GENERATION"23":COMBUSTION APPARATUS; COMBUSTION PROCESSES"24":HEATING; RANGES; VENTILATING"25":REFRIGERATION OR COOLING; COMBINED HEATING AND REFRIGERATION SYSTEMS; HEAT PUMP SYSTEMS; MANUFACTURE OR STORAGE OF ICE; LIQUEFACTION OR SOLIDIFICATION OF GASES"26":DRYING"27":FURNACES; KILNS, OVENS OR RETORTS [4]"28":HEAT EXCHANGE IN GENERAL"41":WEAPONS"42":AMMUNITION; BLASTING"99":SUBJECT MATTER NOT OTHERWISE PROVIDED FOR IN THIS SECTION [2006.01]G:"01":MEASURING; TESTING"02":OPTICS"03":PHOTOGRAPHY; CINEMATOGRAPHY; ANALOGOUS TECHNIQUES USING WAVES OTHER THAN OPTICAL WAVES; ELECTROGRAPHY; HOLOGRAPHY [4]"04":HOROLOGY"05":CONTROLLING; REGULATING"06":COMPUTING; CALCULATING OR COUNTING"07":CHECKING-DEVICES"08":SIGNALLING"09":EDUCATING; CRYPTOGRAPHY; DISPLAY; ADVERTISING; SEALS"10":MUSICAL INSTRUMENTS; ACOUSTICS"11":INFORMATION STORAGE"12":INSTRUMENT DETAILS"16":INFORMATION AND COMMUNICATION TECHNOLOGY [ICT] SPECIALLY ADAPTED FOR SPECIFIC APPLICATION FIELDS [2018.01]"21":NUCLEAR PHYSICS; NUCLEAR ENGINEERING"99":SUBJECT MATTER NOT OTHERWISE PROVIDED FOR IN THIS SECTION [2006.01]H:"01":BASIC ELECTRIC ELEMENTS"02":GENERATION, CONVERSION, OR DISTRIBUTION OF ELECTRIC POWER"03":BASIC ELECTRONIC CIRCUITRY"04":ELECTRIC COMMUNICATION TECHNIQUE"05":ELECTRIC TECHNIQUES NOT OTHERWISE PROVIDED FOR"99":SUBJECT MATTER NOT OTHERWISE PROVIDED FOR IN THIS SECTION [2006.01]nace2:0:10:MANUFACTURE OF FOOD PRODUCTS11:MANUFACTURE OF BEVERAGES12:MANUFACTURE OF TOBACCO PRODUCTS13:MANUFACTURE OF TEXTILES14:MANUFACTURE OF WEARING APPAREL15:MANUFACTURE OF LEATHER AND RELATED PRODUCTS16:MANUFACTURE OF WOOD AND OF PRODUCTS OF WOOD AND CORK, EXCEPT FURNITURE; MANUFACTURE OF ARTICLES OF STRAW AND PLAITING MATERIALS17:MANUFACTURE OF PAPER AND PAPER PRODUCTS18:PRINTING AND REPRODUCTION OF RECORDED MEDIA19:MANUFACTURE OF COKE AND REFINED PETROLEUM PRODUCTS20:MANUFACTURE OF CHEMICALS AND CHEMICAL PRODUCTS21:MANUFACTURE OF BASIC PHARMACEUTICAL PRODUCTS AND PHARMACEUTICAL PREPARATIONS22:MANUFACTURE OF RUBBER AND PLASTIC PRODUCTS23:MANUFACTURE OF OTHER NON-METALLIC MINERAL PRODUCTS24:MANUFACTURE OF BASIC METALS25:MANUFACTURE OF FABRICATED METAL PRODUCTS, EXCEPT MACHINERY AND EQUIPMENT26:MANUFACTURE OF COMPUTER, ELECTRONIC AND OPTICAL PRODUCTS27:MANUFACTURE OF ELECTRICAL EQUIPMENT28:MANUFACTURE OF MACHINERY AND EQUIPMENT N.E.C.29:MANUFACTURE OF MOTOR VEHICLES, TRAILERS AND SEMI-TRAILERS30:MANUFACTURE OF OTHER TRANSPORT EQUIPMENT31:MANUFACTURE OF FURNITURE32:OTHER MANUFACTURING42:CIVIL ENGINEERING43:SPECIALISED CONSTRUCTION ACTIVITIES62:COMPUTER PROGRAMMING, CONSULTANCY AND RELATED ACTIVITIES1:20:1:MANUFACTURE OF BASIC CHEMICALS, FERTILISERS AND NITROGEN COMPOUNDS, PLASTICS AND SYNTHETIC RUBBER IN PRIMARY FORMS2:MANUFACTURE OF PESTICIDES AND OTHER AGROCHEMICAL PRODUCTS3:MANUFACTURE OF PAINTS, VARNISHES AND SIMILAR COATINGS, PRINTING INK AND MASTICS4:MANUFACTURE OF SOAP AND DETERGENTS, CLEANING AND POLISHING PREPARATIONS, PERFUMES AND TOILET PREPARATIONS5:MANUFACTURE OF OTHER CHEMICAL PRODUCTS6:MANUFACTURE OF MAN-MADE FIBRES25:1:MANUFACTURE OF STRUCTURAL METAL PRODUCTS2:MANUFACTURE OF TANKS, RESERVOIRS AND CONTAINERS OF METAL3:MANUFACTURE OF STEAM GENERATORS, EXCEPT CENTRAL HEATING HOT WATER BOILERS4:MANUFACTURE OF WEAPONS AND AMMUNITION5:FORGING, PRESSING, STAMPING AND ROLL-FORMING OF METAL; POWDER METALLURGY6:TREATMENT AND COATING OF METALS; MACHINING7:MANUFACTURE OF CUTLERY, TOOLS AND GENERAL HARDWARE9:MANUFACTURE OF OTHER FABRICATED METAL PRODUCTS26:1:MANUFACTURE OF ELECTRONIC COMPONENTS AND BOARDS2:MANUFACTURE OF COMPUTERS AND PERIPHERAL EQUIPMENT3:MANUFACTURE OF COMPUTERS AND PERIPHERAL EQUIPMENT4:MANUFACTURE OF CONSUMER ELECTRONICS5:MANUFACTURE OF INSTRUMENTS AND APPLIANCES FOR MEASURING, TESTING AND NAVIGATION; WATCHES AND CLOCKS6:MANUFACTURE OF IRRADIATION, ELECTROMEDICAL AND ELECTROTHERAPEUTIC EQUIPMENT7:MANUFACTURE OF OPTICAL INSTRUMENTS AND PHOTOGRAPHIC EQUIPMENT8:MANUFACTURE OF MAGNETIC AND OPTICAL MEDIA27:1:MANUFACTURE OF ELECTRIC MOTORS, GENERATORS, TRANSFORMERS AND ELECTRICITY DISTRIBUTION AND CONTROL APPARATUS2:MANUFACTURE OF BATTERIES AND ACCUMULATORS3:MANUFACTURE OF WIRING AND WIRING DEVICES4:MANUFACTURE OF ELECTRIC LIGHTING EQUIPMENT5:MANUFACTURE OF DOMESTIC APPLIANCES9:MANUFACTURE OF OTHER ELECTRICAL EQUIPMENT28:1:MANUFACTURE OF GENERAL — PURPOSE MACHINERY2:MANUFACTURE OF OTHER GENERAL-PURPOSE MACHINERY3:MANUFACTURE OF AGRICULTURAL AND FORESTRY MACHINERY4:MANUFACTURE OF METAL FORMING MACHINERY AND MACHINE TOOLS9:MANUFACTURE OF OTHER SPECIAL-PURPOSE MACHINERY29:1:MANUFACTURE OF MOTOR VEHICLES3:MANUFACTURE OF PARTS AND ACCESSORIES FOR MOTOR VEHICLES42:2:CONSTRUCTION OF UTILITY PROJECTS9:CONSTRUCTION OF OTHER CIVIL ENGINEERING PROJECTS