gaur3009 commited on
Commit
fa33802
Β·
verified Β·
1 Parent(s): 72e4742

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -1,33 +1,51 @@
 
1
  import gradio as gr
2
  from search import search_google
3
  from scraper import scrape_url
 
4
  from llm import generate_answer
 
 
 
 
5
 
6
  def ask_agent(question):
 
7
  urls = search_google(question, num_results=5)
8
-
9
  if not urls:
10
  return "❗ No search results found."
11
 
12
- first_url = urls[0]
13
- context = scrape_url(first_url)
14
-
15
- answer = generate_answer(context, question)
16
-
17
- # Show other high quality links as sources (skip first scraped one)
18
- better_links = "\n".join([f"- [{u}]({u})" for u in urls[1:]])
19
-
20
- return f"""### 🧠 **Answer**
 
 
 
 
 
 
 
 
 
 
 
21
  {answer}
22
 
23
- βœ… **Context scraped from:** [{first_url}]({first_url})
24
 
25
  πŸ“š **Other useful links:**
26
- {better_links if better_links else 'No other links'}
27
  """
28
 
 
29
  with gr.Blocks() as demo:
30
- gr.Markdown("# πŸ” Dynamic RAG agent\nScrapes *one* top site, answers, and shows more trusted links.")
31
  inp = gr.Textbox(label="Ask your question")
32
  out = gr.Markdown()
33
  btn = gr.Button("Ask")
 
1
+ # app.py
2
  import gradio as gr
3
  from search import search_google
4
  from scraper import scrape_url
5
+ from rag import VectorStore
6
  from llm import generate_answer
7
+ from summarizer import summarize_text
8
+
9
+ # initialize vector store once
10
+ vs = VectorStore()
11
 
12
  def ask_agent(question):
13
+ # Step 1: Search Google for top 5 URLs
14
  urls = search_google(question, num_results=5)
 
15
  if not urls:
16
  return "❗ No search results found."
17
 
18
+ # Step 2: Scrape text from each URL
19
+ texts = [scrape_url(url) for url in urls]
20
+
21
+ # Step 3: Add scraped texts to vector store
22
+ vs.add_texts(texts)
23
+
24
+ # Step 4: Retrieve top 3 most relevant texts to the question
25
+ relevant_texts = vs.retrieve(question, top_k=3)
26
+ context = "\n\n".join(relevant_texts)
27
+
28
+ # Step 5: Summarize the context to keep it concise
29
+ summary = summarize_text(context, max_length=100)
30
+
31
+ # Step 6: Generate final answer using LLM
32
+ answer = generate_answer(summary, question)
33
+
34
+ # Step 7: Format other URLs as markdown links
35
+ better_links = "\n".join([f"- [{u}]({u})" for u in urls])
36
+
37
+ return f"""### 🧠 **Answer**
38
  {answer}
39
 
40
+ βœ… **Context summarized from top sites**
41
 
42
  πŸ“š **Other useful links:**
43
+ {better_links}
44
  """
45
 
46
+ # Gradio interface
47
  with gr.Blocks() as demo:
48
+ gr.Markdown("# πŸ” **Dynamic RAG Agent + Summarizer**\nGoogle search β†’ scrape β†’ embed β†’ retrieve β†’ summarize β†’ answer!")
49
  inp = gr.Textbox(label="Ask your question")
50
  out = gr.Markdown()
51
  btn = gr.Button("Ask")