Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
# app.py
|
2 |
import gradio as gr
|
3 |
from search import search_google
|
4 |
from scraper import scrape_url
|
@@ -8,42 +7,38 @@ from llm import generate_answer
|
|
8 |
vs = VectorStore()
|
9 |
|
10 |
def ask_agent(question):
|
11 |
-
urls = search_google(question, num_results=3)
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
vs.add_texts(texts)
|
14 |
relevant = vs.retrieve(question, top_k=2)
|
15 |
context = "\n\n".join(relevant)
|
|
|
|
|
16 |
answer = generate_answer(context, question)
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
examples=[
|
40 |
-
"Best laptop under 50,000 INR",
|
41 |
-
"Latest news about ISRO moon mission",
|
42 |
-
"What are some tourist places near Mumbai"
|
43 |
-
],
|
44 |
-
inputs=[question]
|
45 |
-
)
|
46 |
-
|
47 |
-
btn.click(fn=ask_agent, inputs=question, outputs=output)
|
48 |
-
|
49 |
-
demo.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from search import search_google
|
3 |
from scraper import scrape_url
|
|
|
7 |
vs = VectorStore()
|
8 |
|
9 |
def ask_agent(question):
|
10 |
+
urls = [u for u in search_google(question, num_results=3) if u.startswith("http")]
|
11 |
+
|
12 |
+
texts_images = [scrape_url(url) for url in urls]
|
13 |
+
texts = [ti[0] for ti in texts_images if not ti[0].startswith("[Error")]
|
14 |
+
images = [ti[1] for ti in texts_images] # list of list of images
|
15 |
+
|
16 |
+
# add to vector store
|
17 |
vs.add_texts(texts)
|
18 |
relevant = vs.retrieve(question, top_k=2)
|
19 |
context = "\n\n".join(relevant)
|
20 |
+
|
21 |
+
# generate answer
|
22 |
answer = generate_answer(context, question)
|
23 |
+
|
24 |
+
# build image markdown with source
|
25 |
+
image_markdown = ""
|
26 |
+
for url, imgs in zip(urls, images):
|
27 |
+
if imgs:
|
28 |
+
# show first image as thumbnail
|
29 |
+
img_url = imgs[0]
|
30 |
+
image_markdown += f"\n"
|
31 |
+
image_markdown += f"[Source]({url})\n\n"
|
32 |
+
|
33 |
+
final_output = f"## π§ Answer\n\n{answer}\n\n---\n## πΈ Images & Sources\n\n{image_markdown}"
|
34 |
+
return final_output
|
35 |
+
|
36 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as demo:
|
37 |
+
gr.Markdown("# π **AI Web RAG Agent**\nAsk me anything, I'll search, scrape text & images, and answer!")
|
38 |
+
inp = gr.Textbox(label="Your question", placeholder="e.g., Best laptop under 50,000 INR")
|
39 |
+
btn = gr.Button("Ask")
|
40 |
+
out = gr.Markdown()
|
41 |
+
|
42 |
+
btn.click(fn=ask_agent, inputs=inp, outputs=out)
|
43 |
+
|
44 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|