gaur3009 commited on
Commit
4543af6
Β·
verified Β·
1 Parent(s): 67d5453

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -59
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
- from memory import ConversationMemory
7
- from utils import async_timeout
 
8
 
9
- # Initialize conversation memory
10
- memory = ConversationMemory()
11
 
12
- @async_timeout(30) # Timeout after 30 seconds
13
- async def ask_agent(question, progress=gr.Progress()):
14
- # Track progress
15
- progress(0.1, desc="πŸ“‘ Searching the web...")
 
 
 
 
 
16
 
17
- # Retrieve conversation context
18
- context = memory.get_context()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Search for information (with caching)
21
- search_results = await cached_search_async(question, num_results=5)
22
 
23
  if not search_results:
24
- return "I couldn't find any relevant information about that. Could you try rephrasing your question?"
25
-
26
- # Track progress
27
- progress(0.4, desc="🧠 Processing information...")
28
-
29
- # Generate human-like response
30
- answer = await generate_answer(
31
- question=question,
32
- context=context,
33
- search_results=search_results
34
- )
35
-
36
- # Update conversation history
37
- memory.add_exchange(question, answer['response'])
38
-
39
- # Format response with sources
40
- formatted_response = f"""
41
- πŸ€– **Assistant**: {answer['response']}\n
42
- πŸ” **Sources I used**:\n
43
- """
44
- for source in answer['sources']:
45
- formatted_response += f"- [{source['title']}]({source['url']})\n"
46
-
47
- # Track progress
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  progress(1.0, desc="βœ… Response ready")
49
- return formatted_response
50
 
51
- # Cached async search
52
- @lru_cache(maxsize=100)
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>🧠 AI Research Assistant</h1>
61
- <p>I can help you find information on any topic!</p>
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 History")
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
- bot_message = await ask_agent(
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
- bot_message = f"⚠️ Sorry, I encountered an error: {str(e)[:100]}"
86
  status.value = "Error occurred"
87
 
88
- chat_history.append((message, bot_message))
89
  return "", chat_history
90
 
91
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
92
- clear.click(lambda: (memory.clear(), None), None, chatbot, queue=False)
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()