Integrating Custom build – LLM Models with Hugging face and Snowflake

Overview:

LLMs (Large Language Models) are undeniably powerful – that’s a fact. But the real question is: Are they truly useful for our complex, tailor-made tasks? The answer is a resounding yes – but only if we can train them to meet our custom needs.

In this blog, we’ll explore how to harness the capabilities of LLMs by aligning them with your specific business or analytical requirements. More importantly, we’ll walk through how to combine the strengths of custom-trained LLMs, Hugging Face, and Snowflake to build solutions that not only understand your data but also derive meaningful insights from it securely and at scale using Snowflake’s robust data governance and access controls.

In this example, we’ll utilize Google’s BERT base uncased model and fine-tune it with our custom dataset to create an intent classifier – a model that understands what the user is really asking. This becomes especially powerful when building intelligent agents that interact with data-rich domains like weather forecasting.

🌦️ Real-World Example: Weather Assistant

Let’s say you’re building a conversational assistant for a weather analytics platform. Users might ask:

  • “Will it rain in New York this weekend?”
  • “Has there been an increase in average temperatures over the past decade?”
  • “What’s the usual humidity level in Mumbai during monsoon?”
  • “Is today’s weather unusual compared to the average?”

These questions fall into two broad categories:

  1. Real-time data requests (e.g., “Will it rain today?”)
  2. Analytical insights (e.g., “How does today compare to historical trends?”)

By training an intent classifier, we can teach the model to distinguish between queries that need live weather feeds and those that require deep historical or trend-based analysis. This enables the backend to route the request accordingly – either fetching real-time data via APIs or triggering a Snowflake query over historical datasets., this is one of the examples which we are dealing today but there is plethora of other ways to utilize this intent classifier based on our requirement.

Pre-Requisites:

We plan to leverage Snowflake’s ML Model Registry to seamlessly reference and store the trained models from Hugging Face within a secure, scalable environment.

To get started, there are two key prerequisites:

  1. Snowflake Account – With access to Snowflake’s ML Model Registry.
  2. Hugging Face Account – To access, customize, and fine-tune pre-trained models like BERT for your specific use case.

This setup allows us to host powerful NLP models without leaving the Snowflake ecosystem—ensuring enterprise-grade security, data governance, and performance.

Training the Model and pushing to Hugging Face:A screenshot of a computer

AI-generated content may be incorrect.

Bring in essential Python libraries for data handling, model training, and evaluation.

In this step, you set up the BERT model that will be used for classifying your custom dataset into two categories — “Data” and “Insights”. The process begins by specifying the path to a pretrained BERT model. Then, the tokenizer associated with this model is loaded, which is necessary for converting raw text into token IDs that the model can understand.

Next, label mappings are defined to connect numeric IDs with human-readable class names. These mappings are essential for both interpreting the model’s predictions and preparing the dataset labels. Finally, the actual classification model is initialized using AutoModelForSequenceClassification, with the number of labels and the label mappings passed in. This configures the BERT model to perform a classification task with your specific label structure.

This part focuses on optimizing the model for faster and more efficient training. It starts by freezing all model parameters, which means they won’t be updated during training — making the model lighter. However, the “pooler” layers are kept trainable, which allows for some learning to happen without retraining the entire model. This is especially useful when working with limited computing power or small datasets.

After that, a preprocess_function is defined to tokenize input text using the BERT tokenizer. The dataset is then mapped with this function in a batched manner, preparing it for model training. Finally, a DataCollatorWithPadding is used to dynamically pad the input sequences during training to the longest sequence in each batch, optimizing memory usage.

The model is tested using sample text questions. The text is tokenized, passed through the model, and predictions are made. These predictions are then mapped to readable labels like “Insights” or “Data” and printed.
After testing, the model is pushed to Hugging Face (model.push_to_hub) so it can be shared, reused, and hosted (for example, in Snowflake).

Checkout the Git hub location for the python Notebook,
Bert-Intent-Classifier/Bert-Classifier.ipynb

Registering the Pushed Model to Snowflake Model Registry:

After completing the setup process, the Hugging Face model that was pushed to the registry is loaded into the environment, along with its corresponding tokenizer. The model is then configured with label mappings (id2label and label2id) to ensure that its numerical prediction outputs are properly interpreted as meaningful labels during inference. This step is crucial for maintaining consistency between training and deployment. Once the model and tokenizer are ready, they are combined into a text-classification pipeline using Hugging Face’s transformers library. This pipeline abstracts away the complexity of preprocessing, model inference, and postprocessing, allowing for seamless text classification in subsequent steps. The result is an easily accessible tool for feeding in text inputs and retrieving classified outputs based on the model’s trained behavior.

In this section, the notebook outlines how to connect to a Snowflake instance, register a custom model, and test it. First, connection parameters such as account, user, password, and role are loaded from environment variables and used to create a session. Then, using the Snowflake model registry, the custom classifier model is logged with necessary dependencies like tokenizers, transformers, and pytorch. Finally, the model is tested by running it against example inputs. The results confirm that the registered model can correctly understand and respond to user queries based on the custom training.

After Registering the Model to Snowflake, we can call the model in snowflake to see the predictions for the data stored in snowflake.

Using the below code as an example,
–SELECT MODEL_NAME!__CALL__(‘’)

https://github.com/azar01j/Bert-Intent-Classifier/blob/0eb2bba6fe791d397a681a8936dd1f9f5a45a814/Registering-Model-Snowflake.ipynb

Conclusion:

By integrating the capabilities of Hugging Face for model development and Snowflake for secure, scalable deployment, organizations can efficiently align AI models with their operational needs. This approach not only enhances the relevance and accuracy of model outputs but also ensures that data governance, compliance, and security standards are upheld. As enterprises increasingly seek to derive actionable insights from complex datasets, the ability to deploy customized, domain-specific LLM solutions will be a key differentiator in driving innovation and achieving competitive advantage.

Referral Links:

GitHub Location :
azar01j/Bert-Intent-Classifier: This Repository Contains the code to Custom train a bert model



Leave a Reply