CoolFace
Apppublic

Muhammadmohid/Retrieval-Augmented-Generation

sourceHugging Facemitupdated 9mo agoView on Hugging Face
0likes
App README

๐Ÿค– RAG-Based Chatbot

A Retrieval-Augmented Generation (RAG) chatbot that answers questions from PDF documents using AI-powered semantic search and LLM generation.

![Hugging Face Spaces](https://huggingface.co/spaces/Muhammadmohid/Retrieval-Augmented-Generation) ![Python 3.10+](https://www.python.org/downloads/)

Live Demo: https://huggingface.co/spaces/Muhammadmohid/Retrieval-Augmented-Generation


๐Ÿ“– What is RAG?

RAG (Retrieval-Augmented Generation) combines information retrieval with text generation:

  1. 1.Retrieve โ†’ Find relevant content from documents using semantic search
  2. 2.Augment โ†’ Add retrieved context to the user's question
  3. 3.Generate โ†’ LLM produces accurate, grounded answers

This prevents hallucinations and ensures responses are based on actual document content.


โœจ Features

Core Features

  • โ€”PDF Processing: Upload and extract text from PDF documents
  • โ€”Semantic Search: Uses sentence-transformers for intelligent content retrieval
  • โ€”LLM Integration: Powered by Groq's Llama 3.3 70B model
  • โ€”Source Citations: Every answer includes page numbers and relevance scores
  • โ€”Document Preview: View document statistics and sample chunks
  • โ€”AI Suggested Questions: Generate relevant questions based on document content
  • โ€”Smart Chunking: Sentence-aware text splitting for better context
  • โ€”Interactive UI: Clean Gradio interface with gradient styling

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚         User Interface (Gradio)              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    Document Processing (PyPDF2)              โ”‚
โ”‚    โ€ข Text extraction from PDFs               โ”‚
โ”‚    โ€ข Metadata preservation (page, filename)  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    Text Chunking (Regex)                     โ”‚
โ”‚    โ€ข Sentence-aware splitting                โ”‚
โ”‚    โ€ข 500 chars/chunk, 100 char overlap       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    Embedding Generation                      โ”‚
โ”‚    โ€ข SentenceTransformer (all-MiniLM-L6-v2)  โ”‚
โ”‚    โ€ข 384-dimensional vectors                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    Similarity Search                         โ”‚
โ”‚    โ€ข Cosine similarity (scikit-learn)        โ”‚
โ”‚    โ€ข Top-3 most relevant chunks              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚    LLM Generation (Groq API)                 โ”‚
โ”‚    โ€ข Llama 3.3 70B model                     โ”‚
โ”‚    โ€ข Context + Question โ†’ Answer             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ› ๏ธ Technologies Used

python
# Core Libraries
Python 3.10+             
Gradio                    
PyPDF2                   
sentence-transformers     
scikit-learn          
numpy                
Groq API                 

๐Ÿš€ Installation

Prerequisites

Python 3.10 or higher Groq API key (free at https://console.groq.com)


### Local Setup

# 1. Install dependencies
pip install -r requirements.txt

# 2. Run the application
python app.py

# 3. Open browser
# Navigate to http://localhost:7860

Requirements.txt

txt
gradio
PyPDF2
sentence-transformers
scikit-learn
numpy
groq

๐Ÿ“– Usage

Step 1: Enter API Key

1. Get free API key from https://console.groq.com
2. Paste in the "๐Ÿ”‘ Groq API Key" field

Step 2: Upload PDF

1. Click "๐Ÿ“ Upload PDF Files"
2. Select one or more PDF files
3. Click "๐Ÿš€ Process PDFs"
4. Wait for processing confirmation

Step 3: Generate Questions (Optional)

1. Click "โœจ Generate Suggested Questions"
2. Copy any suggested question
3. Paste into chat input

Step 4: Ask Questions

1. Type your question in chat input
2. Click "Send ๐Ÿ“ค" or press Enter
3. View answer with source citations

๐Ÿ“ Project Structure

rag-based-chatbot/ โ”‚ โ”œโ”€โ”€ app.py โ”œโ”€โ”€ requirements.txt โ”œโ”€โ”€ README.md

๐ŸŽฏ Enhancements Implemented

EnhancementDescription
Sentence-TransformersAdvanced semantic embeddings instead of TF-IDF
Document PreviewShows chunks and statistics after processing
Source CitationsPage numbers and relevance scores in answers
Suggested QuestionsAI-generated questions based on content
Smart ChunkingSentence-aware splitting with metadata

๐Ÿ› Troubleshooting

IssueSolution
"Please enter your Groq API key"Enter valid API key from console.groq.com
"No text could be extracted"Ensure PDFs contain text (not scanned images)
API timeoutCheck internet connection and retry
Chat format errorEnsure using dictionary format with role/content

๐Ÿ“ License

This project is for educational purposes.


๐Ÿ‘จโ€๐Ÿ’ป Author

Muhammad Mohid Hugging Face: @Muhammadmohid


๐Ÿ™ Acknowledgments

  • โ€”Groq for fast LLM inference
  • โ€”Hugging Face for model hosting
  • โ€”Sentence-Transformers team for embedding models
  • โ€”Gradio team for UI framework

Last Updated: December 27, 2025