gaur3009 commited on
Commit
69f26d5
Β·
verified Β·
1 Parent(s): b5cde6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -36
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
- texts = [scrape_url(url) for url in urls]
 
 
 
 
 
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
- return f"## 🧠 Answer\n\n{answer}\n\n---\n### πŸ”— Sources\n" + "\n".join(f"- [{url}]({url})" for url in urls)
18
-
19
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet", secondary_hue="blue")) as demo:
20
- gr.Markdown("""
21
- # πŸ” **AI Web RAG Agent**
22
- _Ask anything, I'll search, scrape & answer in real time!_
23
- """)
24
-
25
- with gr.Column(scale=1):
26
- question = gr.Textbox(
27
- label="πŸ’‘ Your question",
28
- placeholder="e.g., Find cheapest flights Kanpur to Mumbai on 30 July",
29
- show_label=True,
30
- scale=1
31
- )
32
-
33
- btn = gr.Button("πŸš€ Ask")
34
-
35
- output = gr.Markdown("πŸ€– *Your answer will appear here...*")
36
-
37
- # Examples help new users
38
- gr.Examples(
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"![image]({img_url})\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()