File size: 6,757 Bytes
98857c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---

title: Studyson
emoji: πŸ“š
colorFrom: purple
colorTo: blue
sdk: docker
pinned: false
---


# Studyson - RAG Document QA & Summarization API

A full-stack Retrieval-Augmented Generation (RAG) system for intelligent document question-answering and summarization. Built with FastAPI, LlamaIndex, and Groq AI.

## Features

- **πŸ“„ PDF Document Processing**: Upload and index PDF documents with intelligent text extraction
- **🌐 Web Content Scraping**: Scrape and index content from URLs
- **πŸ’¬ Interactive Q&A Chat**: Ask questions about your documents with streaming responses
- **πŸ“ Smart Summarization**: Generate concise summaries of indexed documents
- **πŸ” Source Citations**: Get verifiable citations with exact source snippets
- **⚑ Real-time Streaming**: Token-by-token streaming for responsive user experience
- **🎨 Modern UI**: Clean, responsive web interface with tabbed navigation
- **🐳 Docker Support**: Easy deployment with Docker and Docker Compose

## Tech Stack

### Backend
- **FastAPI**: Modern Python web framework
- **LlamaIndex**: RAG orchestration and document indexing
- **Groq**: Lightning-fast LLM inference (Llama 3.1)
- **FastEmbed**: Lightweight embeddings (BGE-small)
- **PyMuPDF**: Advanced PDF text extraction
- **BeautifulSoup**: HTML parsing and web scraping
- **Pydantic**: Data validation and settings management

### Frontend
- **HTML5/CSS3/JavaScript**: Vanilla web technologies
- **Server-Sent Events (SSE)**: Real-time streaming responses

## Architecture

### Ingestion Pipeline
1. User uploads PDF or provides URL
2. Content extraction (PyMuPDF for PDFs, BeautifulSoup for web)
3. Text chunking and embedding via LlamaIndex + FastEmbed
4. In-memory vector index creation

### Query Pipeline
1. Question embedding generation
2. Semantic similarity search for relevant chunks
3. Context + question sent to Groq LLM
4. Streaming response with source citations

## Installation

### Prerequisites
- Python 3.10 or higher
- Groq API key ([Get it free here](https://console.groq.com))

### Local Setup

1. **Clone the repository**
```bash

git clone <repository-url>

cd studyrag

```

2. **Create virtual environment**
```bash

python -m venv venv

source venv/bin/activate  # On Windows: venv\Scripts\activate

```

3. **Install dependencies**
```bash

pip install -r requirements.txt

```

4. **Set up environment variables**
```bash

cp .env.example .env

```

Edit `.env` and add your Groq API key:
```

GROQ_API_KEY=your_groq_api_key_here

PORT=7860

HOST=0.0.0.0

```

5. **Run the application**
```bash

uvicorn app.main:app --reload --port 7860

```

6. **Access the application**

Open your browser and navigate to: `http://localhost:7860`

### Docker Setup

1. **Set environment variables**
```bash

cp .env.example .env

# Edit .env with your Groq API key

```

2. **Build and run with Docker Compose**
```bash

docker-compose up --build

```

## API Endpoints

| Method | Endpoint | Description |
|--------|----------|-------------|
| GET | `/` | Serves the web UI |
| POST | `/upload` | Upload PDF document |
| POST | `/scrape` | Scrape URL content |
| POST | `/stream_query` | Stream Q&A response |
| POST | `/query` | Get Q&A response |
| POST | `/summarize` | Generate summary |
| POST | `/reset` | Clear all documents |
| GET | `/status` | Get system status |

## Project Structure

```

studyrag/

β”œβ”€β”€ app/

β”‚   β”œβ”€β”€ __init__.py

β”‚   β”œβ”€β”€ main.py              # FastAPI application

β”‚   β”œβ”€β”€ config.py            # Configuration settings

β”‚   β”œβ”€β”€ models/

β”‚   β”‚   └── schemas.py       # Pydantic models

β”‚   β”œβ”€β”€ services/

β”‚   β”‚   └── rag_service.py   # RAG logic

β”‚   └── utils/

β”‚       └── document_processor.py

β”œβ”€β”€ static/

β”‚   β”œβ”€β”€ css/style.css

β”‚   β”œβ”€β”€ js/app.js

β”‚   └── index.html

β”œβ”€β”€ .env.example

β”œβ”€β”€ .gitignore

β”œβ”€β”€ Dockerfile

β”œβ”€β”€ docker-compose.yml

β”œβ”€β”€ Procfile

β”œβ”€β”€ requirements.txt

└── README.md

```

## Configuration

### Environment Variables

- `GROQ_API_KEY`: Your Groq API key (required, free tier available)
- `HOST`: Server host (default: 0.0.0.0)
- `PORT`: Server port (default: 7860)

### Application Settings

Edit `app/config.py` to modify:
- `upload_dir`: Upload directory path
- `max_file_size`: Maximum file size (default: 10MB)

## Deployment

### Deploy to Hugging Face Spaces (Recommended - Free)

1. Push code to GitHub
2. Go to [huggingface.co](https://huggingface.co) and create an account
3. Click your profile β†’ **New Space**
4. Configure:
   - **Space name**: `studyson`
   - **SDK**: Select **Docker**
   - **Hardware**: CPU basic (free)
5. Under **Files** β†’ Link to GitHub repo (or upload files)
6. Add secret: `GROQ_API_KEY` in Space Settings β†’ Variables
7. The Space will auto-build and deploy!

**Your app will be live at:** `https://huggingface.co/spaces/YOUR_USERNAME/studyson`

## Features in Detail

### RAG Pipeline
- **Chunking**: Intelligent text splitting for optimal context windows
- **Embeddings**: FastEmbed BGE-small for semantic understanding (lightweight)
- **Retrieval**: Top-k similarity search with configurable parameters
- **Generation**: Groq Llama 3.1 for fast, accurate responses

### Streaming
- Server-Sent Events (SSE) for real-time token delivery
- Progressive rendering in the UI
- Graceful error handling

### Source Attribution
- Exact text snippets from source documents
- Similarity scores for transparency
- Multiple source support per answer

## Limitations

- In-memory vector storage (resets on restart)
- PDF-only document support (extensible to other formats)
- Single-user session management
- No authentication/authorization

## Troubleshooting

### Common Issues

**Import errors:**
```bash

pip install --upgrade -r requirements.txt

```

**API key errors:**
- Verify your `.env` file has the correct `GROQ_API_KEY`
- Check API key validity at [console.groq.com](https://console.groq.com)

**Port already in use:**
```bash

uvicorn app.main:app --port 8000

```

**File upload fails:**
- Check file size is under 10MB

## License

MIT License - feel free to use this project for learning and development.

## Acknowledgments

- [LlamaIndex](https://www.llamaindex.ai/) for RAG orchestration
- [Groq](https://groq.com/) for lightning-fast LLM inference
- [FastEmbed](https://github.com/qdrant/fastembed) for lightweight embeddings
- [FastAPI](https://fastapi.tiangolo.com/) for the web framework

---

Built with ❀️ using RAG technology