File size: 13,332 Bytes
1e638b2
 
 
 
 
 
 
 
 
 
 
25ff05b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0b150b
25ff05b
 
 
 
 
 
 
 
 
b0b150b
 
25ff05b
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
---
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**