-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
37 lines (33 loc) · 1.14 KB
/
Copy pathapp.py
File metadata and controls
37 lines (33 loc) · 1.14 KB
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
import os
from dotenv import load_dotenv
load_dotenv()
import streamlit as st
from src.scraper import scrape
from src.chunker import chunk_text
from src.embedder import embed
from src.retriever import Retriever
from src.generator import generate
# Build or load index on first run (simple demo)
@st.cache_resource
def load_retriever():
url = "https://example.com" # replace with your corpus source
html = scrape(url)
chunks = chunk_text(html)
embeddings = embed(chunks)
retriever = Retriever()
metas = [{"path": url, "chunk_index": i} for i in range(len(chunks))]
retriever.build(embeddings, metas)
return retriever, chunks
retriever, all_chunks = load_retriever()
st.title("RAG Chatbot")
question = st.text_input("Ask a question:")
if st.button("Generate"):
if not question:
st.warning("Please enter a question.")
else:
query_emb = embed([question])[0]
top_meta = retriever.retrieve(query_emb, k=5)
top_indices = [m["chunk_index"] for m in top_meta]
context_chunks = [all_chunks[i] for i in top_indices]
answer = generate(question, context_chunks)
st.write(answer)