Spaces:
Runtime error
Runtime error
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"\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() | |