Spaces:
Sleeping
Sleeping
Download app.py from Biryani123/Cafe: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/spaces/Biryani123/Cafe/resolve/main/app.py
- Command line
-
hf download hf://spaces/Biryani123/Cafe/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Biryani123/Cafe/resolve/main/app.py
2.67 kB
| import gradio as gr | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import numpy as np | |
| import faiss | |
| from sentence_transformers import SentenceTransformer | |
| from transformers import pipeline | |
| # Models | |
| embed_model = SentenceTransformer("all-MiniLM-L6-v2") | |
| llm = pipeline("text2text-generation", model="google/flan-t5-small") | |
| texts = [] | |
| index = None | |
| def fetch_website(url): | |
| global texts, index | |
| texts, index = [], None | |
| if not url.startswith("http"): | |
| return "β Please enter a valid URL starting with http or https" | |
| try: | |
| response = requests.get(url, timeout=10) | |
| except Exception as e: | |
| return f"β Failed to fetch website: {e}" | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| # Remove scripts & styles | |
| for tag in soup(["script", "style", "noscript"]): | |
| tag.decompose() | |
| content = soup.get_text(separator="\n") | |
| lines = [line.strip() for line in content.splitlines() if len(line.strip()) > 40] | |
| if not lines: | |
| return "β οΈ No readable text found on website" | |
| # Chunk text | |
| chunk = "" | |
| for line in lines: | |
| if len(chunk) < 500: | |
| chunk += " " + line | |
| else: | |
| texts.append(chunk.strip()) | |
| chunk = line | |
| if chunk: | |
| texts.append(chunk.strip()) | |
| embeddings = embed_model.encode(texts) | |
| embeddings = np.array(embeddings, dtype=np.float32) | |
| index = faiss.IndexFlatL2(embeddings.shape[1]) | |
| index.add(embeddings) | |
| return f"β Website indexed successfully. Text chunks: {len(texts)}" | |
| def ask_question(question): | |
| if index is None: | |
| return "β Please fetch a website first" | |
| q_embed = embed_model.encode([question]) | |
| q_embed = np.array(q_embed, dtype=np.float32) | |
| _, I = index.search(q_embed, 1) | |
| context = texts[I[0][0]] | |
| prompt = f""" | |
| You are a Website Content Assistant. | |
| Answer ONLY using the website content. | |
| If the answer is not present, say "Not found on the website". | |
| Website Content: | |
| {context} | |
| Question: | |
| {question} | |
| Answer: | |
| """.strip() | |
| out = llm(prompt, max_new_tokens=180) | |
| return out[0]["generated_text"] | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# π Website Content AI Assistant (RAG)") | |
| gr.Markdown("Enter a website URL, fetch its content, and ask questions about it.") | |
| url = gr.Textbox(label="Website URL", placeholder="https://example.com") | |
| fetch_btn = gr.Button("Fetch Website") | |
| status = gr.Textbox(label="Status", lines=2) | |
| fetch_btn.click(fetch_website, url, status) | |
| question = gr.Textbox(label="Ask a question") | |
| answer = gr.Textbox(label="Answer", lines=6) | |
| question.submit(ask_question, question, answer) | |
| demo.launch() | |