Spaces:
Runtime error
Runtime error
File size: 1,606 Bytes
5e827ce 9015035 fa33802 9015035 5e827ce 9015035 911a7e5 9015035 a8b2206 4e51f93 |
1 2 3 4 5 6 7 8 9 10 11 12 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
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() |