File size: 1,503 Bytes
5e827ce
 
6ac7d2a
5e827ce
 
 
 
 
 
69f26d5
 
 
 
 
 
 
5e827ce
 
 
69f26d5
 
5e827ce
69f26d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from search import search_google
from scraper import scrape_url
from rag import VectorStore
from llm import generate_answer

vs = VectorStore()

def ask_agent(question):
    urls = [u for u in search_google(question, num_results=3) if u.startswith("http")]

    texts_images = [scrape_url(url) for url in urls]
    texts = [ti[0] for ti in texts_images if not ti[0].startswith("[Error")]
    images = [ti[1] for ti in texts_images]  # list of list of images

    # add to vector store
    vs.add_texts(texts)
    relevant = vs.retrieve(question, top_k=2)
    context = "\n\n".join(relevant)

    # generate answer
    answer = generate_answer(context, question)

    # build image markdown with source
    image_markdown = ""
    for url, imgs in zip(urls, images):
        if imgs:
            # show first image as thumbnail
            img_url = imgs[0]
            image_markdown += f"![image]({img_url})\n"
        image_markdown += f"[Source]({url})\n\n"

    final_output = f"## 🧠 Answer\n\n{answer}\n\n---\n## πŸ“Έ Images & Sources\n\n{image_markdown}"
    return final_output

with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as demo:
    gr.Markdown("# πŸ” **AI Web RAG Agent**\nAsk me anything, I'll search, scrape text & images, and answer!")
    inp = gr.Textbox(label="Your question", placeholder="e.g., Best laptop under 50,000 INR")
    btn = gr.Button("Ask")
    out = gr.Markdown()

    btn.click(fn=ask_agent, inputs=inp, outputs=out)

demo.launch()