--- title: MEXAR Ultimate emoji: ๐Ÿง  colorFrom: blue colorTo: purple sdk: docker app_port: 7860 pinned: false license: mit --- # MEXAR Ultimate ๐Ÿง  **Multimodal Explainable AI Reasoning Assistant** [![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/) [![React 18](https://img.shields.io/badge/react-18-61dafb.svg)](https://reactjs.org/) [![FastAPI](https://img.shields.io/badge/fastapi-0.109-009688.svg)](https://fastapi.tiangolo.com/) [![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE) [![Deployed](https://img.shields.io/badge/status-live-brightgreen.svg)](https://mexar.vercel.app) > Create domain-specific intelligent agents from your data with transparent, explainable AI responses using RAG (Retrieval-Augmented Generation) with source attribution and faithfulness scoring. **๐Ÿš€ Live Demo**: [https://mexar.vercel.app](https://mexar.vercel.app) **๐Ÿ“ก Backend API**: [https://devrajsinh2012-mexar.hf.space](https://devrajsinh2012-mexar.hf.space) --- ## โœจ Key Features | Feature | Description | |---------|-------------| | ๐Ÿ” **Hybrid Search** | Combines semantic (vector) + keyword search with RRF fusion for optimal retrieval | | ๐ŸŽฏ **Cross-Encoder Reranking** | Improves retrieval precision using sentence-transformers | | ๐Ÿ“Š **Source Attribution** | Inline citations `[1]`, `[2]` linking answers to source data | | โœ… **Faithfulness Scoring** | Measures how well answers are grounded in retrieved context | | ๐Ÿ—ฃ๏ธ **Multimodal Input** | Audio (Whisper), Images (Vision), Video support | | ๐Ÿ” **Domain Guardrails** | Prevents hallucinations outside knowledge base | | ๐Ÿ”Š **Text-to-Speech** | ElevenLabs + Web Speech API integration | | ๐Ÿ“ **5 File Types** | CSV, PDF, DOCX, JSON, TXT | --- ## ๐Ÿ—๏ธ Architecture ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ MEXAR Ultimate Stack โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ โ”‚ โ”‚ [React Frontend - Vercel] โ”‚ โ”‚ โ†“ โ”‚ โ”‚ [FastAPI Backend - Hugging Face Spaces] โ”‚ โ”‚ โ†“ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ ๐Ÿ”„ Data Validator (5 file types) โ”‚ โ”‚ โ”‚ โ”‚ ๐Ÿค– Prompt Analyzer (LLM-based) โ”‚ โ”‚ โ”‚ โ”‚ ๐Ÿ“ฆ Knowledge Compiler (FastEmbed) โ”‚ โ”‚ โ”‚ โ”‚ ๐Ÿง  Reasoning Engine โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€ Hybrid Search โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€ Cross-Encoder Reranker โ”‚ โ”‚ โ”‚ โ”‚ โ”œโ”€ Source Attribution โ”‚ โ”‚ โ”‚ โ”‚ โ””โ”€ Faithfulness Scorer โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ†“ โ”‚ โ”‚ [External Services] โ”‚ โ”‚ โ”œโ”€ Supabase (PostgreSQL + pgvector + Storage) โ”‚ โ”‚ โ”œโ”€ Groq API (LLM + Whisper + Vision) โ”‚ โ”‚ โ””โ”€ ElevenLabs (Text-to-Speech) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` --- ## ๐Ÿš€ Quick Start ### Prerequisites - **Python 3.9+** with pip - **Node.js 18+** with npm - **PostgreSQL** with `pgvector` extension (or use Supabase) - **Groq API Key** - Get free at [console.groq.com](https://console.groq.com) ### Local Development #### 1. Backend Setup ```bash cd backend # Create virtual environment python -m venv venv # Activate (Windows) .\venv\Scripts\activate # Activate (macOS/Linux) source venv/bin/activate # Install dependencies pip install -r requirements.txt # Configure environment cp .env.example .env # Edit .env and add your API keys # Run server python -m uvicorn main:app --reload --host 0.0.0.0 --port 8000 ``` **Backend will run at**: [http://localhost:8000](http://localhost:8000) #### 2. Frontend Setup ```bash cd frontend # Install dependencies npm install # Start development server npm start ``` **Frontend will run at**: [http://localhost:3000](http://localhost:3000) --- ## ๐Ÿ“ Project Structure ``` mexar_ultimate/ โ”œโ”€โ”€ backend/ # FastAPI Backend โ”‚ โ”œโ”€โ”€ api/ # REST API endpoints โ”‚ โ”‚ โ”œโ”€โ”€ auth.py # Authentication (JWT) โ”‚ โ”‚ โ”œโ”€โ”€ agents.py # Agent CRUD โ”‚ โ”‚ โ”œโ”€โ”€ chat.py # Chat + multimodal โ”‚ โ”‚ โ”œโ”€โ”€ compile.py # Knowledge compilation โ”‚ โ”‚ โ””โ”€โ”€ websocket.py # Real-time updates โ”‚ โ”œโ”€โ”€ core/ # Core configuration โ”‚ โ”‚ โ”œโ”€โ”€ config.py # Settings โ”‚ โ”‚ โ”œโ”€โ”€ database.py # SQLAlchemy setup โ”‚ โ”‚ โ””โ”€โ”€ security.py # JWT handling โ”‚ โ”œโ”€โ”€ models/ # Database models โ”‚ โ”‚ โ”œโ”€โ”€ user.py # User model โ”‚ โ”‚ โ”œโ”€โ”€ agent.py # Agent + CompilationJob โ”‚ โ”‚ โ”œโ”€โ”€ chunk.py # DocumentChunk (pgvector) โ”‚ โ”‚ โ””โ”€โ”€ conversation.py # Chat history โ”‚ โ”œโ”€โ”€ modules/ # Core AI modules โ”‚ โ”‚ โ”œโ”€โ”€ data_validator.py # File parsing โ”‚ โ”‚ โ”œโ”€โ”€ prompt_analyzer.py # Domain extraction โ”‚ โ”‚ โ”œโ”€โ”€ knowledge_compiler.py # Vector embeddings โ”‚ โ”‚ โ”œโ”€โ”€ reasoning_engine.py # RAG pipeline โ”‚ โ”‚ โ””โ”€โ”€ explainability.py # UI formatting โ”‚ โ”œโ”€โ”€ utils/ # Utilities โ”‚ โ”‚ โ”œโ”€โ”€ groq_client.py # Groq API wrapper โ”‚ โ”‚ โ”œโ”€โ”€ hybrid_search.py # RRF search fusion โ”‚ โ”‚ โ”œโ”€โ”€ reranker.py # Cross-encoder โ”‚ โ”‚ โ”œโ”€โ”€ faithfulness.py # Claim verification โ”‚ โ”‚ โ””โ”€โ”€ source_attribution.py # Citation extraction โ”‚ โ”œโ”€โ”€ main.py # FastAPI entry point โ”‚ โ””โ”€โ”€ requirements.txt # Python dependencies โ”‚ โ”œโ”€โ”€ frontend/ # React Frontend โ”‚ โ”œโ”€โ”€ src/ โ”‚ โ”‚ โ”œโ”€โ”€ pages/ # React pages โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Landing.jsx # Home page โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Login.jsx # Authentication โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ Dashboard.jsx # User dashboard โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ AgentCreation.jsx # Create agent โ”‚ โ”‚ โ”‚ โ”œโ”€โ”€ CompilationProgress.jsx # Build progress โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ Chat.jsx # Chat interface โ”‚ โ”‚ โ”œโ”€โ”€ components/ # Reusable UI โ”‚ โ”‚ โ”œโ”€โ”€ contexts/ # React contexts โ”‚ โ”‚ โ”œโ”€โ”€ api/ # API client โ”‚ โ”‚ โ””โ”€โ”€ App.jsx # Main component โ”‚ โ”œโ”€โ”€ package.json # Node dependencies โ”‚ โ””โ”€โ”€ vercel.json # Vercel config โ”‚ โ”œโ”€โ”€ Dockerfile # Docker config for HF Spaces โ””โ”€โ”€ README.md # This file ``` --- ## ๐ŸŒ Deployment ### Current Deployment (Free Tier) - **Frontend**: Vercel - [https://mexar.vercel.app](https://mexar.vercel.app) - **Backend**: Hugging Face Spaces - [https://devrajsinh2012-mexar.hf.space](https://devrajsinh2012-mexar.hf.space) - **Database**: Supabase (PostgreSQL with pgvector) - **Storage**: Supabase Storage - **Total Cost**: $0/month ### Deploy Your Own Instance #### Deploy Backend to Hugging Face Spaces 1. Fork this repository 2. Create a new Space at [huggingface.co/new-space](https://huggingface.co/new-space) 3. Select **Docker** as SDK 4. Connect your GitHub repository 5. Add Repository Secrets: - `GROQ_API_KEY` - `DATABASE_URL` - `SUPABASE_URL` - `SUPABASE_KEY` - `SECRET_KEY` - `FRONTEND_URL` #### Deploy Frontend to Vercel 1. Import repository at [vercel.com](https://vercel.com) 2. Set **Root Directory** to `frontend` 3. Add Environment Variable: - `REACT_APP_API_URL` = Your HF Spaces URL --- ## ๐Ÿ”ง Environment Variables ### Backend (`backend/.env`) ```env # Required: Get from console.groq.com GROQ_API_KEY=your_groq_api_key_here # Supabase Database DATABASE_URL=postgresql://user:password@host:5432/database # JWT Security SECRET_KEY=generate-a-secure-random-key # Supabase Storage SUPABASE_URL=https://your-project.supabase.co SUPABASE_KEY=your_supabase_service_role_key # Optional: ElevenLabs TTS ELEVENLABS_API_KEY=your_elevenlabs_api_key_here # Frontend URL for CORS FRONTEND_URL=https://mexar.vercel.app ``` ### Frontend (`frontend/.env`) ```env # Backend API URL REACT_APP_API_URL=https://your-backend.hf.space ``` --- ## ๐Ÿ” API Documentation Once the backend is running, interactive API docs are available at: - **Swagger UI**: `http://localhost:8000/docs` - **ReDoc**: `http://localhost:8000/redoc` ### Key Endpoints | Method | Endpoint | Description | |--------|----------|-------------| | POST | `/api/auth/register` | Register new user | | POST | `/api/auth/login` | Login (returns JWT) | | GET | `/api/agents/` | List all agents | | POST | `/api/compile/` | Start agent compilation | | GET | `/api/compile/{name}/status` | Check compilation status | | POST | `/api/chat/` | Send message to agent | | POST | `/api/chat/multimodal` | Send with audio/image | --- ## ๐Ÿงช Technologies ### Backend - **FastAPI** - Modern async Python web framework - **SQLAlchemy** - ORM for PostgreSQL - **pgvector** - Vector similarity search - **FastEmbed** - Local embedding generation (BAAI/bge-small-en-v1.5) - **sentence-transformers** - Cross-encoder reranking - **Groq API** - LLM (Llama 3.1/3.3), Whisper (audio), Vision (images) ### Frontend - **React 18** - UI framework - **Material-UI (MUI)** - Component library - **React Router** - Navigation - **Axios** - HTTP client ### External Services - **Supabase** - Managed PostgreSQL + Storage - **Groq** - Fast AI inference (LPU architecture) - **ElevenLabs** - Text-to-Speech (optional) --- ## ๐Ÿ“Š How It Works ### 1. Agent Creation Flow ``` User uploads files โ†’ DataValidator parses content โ†’ PromptAnalyzer extracts domain & keywords โ†’ KnowledgeCompiler creates embeddings โ†’ Stored in pgvector database ``` ### 2. Query Processing Flow ``` User query โ†’ Domain Guardrail check โ†’ Hybrid Search (semantic + keyword) โ†’ Cross-Encoder Reranking (top 5 results) โ†’ LLM Generation with retrieved context โ†’ Source Attribution (extract citations) โ†’ Faithfulness Scoring (verify grounding) โ†’ Explainability Formatting ``` ### 3. Confidence Calculation Confidence score is calculated from: - **Retrieval Quality** (35%) - Relevance of retrieved chunks - **Rerank Score** (30%) - Cross-encoder confidence - **Faithfulness** (25%) - Answer grounding in context - **Base Floor** (10%) - For in-domain queries --- ## โš ๏ธ Known Limitations (Free Tier) 1. **Cold Start Delay**: First request after 15 min idle takes 45-90 seconds 2. **Model Download**: Initial startup takes 3-5 minutes (FastEmbed caching) 3. **Groq Rate Limits**: 30 requests/min, 14,400/day (free tier) 4. **Concurrent Users**: 1-2 recommended on free tier (2GB RAM limit) 5. **Ephemeral Storage**: HF Spaces `/tmp` data lost on restart (Supabase used for persistence) **Production Migration**: Upgrade to paid tiers for ~$54/month (persistent instances, higher limits) --- ## ๐Ÿค Contributing Contributions are welcome! Please feel free to submit a Pull Request. 1. Fork the repository 2. Create your feature branch (`git checkout -b feature/AmazingFeature`) 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) 4. Push to the branch (`git push origin feature/AmazingFeature`) 5. Open a Pull Request --- ## ๐Ÿ“„ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. --- ## ๐Ÿ™ Acknowledgments - [Groq](https://groq.com) - Fast AI inference with LPU technology - [Supabase](https://supabase.com) - PostgreSQL + Storage platform - [FastEmbed](https://github.com/qdrant/fastembed) - Lightweight embeddings library - [sentence-transformers](https://www.sbert.net) - Reranking models - [Hugging Face](https://huggingface.co) - Free ML model hosting --- ## ๐Ÿ“ž Support For questions or support, please open an issue in the GitHub repository. **Built with โค๏ธ using modern AI technologies**