File size: 2,794 Bytes
5e827ce
 
6ac7d2a
5e827ce
 
1fab24f
5e827ce
 
 
 
1fab24f
 
 
 
 
69f26d5
1fab24f
 
 
 
 
 
 
 
 
69f26d5
1fab24f
 
 
 
 
 
 
 
5e827ce
1fab24f
 
 
 
5e827ce
1fab24f
 
 
69f26d5
 
1fab24f
 
 
 
 
 
69f26d5
1fab24f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69f26d5
1fab24f
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import gradio as gr
from search import search_google
from scraper import scrape_url
from rag import VectorStore
from llm import generate_answer
import time

vs = VectorStore()

def ask_agent(question):
    # Search Google
    with gr.Blocks(analytics_enabled=False) as progress_section:
        with gr.Row():
            gr.Textbox("Searching web...", show_label=False)
    
    urls = [u for u in search_google(question, num_results=3) if u.startswith("http")]
    if not urls:
        return "⚠️ No search results found. Try a different query."
    
    # Scrape URLs
    progress_section.children[0].children[0].value = "Scraping content..."
    texts_images = []
    for url in urls:
        texts_images.append(scrape_url(url))
    
    texts = [ti[0] for ti in texts_images if not ti[0].startswith("[Error")]
    images = [ti[1] for ti in texts_images]
    
    # Add to vector store
    if texts:
        vs.add_texts(texts)
    
    # Retrieve context
    progress_section.children[0].children[0].value = "Analyzing content..."
    relevant = vs.retrieve(question, top_k=2)
    context = "\n\n".join(relevant) if relevant else "No relevant context found."
    
    # Generate answer
    progress_section.children[0].children[0].value = "Generating answer..."
    answer = generate_answer(context, question)
    
    # Prepare image gallery
    image_gallery = []
    for url, imgs in zip(urls, images):
        if imgs:
            image_gallery.extend(imgs[:3])  # Show max 3 images per site
    
    # Prepare sources
    sources = "\n".join([f"- [{url}]({url})" for url in urls])
    
    return answer, image_gallery, sources

with gr.Blocks(
    theme=gr.themes.Soft(
        primary_hue="violet",
        font=[gr.themes.GoogleFont("Poppins")]
    ),
    css=".gradio-container {max-width: 900px !important}"
) as demo:
    gr.Markdown("""
    # 🌐 **Smart Web Research Agent**
    Ask anything - I'll search the web, analyze content, and provide answers with sources!
    """)
    
    with gr.Row():
        question = gr.Textbox(
            label="Your question", 
            placeholder="e.g., Best budget laptop 2024?",
            scale=4
        )
        submit_btn = gr.Button("Search", variant="primary", scale=1)
    
    progress = gr.Textbox(visible=False)
    
    with gr.Accordion("Answer", open=True):
        answer = gr.Markdown()
    
    with gr.Accordion("Sources", open=False):
        sources = gr.Markdown()
    
    with gr.Accordion("Images", open=False):
        gallery = gr.Gallery(
            columns=3, 
            object_fit="contain",
            height="auto"
        )
    
    submit_btn.click(
        fn=ask_agent,
        inputs=question,
        outputs=[answer, gallery, sources],
        api_name="search"
    )

demo.launch()