This FastAPI application serves a BERT-based model (bert-base-uncased) fine-tuned for sentence pair classification, determining whether two sentences are paraphrases.
- FastAPI for serving the model
- Tokenization using Hugging Face Transformers
- Inference using PyTorch
git clone https://github.com/JisnaP/Fine_tuning_pretrained_models.git
cd Fine_tuning_pretrained_modelspython -m venv venv
source venv/bin/activate pip install -r requirements.txt📌 Setup Instructions 1️⃣ Run Colab Notebook to Fine-tune and Save Model Before running the API, you must first run the Colab notebook to train and save the model parameters.
Open and run the Google Colab notebook: fine_tuning_pretrained_models_using_trainer_api_for_paraphrase_sentences.ipynb Save the trained model and download it. (You will have use these as weights in the model in app.py) Move model.pth to the models/ directory in your local repo.
uvicorn app:app --host 0.0.0.0 --port 8000- The API will now be available at:
http://127.0.0.1:8000 - You can test the API using the interactive Swagger UI at:
http://127.0.0.1:8000/docs
Send a POST request to /predict with a JSON payload containing two sentences:
{
"sentence1": "The weather is nice today.",
"sentence2": "It's a beautiful day outside."
}{
"paraphrase": true,
"confidence": 0.89
}paraphrase: truemeans the sentences are paraphrasesconfidence: 0.89indicates an 89% confidence level
This API uses the BERT base uncased (bert-base-uncased) model fine-tuned on a paraphrase detection dataset glue/mrpc.
If the model is not downloaded, it will be automatically fetched from Hugging Face.
This project is open-source and available under the MIT License.