Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- app.py +21 -0
- english_quotes.csv +0 -0
- model.safetensors +3 -0
- rag_pipeline.py +65 -0
app.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from rag_pipeline import QuoteRAG
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Load RAG system
|
| 6 |
+
rag = QuoteRAG()
|
| 7 |
+
|
| 8 |
+
st.title("π RAG-Based Quote Retrieval")
|
| 9 |
+
st.write("Ask me for quotes (e.g., *quotes about courage by women authors*)")
|
| 10 |
+
|
| 11 |
+
query = st.text_input("Enter your query:")
|
| 12 |
+
|
| 13 |
+
if query:
|
| 14 |
+
results = rag.search(query, top_k=5)
|
| 15 |
+
|
| 16 |
+
st.subheader("Results (JSON with similarity score 0β1, higher is better):")
|
| 17 |
+
st.json(results)
|
| 18 |
+
|
| 19 |
+
# Download option
|
| 20 |
+
json_str = json.dumps(results, indent=2)
|
| 21 |
+
st.download_button("π₯ Download JSON", json_str, file_name="results.json")
|
english_quotes.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1377e9af0ca0b016a9f2aa584d6fc71ab3ea6804fae21ef9fb1416e2944057ac
|
| 3 |
+
size 90864192
|
rag_pipeline.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import faiss
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sentence_transformers import SentenceTransformer
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
class QuoteRAG:
|
| 8 |
+
def __init__(self, model_path="models/fine_tuned_model", data_path="data/english_quotes.csv"):
|
| 9 |
+
# Load model
|
| 10 |
+
try:
|
| 11 |
+
self.model = SentenceTransformer(model_path)
|
| 12 |
+
print("Loaded fine-tuned model")
|
| 13 |
+
except:
|
| 14 |
+
self.model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 15 |
+
print("Loaded base model")
|
| 16 |
+
|
| 17 |
+
# Load dataset
|
| 18 |
+
self.df = pd.read_csv(data_path)
|
| 19 |
+
|
| 20 |
+
# Encode all quotes
|
| 21 |
+
self.embeddings = self.model.encode(self.df["quote"].tolist(), convert_to_numpy=True)
|
| 22 |
+
d = self.embeddings.shape[1]
|
| 23 |
+
|
| 24 |
+
# Build FAISS index
|
| 25 |
+
self.index = faiss.IndexFlatL2(d)
|
| 26 |
+
self.index.add(self.embeddings)
|
| 27 |
+
print("FAISS index built with", len(self.df), "quotes")
|
| 28 |
+
|
| 29 |
+
def search(self, query, top_k=5):
|
| 30 |
+
# Encode query
|
| 31 |
+
query_emb = self.model.encode([query], convert_to_numpy=True)
|
| 32 |
+
distances, indices = self.index.search(query_emb, top_k * 3) # fetch more for filtering
|
| 33 |
+
|
| 34 |
+
results = []
|
| 35 |
+
for idx, dist in zip(indices[0], distances[0]):
|
| 36 |
+
row = self.df.iloc[idx]
|
| 37 |
+
|
| 38 |
+
# Normalized similarity: 0β1 (higher is better)
|
| 39 |
+
similarity = round(1 / (1 + float(dist)), 3)
|
| 40 |
+
|
| 41 |
+
results.append({
|
| 42 |
+
"quote": row["quote"],
|
| 43 |
+
"author": row["author"],
|
| 44 |
+
"tags": row.get("tags", ""),
|
| 45 |
+
"similarity": similarity
|
| 46 |
+
})
|
| 47 |
+
|
| 48 |
+
# Simple author filter if author name is in query
|
| 49 |
+
query_lower = query.lower()
|
| 50 |
+
author_filtered = [r for r in results if r["author"].lower() in query_lower]
|
| 51 |
+
|
| 52 |
+
if author_filtered:
|
| 53 |
+
results = author_filtered[:top_k]
|
| 54 |
+
else:
|
| 55 |
+
results = results[:top_k]
|
| 56 |
+
|
| 57 |
+
return results
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
rag = QuoteRAG()
|
| 62 |
+
query = "Quotes about insanity attributed to Einstein"
|
| 63 |
+
results = rag.search(query)
|
| 64 |
+
for r in results:
|
| 65 |
+
print(r)
|