Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from search import search_google
|
| 3 |
from scraper import scrape_url
|
|
@@ -7,25 +8,42 @@ from llm import generate_answer
|
|
| 7 |
vs = VectorStore()
|
| 8 |
|
| 9 |
def ask_agent(question):
|
| 10 |
-
# Step 1: search
|
| 11 |
urls = search_google(question, num_results=3)
|
| 12 |
-
# Step 2: scrape
|
| 13 |
texts = [scrape_url(url) for url in urls]
|
| 14 |
-
# Step 3: embed + store
|
| 15 |
vs.add_texts(texts)
|
| 16 |
-
# Step 4: retrieve
|
| 17 |
relevant = vs.retrieve(question, top_k=2)
|
| 18 |
context = "\n\n".join(relevant)
|
| 19 |
-
# Step 5: generate answer
|
| 20 |
answer = generate_answer(context, question)
|
| 21 |
-
return f"
|
| 22 |
-
|
| 23 |
-
with gr.Blocks() as demo:
|
| 24 |
-
gr.Markdown("
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from search import search_google
|
| 4 |
from scraper import scrape_url
|
|
|
|
| 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()
|