Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
{answer}
|
22 |
|
23 |
-
β
**Context
|
24 |
|
25 |
π **Other useful links:**
|
26 |
-
{better_links
|
27 |
"""
|
28 |
|
|
|
29 |
with gr.Blocks() as demo:
|
30 |
-
gr.Markdown("# π Dynamic RAG
|
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")
|