gaur3009 commited on
Commit
9015035
Β·
verified Β·
1 Parent(s): 32855ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -45
app.py CHANGED
@@ -1,54 +1,55 @@
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")
52
- btn.click(fn=ask_agent, inputs=inp, outputs=out)
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  demo.launch()
 
 
1
  import gradio as gr
2
  from search import search_google
 
 
3
  from llm import generate_answer
4
+ from memory import ConversationMemory
5
 
6
+ # Initialize conversation memory
7
+ memory = ConversationMemory()
8
 
9
  def ask_agent(question):
10
+ # Retrieve conversation context
11
+ context = memory.get_context()
12
+
13
+ # Search for information
14
+ search_results = search_google(question, num_results=5)
15
+
16
+ if not search_results:
17
+ return "I couldn't find any relevant information about that. Could you try rephrasing your question?"
18
+
19
+ # Generate human-like response
20
+ answer = generate_answer(
21
+ question=question,
22
+ context=context,
23
+ search_results=search_results
24
+ )
25
+
26
+ # Update conversation history
27
+ memory.add_exchange(question, answer)
28
+
29
+ # Format response with sources
30
+ formatted_response = f"""
31
+ πŸ€– **Assistant**: {answer['response']}
32
+
33
+ πŸ” **Sources I used**:
 
 
 
 
 
 
 
34
  """
35
+ for source in answer['sources']:
36
+ formatted_response += f"- [{source['title']}]({source['url']})\n"
37
+
38
+ return formatted_response
39
+
40
+ # Gradio chat interface
41
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
42
+ gr.Markdown("# 🧠 **AI Research Assistant**")
43
+ chatbot = gr.Chatbot(height=500)
44
+ msg = gr.Textbox(label="Your Question")
45
+ clear = gr.Button("Clear History")
46
+
47
+ def respond(message, chat_history):
48
+ bot_message = ask_agent(message)
49
+ chat_history.append((message, bot_message))
50
+ return "", chat_history
51
+
52
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
53
+ clear.click(lambda: None, None, chatbot, queue=False)
54
 
55
  demo.launch()