Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
|
12 |
def ask_agent(question):
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
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 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|