File size: 4,927 Bytes
5fd4bb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: RAG Python System
emoji: πŸ€–
colorFrom: blue
colorTo: green
sdk: gradio
sdk_version: 4.44.1
app_file: app.py
pinned: false
license: mit
---

# RAG Python System πŸ€–

A local-first Retrieval-Augmented Generation (RAG) system that enables intelligent question-answering over your documents using Hugging Face Inference API, ChromaDB, and Gradio.

## Features

- πŸ“„ **Document Processing**: Converts PDF, DOCX, and TXT files to searchable format
- πŸ” **Semantic Search**: Uses sentence transformers for accurate context retrieval
- πŸ€– **AI-Powered Answers**: Leverages Llama 3.2 3B via Hugging Face Inference API
- πŸ’¬ **Interactive UI**: Clean Gradio interface for easy interaction
- 🎯 **Source Citations**: Provides references to source documents

## How It Works

1. **Document Ingestion**: Upload or use pre-loaded documents (currently includes "Think Python" guide)
2. **Semantic Chunking**: Documents are split into meaningful chunks
3. **Vector Embeddings**: Text chunks are converted to embeddings using `all-MiniLM-L6-v2`
4. **Context Retrieval**: Relevant chunks are retrieved based on your question
5. **Answer Generation**: Llama 3.2 generates answers using the retrieved context

## Setup Instructions

### For Hugging Face Spaces Deployment

1. **Fork or Duplicate this Space**
   - Click the three dots menu β†’ "Duplicate Space"

2. **Get Your Hugging Face Token**
   - Go to [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
   - Create a new token with `read` permissions
   - Copy the token (starts with `hf_`)

3. **Add Token to Space Secrets**
   - Go to your Space Settings
   - Navigate to "Repository secrets"
   - Add a new secret:
     - **Name**: `HF_TOKEN`
     - **Value**: Your token from step 2
   - Click "Add"

4. **Restart the Space**
   - The Space will automatically rebuild and start

### For Local Development

1. **Clone the Repository**
   ```bash
   git clone https://github.com/monsara/rag-python-rag.git
   cd rag-python-rag
   ```

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 Environment Variable**
   ```bash
   export HF_TOKEN=hf_your_token_here
   ```

5. **Run the Application**
   ```bash
   python app.py
   ```

6. **Access the Interface**
   - Open your browser to `http://localhost:7860`

## Usage Examples

Try asking questions like:

- "How do if-else statements work in Python?"
- "What are the different types of loops in Python?"
- "How do you handle errors in Python?"
- "Explain Python functions with examples"
- "What is object-oriented programming in Python?"

## Architecture

```
User Query β†’ Gradio UI β†’ Vector Store (ChromaDB) β†’ Context Retrieval
                                                          ↓
                                                   HF Inference API
                                                          ↓
                                                   Llama 3.2 3B
                                                          ↓
                                                   Formatted Response
```

## Tech Stack

- **Frontend**: Gradio 4.44.1
- **LLM**: Llama 3.2 3B Instruct (via Hugging Face Inference API)
- **Embeddings**: all-MiniLM-L6-v2 (Sentence Transformers)
- **Vector DB**: ChromaDB
- **Document Processing**: PyMuPDF, python-docx
- **Text Splitting**: LangChain Text Splitters

## Rate Limits

**Free Tier (Hugging Face Inference API):**
- ~1000 requests/hour
- 1024 max tokens per response
- Shared infrastructure

**For Production:**
Consider upgrading to [Hugging Face Pro](https://huggingface.co/pricing) ($9/month) for:
- Higher rate limits
- Faster inference
- Priority support

## Limitations

- Currently uses a single pre-loaded document ("Think Python")
- Free tier has rate limits
- Response quality depends on context relevance
- Max 1024 tokens per response

## Roadmap

- [ ] File upload functionality
- [ ] Multiple document support
- [ ] Conversation history
- [ ] Custom model selection
- [ ] Advanced filtering options
- [ ] Export conversation feature

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

MIT License - see LICENSE file for details

## Links

- **GitHub Repository**: [monsara/rag-python-rag](https://github.com/monsara/rag-python-rag)
- **Documentation**: See [README.md](README.md) for detailed technical documentation
- **Hugging Face**: [Get your API token](https://huggingface.co/settings/tokens)

## Support

If you encounter issues:

1. Check that `HF_TOKEN` is set correctly in Space secrets
2. Verify your token has `read` permissions
3. Check the Space logs for error messages
4. Open an issue on GitHub

---

Built with ❀️ using Hugging Face, Gradio, and ChromaDB