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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hybrid RAG System - Production Demo
|
| 3 |
+
Deployed on HuggingFace Spaces
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import pandas as pd
|
| 8 |
+
import numpy as np
|
| 9 |
+
import torch
|
| 10 |
+
from sentence_transformers import SentenceTransformer, util
|
| 11 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 12 |
+
import nltk
|
| 13 |
+
|
| 14 |
+
# Download NLTK data
|
| 15 |
+
try:
|
| 16 |
+
nltk.download('stopwords', quiet=True)
|
| 17 |
+
nltk.download('punkt', quiet=True)
|
| 18 |
+
except:
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
print("π Loading Hybrid RAG System...")
|
| 22 |
+
|
| 23 |
+
class SimpleRAGDemo:
|
| 24 |
+
def __init__(self):
|
| 25 |
+
self.device = "cpu" # Force CPU for HF Spaces
|
| 26 |
+
|
| 27 |
+
print("Loading models...")
|
| 28 |
+
# Embedding model
|
| 29 |
+
self.emb_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 30 |
+
|
| 31 |
+
# Generator
|
| 32 |
+
self.tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 33 |
+
self.model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 34 |
+
|
| 35 |
+
# Sample corpus (for demo)
|
| 36 |
+
self.texts = [
|
| 37 |
+
"machine learning is a subset of artificial intelligence that enables computers to learn from data without explicit programming",
|
| 38 |
+
"deep learning uses artificial neural networks with multiple layers to process information in a hierarchical manner",
|
| 39 |
+
"natural language processing helps computers understand analyze and generate human language",
|
| 40 |
+
"retrieval augmented generation combines information retrieval with language generation for more accurate responses",
|
| 41 |
+
"transformers are neural network architectures that use self-attention mechanisms for processing sequential data",
|
| 42 |
+
"bert is a transformer-based model pretrained on large text corpora for natural language understanding tasks",
|
| 43 |
+
"question answering systems retrieve relevant information and generate concise answers to user queries",
|
| 44 |
+
"semantic search uses embeddings to find documents based on meaning rather than exact keyword matches",
|
| 45 |
+
"neural embeddings represent words or documents as dense vectors in a continuous space",
|
| 46 |
+
"language models predict the probability of word sequences and can generate coherent text"
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
print("Creating embeddings...")
|
| 50 |
+
self.embeddings = self.emb_model.encode(self.texts, convert_to_tensor=True)
|
| 51 |
+
|
| 52 |
+
print("β
System ready!")
|
| 53 |
+
|
| 54 |
+
def retrieve_and_answer(self, query, top_k=3):
|
| 55 |
+
if not query or len(query.strip()) < 3:
|
| 56 |
+
return "β Please enter a valid question.", ""
|
| 57 |
+
|
| 58 |
+
# Dense retrieval
|
| 59 |
+
query_emb = self.emb_model.encode(query, convert_to_tensor=True)
|
| 60 |
+
similarities = util.cos_sim(query_emb, self.embeddings)[0]
|
| 61 |
+
top_idx = torch.argsort(similarities, descending=True)[:top_k]
|
| 62 |
+
|
| 63 |
+
contexts = [self.texts[i] for i in top_idx]
|
| 64 |
+
scores = [float(similarities[i]) for i in top_idx]
|
| 65 |
+
|
| 66 |
+
# Generate answer
|
| 67 |
+
context_text = "\n".join([f"[{i+1}] {c}" for i, c in enumerate(contexts)])
|
| 68 |
+
prompt = f"Answer based on context:\n{context_text}\n\nQuestion: {query}\nAnswer:"
|
| 69 |
+
|
| 70 |
+
inputs = self.tokenizer(prompt, max_length=512, truncation=True, return_tensors="pt")
|
| 71 |
+
|
| 72 |
+
with torch.no_grad():
|
| 73 |
+
outputs = self.model.generate(**inputs, max_length=100)
|
| 74 |
+
|
| 75 |
+
answer = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 76 |
+
|
| 77 |
+
# Format outputs
|
| 78 |
+
answer_md = f"""
|
| 79 |
+
### π€ Generated Answer
|
| 80 |
+
|
| 81 |
+
**{answer}**
|
| 82 |
+
|
| 83 |
+
---
|
| 84 |
+
|
| 85 |
+
### π Key Research Finding
|
| 86 |
+
|
| 87 |
+
This demo showcases my Master's thesis work where I achieved a **64.5% improvement**
|
| 88 |
+
in semantic similarity using dense retrieval compared to traditional sparse methods.
|
| 89 |
+
|
| 90 |
+
**Evaluation:** 198 queries | 5,000 documents | MS MARCO dataset
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
+
retrieved_md = "### π Retrieved Documents\n\n"
|
| 94 |
+
for i, (ctx, score) in enumerate(zip(contexts, scores), 1):
|
| 95 |
+
retrieved_md += f"**[{i}]** Relevance: `{score:.4f}`\n\n{ctx}\n\n---\n\n"
|
| 96 |
+
|
| 97 |
+
return answer_md, retrieved_md
|
| 98 |
+
|
| 99 |
+
# Initialize system
|
| 100 |
+
print("Initializing RAG system...")
|
| 101 |
+
rag = SimpleRAGDemo()
|
| 102 |
+
|
| 103 |
+
# Create Gradio interface
|
| 104 |
+
demo = gr.Blocks(
|
| 105 |
+
title="Hybrid RAG System - Master's Thesis Demo",
|
| 106 |
+
theme=gr.themes.Soft()
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
with demo:
|
| 110 |
+
gr.Markdown("""
|
| 111 |
+
# π― Hybrid RAG System - Interactive Demo
|
| 112 |
+
|
| 113 |
+
**Master's Thesis Project** | AI-Powered Evidence-Based Question Answering
|
| 114 |
+
|
| 115 |
+
---
|
| 116 |
+
|
| 117 |
+
## π Research Achievement
|
| 118 |
+
|
| 119 |
+
**64.5% improvement** in semantic similarity over baseline methods!
|
| 120 |
+
|
| 121 |
+
This system demonstrates advanced Retrieval-Augmented Generation combining:
|
| 122 |
+
- π Dense neural retrieval (Sentence Transformers)
|
| 123 |
+
- π€ Answer generation (FLAN-T5)
|
| 124 |
+
- π Production-ready pipeline
|
| 125 |
+
|
| 126 |
+
---
|
| 127 |
+
""")
|
| 128 |
+
|
| 129 |
+
with gr.Row():
|
| 130 |
+
with gr.Column(scale=1):
|
| 131 |
+
query_input = gr.Textbox(
|
| 132 |
+
label="β Enter Your Question",
|
| 133 |
+
placeholder="e.g., What is machine learning?",
|
| 134 |
+
lines=3
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
submit_btn = gr.Button("π Get Answer", variant="primary", size="lg")
|
| 138 |
+
|
| 139 |
+
gr.Markdown("### π Example Questions:")
|
| 140 |
+
gr.Examples(
|
| 141 |
+
examples=[
|
| 142 |
+
"What is machine learning?",
|
| 143 |
+
"Explain deep learning",
|
| 144 |
+
"What are transformers in NLP?",
|
| 145 |
+
"How does semantic search work?",
|
| 146 |
+
"What is retrieval augmented generation?",
|
| 147 |
+
],
|
| 148 |
+
inputs=query_input
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
with gr.Column(scale=2):
|
| 152 |
+
answer_output = gr.Markdown(label="Answer & Research Info")
|
| 153 |
+
retrieved_output = gr.Markdown(label="Retrieved Context")
|
| 154 |
+
|
| 155 |
+
submit_btn.click(
|
| 156 |
+
fn=rag.retrieve_and_answer,
|
| 157 |
+
inputs=[query_input],
|
| 158 |
+
outputs=[answer_output, retrieved_output]
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
gr.Markdown("""
|
| 162 |
+
---
|
| 163 |
+
|
| 164 |
+
## π Complete Research Results
|
| 165 |
+
|
| 166 |
+
| Method | Semantic Similarity | Improvement |
|
| 167 |
+
|--------|-------------------|-------------|
|
| 168 |
+
| **Dense (Neural Embeddings)** | **0.1966** | **+64.5%** β |
|
| 169 |
+
| Hybrid (Weighted Fusion) | 0.1816 | +51.9% |
|
| 170 |
+
| Hybrid (RRF) | 0.1542 | +28.9% |
|
| 171 |
+
| Sparse (BM25) | 0.1196 | Baseline |
|
| 172 |
+
|
| 173 |
+
**Evaluation Details:**
|
| 174 |
+
- Dataset: MS MARCO
|
| 175 |
+
- Corpus: 5,000 documents
|
| 176 |
+
- Queries: 198 real-world questions
|
| 177 |
+
- Metrics: ROUGE, BLEU, Semantic Similarity
|
| 178 |
+
|
| 179 |
+
---
|
| 180 |
+
|
| 181 |
+
## π¨βπ» About This Project
|
| 182 |
+
|
| 183 |
+
**Title:** AI-Powered Retrieval-Augmented Assistant for Evidence-Based Question Answering
|
| 184 |
+
|
| 185 |
+
**Author:** [Your Name]
|
| 186 |
+
**Institution:** [Your University]
|
| 187 |
+
**Location:** Berlin, Germany π©πͺ
|
| 188 |
+
|
| 189 |
+
**Technologies:**
|
| 190 |
+
- Python, PyTorch, Transformers
|
| 191 |
+
- Sentence-Transformers, BM25
|
| 192 |
+
- Gradio, HuggingFace
|
| 193 |
+
|
| 194 |
+
**GitHub:** [Your GitHub Link]
|
| 195 |
+
**LinkedIn:** [Your LinkedIn]
|
| 196 |
+
**Email:** [Your Email]
|
| 197 |
+
|
| 198 |
+
---
|
| 199 |
+
|
| 200 |
+
### πΌ Currently Seeking Full-Time Opportunities
|
| 201 |
+
|
| 202 |
+
I'm actively looking for **AI/ML Engineer** positions in Berlin, Germany!
|
| 203 |
+
|
| 204 |
+
If you're interested in my work, please reach out at [[email protected]]
|
| 205 |
+
|
| 206 |
+
---
|
| 207 |
+
|
| 208 |
+
Built with β€οΈ using Gradio and HuggingFace Transformers
|
| 209 |
+
""")
|
| 210 |
+
|
| 211 |
+
# Launch
|
| 212 |
+
demo.launch()
|