Prerequisites
Before deploying to Hugging Face Spaces, ensure you have:- A Hugging Face account
- Mistral AI API key
- OpenAI API key
- Your PAS2 repository ready for deployment
Deployment steps
Create a new Space
- Go to Hugging Face Spaces
- Click “Create new Space”
- Choose a name for your Space
- Select Gradio as the SDK
- Choose your preferred visibility (Public or Private)
Add your repository
Connect your PAS2 repository to the Space:
- Upload your files directly, or
- Link to your GitHub repository for automatic syncing
pas2.py(main application code)requirements.txt(dependencies)README.md(optional documentation)
Configure API keys as secrets
Set your API keys in the Space’s settings to keep them secure:
- Navigate to your Space’s Settings tab
- Scroll to the Repository secrets section
- Add the following secrets:
The application automatically reads these environment variables from lines pas2.py:44-45. The
HF_* prefix is used specifically for Hugging Face Spaces secrets.Enable persistent storage
Hugging Face Spaces automatically provides a This ensures:
/data directory for persistent storage. The PAS2 application uses this for the SQLite feedback database.The persistent storage setup is configured in pas2.py:384-386:- User feedback survives Space restarts
- Statistics are preserved long-term
- No data loss during inactivity periods
Environment variable configuration
The application supports multiple API key configurations with the following priority order:- Explicitly passed parameters
- Hugging Face Spaces secrets (
HF_MISTRAL_API_KEY,HF_OPENAI_API_KEY) - Standard environment variables (
MISTRAL_API_KEY,OPENAI_API_KEY)
Required dependencies
Yourrequirements.txt should include:
Data persistence details
The SQLite database stores:- User query history
- Original and paraphrased responses
- Hallucination detection results
- User feedback ratings
- Confidence scores and reasoning
/data directory.
Monitoring your deployment
After deployment, you can:- View real-time logs in the Space’s Logs tab
- Monitor usage statistics through the built-in dashboard
- Check feedback data persistence by submitting test queries
- Review API usage to ensure rate limits are not exceeded
The application logs all operations using Python’s logging module with INFO level. Check the logs if you encounter API initialization errors or database issues.
Troubleshooting
API keys not found
If you see “API key is required” errors:- Verify secrets are set correctly in Space settings
- Ensure secret names match exactly:
HF_MISTRAL_API_KEYandHF_OPENAI_API_KEY - Restart the Space after adding secrets
Database initialization fails
If the database fails to initialize:- Check the logs for permission errors
- Verify the Space has persistent storage enabled
- The application will attempt fallback to temporary directory
Build failures
If the Space fails to build:- Verify
requirements.txtincludes all dependencies - Check for syntax errors in
pas2.py - Review build logs for specific error messages