Skjerdal/Pretrained_vs_OpenAI_QA
QA Model Comparison: Custom Model vs OpenAI API
This application compares the performance and response time of a custom Question Answering model against OpenAI's API. It allows you to visually see the difference in response quality and speed between a locally hosted model and an API call to a large language model.
Features
- Custom QA model inference using a pre-trained Transformer model
- OpenAI API integration for comparison
- Response time measurement for both models
- Side-by-side result comparison
- User-friendly Gradio interface
Setup Instructions
1. Clone the repository
git clone <repository-url>
cd <repository-directory>2. Install dependencies
pip install -r requirements.txt3. Set up your OpenAI API key
Copy the example environment file and add your OpenAI API key:
cp .env.example .envThen edit the .env file and replace your_openai_api_key_here with your actual OpenAI API key.
4. Download/prepare your custom model
Make sure your custom QA model is in the tf210_custom_qa_model directory with the following structure:
tf210_custom_qa_model/
├── config.json
├── qa_model_weights
└── tokenizer filesIf you don't have a pre-trained model, you can modify the code to use a model from the Hugging Face hub.
5. Run the application
python app.pyThe Gradio interface will start and provide a local URL (typically http://127.0.0.1:7860) where you can access the application.
Usage
- Enter or paste a context paragraph in the Context box
- Enter a question about the context in the Question box
- Check the "Use OpenAI API for comparison" box to compare with OpenAI's response
- Click Submit to see the results
- Compare the answers and response times from both models
Example
The application comes with an example about the SQuAD dataset that you can use to test it immediately.
Custom Model Details
The custom QA model is based on DistilBERT and fine-tuned for question answering tasks. It extracts the exact answer spans from the provided context that best answer the question.
Troubleshooting
Missing Model Files
If you encounter an error like "Error loading model", make sure:
- The
tf210_custom_qa_modeldirectory exists - All required files (config.json, qamodelweights) are present in the directory
- The files have the correct permissions
OpenAI API Issues
If you encounter errors with the OpenAI API:
- Verify your API key is correctly set in the
.envfile - Check your internet connection
- Ensure your OpenAI account has available credits
- Check if the requested model (gpt-3.5-turbo) is available for your account
Package Version Conflicts
If you encounter errors related to the OpenAI package or TensorFlow:
- Try updating to the latest versions with
pip install -U openai tensorflow transformers - If using an older version of the OpenAI package, the app has a fallback mechanism to use the legacy API
Limitations
- The custom model has a maximum sequence length (default 384 tokens)
- Long contexts are processed in chunks with a stride
- OpenAI API calls require an internet connection and valid API key
- API calls may incur costs depending on your OpenAI usage
