File size: 2,923 Bytes
840261a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
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
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)