Spaces:
Runtime error
Runtime error
import gradio as gr | |
from search import search_google | |
from llm import generate_answer | |
from memory import ConversationMemory | |
# Initialize conversation memory | |
memory = ConversationMemory() | |
def ask_agent(question): | |
# Retrieve conversation context | |
context = memory.get_context() | |
# Search for information | |
search_results = search_google(question, num_results=5) | |
if not search_results: | |
return "I couldn't find any relevant information about that. Could you try rephrasing your question?" | |
# Generate human-like response | |
answer = generate_answer( | |
question=question, | |
context=context, | |
search_results=search_results | |
) | |
# Update conversation history | |
memory.add_exchange(question, answer) | |
# Format response with sources | |
formatted_response = f""" | |
π€ **Assistant**: {answer['response']} | |
π **Sources I used**: | |
""" | |
for source in answer['sources']: | |
formatted_response += f"- [{source['title']}]({source['url']})\n" | |
return formatted_response | |
# Gradio chat interface | |
with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
gr.Markdown("# π§ **AI Research Assistant**") | |
chatbot = gr.Chatbot(height=500) | |
msg = gr.Textbox(label="Your Question") | |
clear = gr.Button("Clear History") | |
def respond(message, chat_history): | |
bot_message = ask_agent(message) | |
chat_history.append((message, bot_message)) | |
return "", chat_history | |
msg.submit(respond, [msg, chatbot], [msg, chatbot]) | |
clear.click(lambda: None, None, chatbot, queue=False) | |
demo.launch() |