Spaces:
Paused
Paused
| import gradio as gr | |
| from index_manager import IndexManager | |
| from scraper import BrowserScraper | |
| # Initialize Core Services | |
| # The IndexManager handles model loading and FAISS index initialization | |
| print("π Initializing Semantic Bookmark Engines...") | |
| ai_index = IndexManager() | |
| scanner = BrowserScraper() | |
| def add_new_bookmark(url): | |
| """ | |
| Handler for adding a new bookmark. | |
| 1. Uses Selenium to scrape the actual page content. | |
| 2. Uses SentenceTransformer to create a vector embedding. | |
| 3. Adds to FAISS index. | |
| """ | |
| if not url: return "β οΈ Please enter a URL" | |
| # 1. Use Real Browser to fetch content | |
| meta = scanner.fetch_page_metadata(url) | |
| if meta["status"] == "failed": | |
| return f"β Failed to reach URL: {meta['summary']}" | |
| # 2. Use AI to vectorize and index content | |
| ai_index.add_bookmark(meta["summary"], url, meta["title"]) | |
| return f"β Indexed Successfully!\n\nTitle: {meta['title']}\nAnalyzed Content Length: {len(meta['summary'])} chars" | |
| def search_bookmarks(query): | |
| """ | |
| Handler for semantic search. | |
| Performs vector similarity search on the local FAISS index. | |
| """ | |
| if not query: return "β οΈ Please enter a search query" | |
| # 3. Perform Vector Search | |
| results = ai_index.search(query) | |
| if not results: | |
| return "π€· No relevant bookmarks found. Try adding some URLs first!" | |
| output = "" | |
| for idx, res in enumerate(results): | |
| output += f"### {idx+1}. [{res['title']}]({res['url']})\n> {res['text'][:150]}...\n\n" | |
| return output | |
| # UI Definition | |
| with gr.Blocks(title="Semantic AI Bookmarks") as app: | |
| gr.Markdown("# π Semantic AI Bookmarks") | |
| gr.Markdown("Smart bookmark manager that uses **Selenium** to crawl pages and **MiniLM AI** for vector search.") | |
| with gr.Tab("Add Bookmark"): | |
| gr.Markdown("Paste a URL below. The system will use a **headless browser** to scrape the page content and generate an **AI vector embedding**.") | |
| with gr.Row(): | |
| url_input = gr.Textbox(label="Page URL", placeholder="https://example.com", scale=4) | |
| add_btn = gr.Button("π§ Scrape & Vectorize", scale=1) | |
| add_output = gr.Textbox(label="Processing Status") | |
| add_btn.click(add_new_bookmark, inputs=url_input, outputs=add_output) | |
| with gr.Tab("Semantic Search"): | |
| gr.Markdown("Search your bookmarks using natural language. The AI understands meaning, not just keywords.") | |
| with gr.Row(): | |
| q_input = gr.Textbox(label="Search Query", placeholder="e.g. 'tutorials for deep learning'", scale=4) | |
| search_btn = gr.Button("π Find by Meaning", scale=1) | |
| search_output = gr.Markdown(label="Results") | |
| search_btn.click(search_bookmarks, inputs=q_input, outputs=search_output) | |
| if __name__ == "__main__": | |
| app.launch(server_name="0.0.0.0", server_port=7860, share=False) | |