Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,99 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from selenium import webdriver
|
| 3 |
-
from selenium.webdriver.chrome.
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
from
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
driver.
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from selenium import webdriver
|
| 3 |
+
from selenium.webdriver.chrome.options import Options
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
# -------------------------
|
| 7 |
+
# FIX for huggingface_hub cached_download issue
|
| 8 |
+
# -------------------------
|
| 9 |
+
import huggingface_hub
|
| 10 |
+
if not hasattr(huggingface_hub, "cached_download"):
|
| 11 |
+
from huggingface_hub import hf_hub_download
|
| 12 |
+
huggingface_hub.cached_download = hf_hub_download
|
| 13 |
+
|
| 14 |
+
# -------------------------
|
| 15 |
+
# RAG + NLP libraries
|
| 16 |
+
# -------------------------
|
| 17 |
+
from sentence_transformers import SentenceTransformer
|
| 18 |
+
import faiss
|
| 19 |
+
import numpy as np
|
| 20 |
+
from transformers import pipeline
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# -------------------------
|
| 24 |
+
# 1️⃣ Function: Scrape website using Selenium
|
| 25 |
+
# -------------------------
|
| 26 |
+
def scrape_with_selenium(url: str):
|
| 27 |
+
chrome_options = Options()
|
| 28 |
+
chrome_options.add_argument("--headless")
|
| 29 |
+
chrome_options.add_argument("--no-sandbox")
|
| 30 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 31 |
+
|
| 32 |
+
driver = webdriver.Chrome(options=chrome_options)
|
| 33 |
+
driver.get(url)
|
| 34 |
+
time.sleep(2)
|
| 35 |
+
|
| 36 |
+
# Scrape all visible text
|
| 37 |
+
paragraphs = driver.find_elements("tag name", "p")
|
| 38 |
+
text_data = [p.text for p in paragraphs if p.text.strip()]
|
| 39 |
+
driver.quit()
|
| 40 |
+
|
| 41 |
+
return text_data
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# -------------------------
|
| 45 |
+
# 2️⃣ Function: Build FAISS Index
|
| 46 |
+
# -------------------------
|
| 47 |
+
def build_faiss_index(text_data):
|
| 48 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 49 |
+
embeddings = model.encode(text_data, convert_to_numpy=True)
|
| 50 |
+
|
| 51 |
+
dim = embeddings.shape[1]
|
| 52 |
+
index = faiss.IndexFlatL2(dim)
|
| 53 |
+
index.add(embeddings)
|
| 54 |
+
|
| 55 |
+
return model, index, text_data
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# -------------------------
|
| 59 |
+
# 3️⃣ Function: Query RAG
|
| 60 |
+
# -------------------------
|
| 61 |
+
def query_rag(question, model, index, text_data):
|
| 62 |
+
q_embedding = model.encode([question], convert_to_numpy=True)
|
| 63 |
+
D, I = index.search(q_embedding, k=3)
|
| 64 |
+
retrieved = [text_data[i] for i in I[0]]
|
| 65 |
+
|
| 66 |
+
# Generate answer using Flan-T5
|
| 67 |
+
generator = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 68 |
+
context = " ".join(retrieved)
|
| 69 |
+
prompt = f"Answer the question using the context:\nContext: {context}\nQuestion: {question}"
|
| 70 |
+
answer = generator(prompt, max_length=150, do_sample=True)[0]["generated_text"]
|
| 71 |
+
|
| 72 |
+
return answer, retrieved
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# -------------------------
|
| 76 |
+
# 4️⃣ Streamlit UI
|
| 77 |
+
# -------------------------
|
| 78 |
+
st.title("🚀 Web Scraping + RAG with Selenium")
|
| 79 |
+
|
| 80 |
+
url = st.text_input("Enter a website URL:", "https://quotes.toscrape.com/")
|
| 81 |
+
if st.button("Scrape Website"):
|
| 82 |
+
with st.spinner("Scraping website..."):
|
| 83 |
+
scraped_text = scrape_with_selenium(url)
|
| 84 |
+
st.success(f"✅ Scraped {len(scraped_text)} paragraphs!")
|
| 85 |
+
|
| 86 |
+
st.session_state["scraped_text"] = scraped_text
|
| 87 |
+
|
| 88 |
+
if "scraped_text" in st.session_state:
|
| 89 |
+
question = st.text_input("Ask a question based on scraped data:")
|
| 90 |
+
if st.button("Get Answer"):
|
| 91 |
+
model, index, text_data = build_faiss_index(st.session_state["scraped_text"])
|
| 92 |
+
answer, retrieved = query_rag(question, model, index, text_data)
|
| 93 |
+
|
| 94 |
+
st.subheader("🔍 Retrieved Context")
|
| 95 |
+
for r in retrieved:
|
| 96 |
+
st.write("-", r)
|
| 97 |
+
|
| 98 |
+
st.subheader("💡 Answer")
|
| 99 |
+
st.write(answer)
|