Spaces:
Sleeping
A newer version of the Gradio SDK is available:
6.5.1
title: Non-QM Glossary Bot
emoji: π
colorFrom: indigo
colorTo: purple
sdk: gradio
sdk_version: 5.31.0
app_file: app.py
pinned: false
Non-QM Glossary Chatbot
A professional RAG-powered chatbot that provides instant, accurate definitions of Non-Qualified Mortgage terms with strict compliance controls and conversation memory.
Features
- π Non-QM Expertise: Specialized glossary of mortgage terminology
- π¬ Conversation Memory: Smart follow-up question handling
- π Compliance First: Built-in disclaimers and PII protection
- β‘ Streaming Responses: Real-time text generation
- π¨ Professional UI: Modern Gradio interface with custom styling
- π° Cost Efficient: Optimized for <$10/month operation
Prerequisites
- Python 3.8 or higher
- OpenAI API key (for embeddings)
- OpenRouter API key (for Gemini LLM access)
Installation
Clone the repository:
git clone <repository-url> cd ChatBotCreate and activate a virtual environment:
python -m venv venv # On Windows: venv\Scripts\activate # On macOS/Linux: source venv/bin/activateInstall dependencies:
pip install -r requirements.txt
API Key Setup
1. OpenAI API Key
- Go to OpenAI API Keys
- Create a new API key
- Copy the key (starts with
sk-proj-...)
2. OpenRouter API Key
- Go to OpenRouter Keys
- Create a new API key
- Copy the key (starts with
sk-or-...)
3. Environment Configuration
Create a .env file in the project root:
# Create .env file
touch .env
Add your API keys to the .env file:
OPENAI_API_KEY=sk-proj-your-openai-key-here
OPENROUTER_API_KEY=sk-or-your-openrouter-key-here
β οΈ Important: Never commit your .env file to version control. It's already included in .gitignore.
Running the Application
1. Generate Vector Index (First Time Only)
Before running the chatbot for the first time, generate the search index:
python build_index.py
This creates:
glossary.index- FAISS vector search indexchunks.json- Text chunks metadata
2. Start the Chatbot
python app.py
The application will start and display:
Running on local URL: http://127.0.0.1:7860
3. Access the Interface
Open your browser and go to: http://127.0.0.1:7860
Usage
Basic Questions
Ask about Non-QM mortgage terms:
- "What is a Non-QM loan?"
- "Define debt-to-income ratio"
- "What does DSCR mean?"
- "Explain asset-based lending"
Follow-up Questions
The chatbot remembers conversation context:
- After asking about a term, say "tell me more"
- "Can you elaborate on that?"
- "Give me more details"
What NOT to Ask
- Personal financial information
- Rate quotes or loan applications
- Questions outside the glossary scope
Project Structure
ChatBot/
βββ app.py # Main Gradio application
βββ build_index.py # Vector index generation
βββ requirements.txt # Python dependencies
βββ glossary.txt # Source glossary content
βββ glossary.index # Generated FAISS index (after build)
βββ chunks.json # Generated text chunks (after build)
βββ .env # API keys (create this file)
βββ .gitignore # Files to exclude from git
βββ memory-bank/ # Project documentation
Configuration
Key settings in app.py:
EMBED_MODEL = "text-embedding-3-small" # OpenAI embeddings
GPT_MODEL = "google/gemini-2.5-flash-preview-05-20" # OpenRouter LLM
SIM_THRESHOLD = 0.30 # Similarity threshold
TOP_K = 3 # Number of chunks to retrieve
Deployment
Hugging Face Spaces
Create a new Space:
- Go to Hugging Face Spaces
- Choose Gradio SDK
- Set hardware to CPU Basic (free)
Upload required files:
app.py requirements.txt glossary.txt glossary.index chunks.json build_index.pyConfigure secrets in HF Spaces:
- Go to Settings β Variables and Secrets
- Add
OPENAI_API_KEY - Add
OPENROUTER_API_KEY
Deploy:
- Push files to the Space repository
- The app will automatically build and deploy
Maintenance
Updating the Glossary
- Edit
glossary.txtwith new terms - Regenerate the index:
python build_index.py - Restart the application
Cost Monitoring
- OpenAI: ~$0.0001 per query (embeddings)
- OpenRouter: ~$0.005 per response (Gemini)
- Target: <$10/month total operation
Troubleshooting
Common Issues:
"Module not found" error:
pip install -r requirements.txt"No such file" for index files:
python build_index.pyAPI key errors:
- Check
.envfile exists and has correct keys - Verify API keys are valid and have sufficient credits
- Check
Import errors:
pip install faiss-cpu numpy openai requests gradio python-dotenv
Compliance Features
- Automatic Disclaimers: Every response includes required compliance text
- PII Detection: Blocks emails, SSNs, and credit score references
- Scope Limiting: Only answers questions about glossary terms
- Session Memory: Context resets when chat is cleared (no persistent data)
Security
- API keys stored in environment variables
- No user data persistence
- Input sanitization and validation
- PII detection and rejection
Support
For technical issues:
- Check the troubleshooting section above
- Verify all dependencies are installed
- Ensure API keys are correctly configured
- Check that vector index files exist
License
This project is designed for internal compliance-focused use with strict business requirements.