Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,95 +1,127 @@
|
|
1 |
import gradio as gr
|
2 |
-
import asyncio
|
3 |
-
from functools import lru_cache
|
4 |
from search import search_google
|
|
|
|
|
|
|
5 |
from llm import generate_answer
|
6 |
-
|
7 |
-
from
|
|
|
8 |
|
9 |
-
# Initialize
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
#
|
21 |
-
search_results = await
|
22 |
|
23 |
if not search_results:
|
24 |
-
return "I couldn't find any relevant information
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
progress(1.0, desc="β
Response ready")
|
49 |
-
return
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
def cached_search_async(query, num_results=5):
|
54 |
-
return search_google(query, num_results)
|
55 |
-
|
56 |
-
# Gradio chat interface with progress tracking
|
57 |
-
with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 800px; margin: auto;}") as demo:
|
58 |
gr.Markdown("""
|
59 |
-
<div style="text-align: center
|
60 |
-
<h1
|
61 |
-
<p>I
|
62 |
</div>
|
63 |
""")
|
64 |
|
65 |
chatbot = gr.Chatbot(height=400, bubble_full_width=False)
|
66 |
msg = gr.Textbox(label="Your Question", placeholder="Ask me anything...")
|
67 |
-
clear = gr.Button("Clear
|
68 |
status = gr.Textbox("", label="Status", interactive=False)
|
69 |
|
70 |
async def respond(message, chat_history):
|
71 |
-
# Create progress tracker
|
72 |
tracker = []
|
73 |
-
|
74 |
-
# Wrap in try/except for better error handling
|
75 |
try:
|
76 |
-
|
77 |
-
message,
|
78 |
progress=lambda p, d, t=tracker: tracker.append((p, d))
|
79 |
-
)
|
80 |
|
81 |
# Update status
|
82 |
if tracker:
|
83 |
status.value = tracker[-1][1]
|
84 |
except Exception as e:
|
85 |
-
|
86 |
status.value = "Error occurred"
|
87 |
|
88 |
-
chat_history.append((message,
|
89 |
return "", chat_history
|
90 |
|
91 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
92 |
-
clear.click(lambda: (
|
93 |
|
94 |
if __name__ == "__main__":
|
95 |
demo.queue(concurrency_count=4).launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from search import search_google
|
3 |
+
from scraper import scrape_url
|
4 |
+
from summarizer import summarize_text
|
5 |
+
from rag import VectorStore
|
6 |
from llm import generate_answer
|
7 |
+
import asyncio
|
8 |
+
from functools import lru_cache
|
9 |
+
from concurrent.futures import ThreadPoolExecutor
|
10 |
|
11 |
+
# Initialize vector store
|
12 |
+
vs = VectorStore()
|
13 |
|
14 |
+
# Cached scraping function
|
15 |
+
@lru_cache(maxsize=100)
|
16 |
+
def cached_scrape(url):
|
17 |
+
return scrape_url(url)
|
18 |
+
|
19 |
+
async def process_search_results(query):
|
20 |
+
"""Search and scrape in parallel"""
|
21 |
+
# Step 1: Search Google for URLs
|
22 |
+
search_results = search_google(query, num_results=5)
|
23 |
|
24 |
+
if not search_results:
|
25 |
+
return None, None
|
26 |
+
|
27 |
+
# Step 2: Scrape text from each URL in parallel
|
28 |
+
with ThreadPoolExecutor(max_workers=5) as executor:
|
29 |
+
loop = asyncio.get_running_loop()
|
30 |
+
scrape_tasks = [
|
31 |
+
loop.run_in_executor(executor, cached_scrape, result['url'])
|
32 |
+
for result in search_results
|
33 |
+
]
|
34 |
+
texts = await asyncio.gather(*scrape_tasks)
|
35 |
+
|
36 |
+
return search_results, texts
|
37 |
+
|
38 |
+
async def ask_agent(question, progress=gr.Progress()):
|
39 |
+
progress(0.1, desc="π Searching the web...")
|
40 |
|
41 |
+
# Process search results
|
42 |
+
search_results, texts = await process_search_results(question)
|
43 |
|
44 |
if not search_results:
|
45 |
+
return "I couldn't find any relevant information. Please try a different question."
|
46 |
+
|
47 |
+
progress(0.3, desc="π Processing content...")
|
48 |
+
|
49 |
+
# Step 3: Summarize each text
|
50 |
+
with ThreadPoolExecutor(max_workers=3) as executor:
|
51 |
+
loop = asyncio.get_running_loop()
|
52 |
+
summarize_tasks = [
|
53 |
+
loop.run_in_executor(executor, summarize_text, text, 100)
|
54 |
+
for text in texts
|
55 |
+
]
|
56 |
+
summaries = await asyncio.gather(*summarize_tasks)
|
57 |
+
|
58 |
+
# Step 4: Add to vector store
|
59 |
+
vs.add_texts(summaries)
|
60 |
+
|
61 |
+
progress(0.6, desc="π§ Finding relevant information...")
|
62 |
+
|
63 |
+
# Step 5: Retrieve top 3 most relevant texts
|
64 |
+
relevant_texts, indices = vs.retrieve(question, top_k=3)
|
65 |
+
context = "\n\n".join(relevant_texts)
|
66 |
+
|
67 |
+
progress(0.8, desc="π‘ Generating answer...")
|
68 |
+
|
69 |
+
# Step 6: Generate final answer
|
70 |
+
answer = generate_answer(context, question)
|
71 |
+
|
72 |
+
# Format response
|
73 |
+
response = f"### π€ Assistant\n{answer}\n\n"
|
74 |
+
response += "### π Sources Used in This Answer:\n"
|
75 |
+
|
76 |
+
# Add sources used in answer
|
77 |
+
for idx in indices:
|
78 |
+
result = search_results[idx]
|
79 |
+
response += f"- [{result['title']}]({result['url']})\n"
|
80 |
+
|
81 |
+
# Add other sources
|
82 |
+
other_indices = [i for i in range(len(search_results)) if i not in indices]
|
83 |
+
if other_indices:
|
84 |
+
response += "\n### π Other Useful Sources:\n"
|
85 |
+
for idx in other_indices:
|
86 |
+
result = search_results[idx]
|
87 |
+
response += f"- [{result['title']}]({result['url']})\n"
|
88 |
+
|
89 |
progress(1.0, desc="β
Response ready")
|
90 |
+
return response
|
91 |
|
92 |
+
# Gradio interface with progress tracking
|
93 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {max-width: 800px}") as demo:
|
|
|
|
|
|
|
|
|
|
|
94 |
gr.Markdown("""
|
95 |
+
<div style="text-align: center">
|
96 |
+
<h1>π AI Research Assistant</h1>
|
97 |
+
<p>I'll search the web and summarize information for you!</p>
|
98 |
</div>
|
99 |
""")
|
100 |
|
101 |
chatbot = gr.Chatbot(height=400, bubble_full_width=False)
|
102 |
msg = gr.Textbox(label="Your Question", placeholder="Ask me anything...")
|
103 |
+
clear = gr.Button("Clear Conversation")
|
104 |
status = gr.Textbox("", label="Status", interactive=False)
|
105 |
|
106 |
async def respond(message, chat_history):
|
|
|
107 |
tracker = []
|
|
|
|
|
108 |
try:
|
109 |
+
response = await ask_agent(
|
110 |
+
message,
|
111 |
progress=lambda p, d, t=tracker: tracker.append((p, d))
|
|
|
112 |
|
113 |
# Update status
|
114 |
if tracker:
|
115 |
status.value = tracker[-1][1]
|
116 |
except Exception as e:
|
117 |
+
response = f"β οΈ Sorry, I encountered an error: {str(e)[:100]}"
|
118 |
status.value = "Error occurred"
|
119 |
|
120 |
+
chat_history.append((message, response))
|
121 |
return "", chat_history
|
122 |
|
123 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
124 |
+
clear.click(lambda: (vs.clear(), None), None, chatbot, queue=False)
|
125 |
|
126 |
if __name__ == "__main__":
|
127 |
demo.queue(concurrency_count=4).launch()
|