import gradio as gr
from RAG import get_retriever_links, RAG_with_memory
import requests
rag_llm = None
searched = False
DEFAULT_FAVICON_URL = "https://www.nasa.gov/favicon.ico"
def get_favicon_url(link):
from urllib.parse import urlparse
domain = urlparse(link).netloc
url = f"https://{domain}/favicon.ico"
if favicon_exists(url):
return url
return DEFAULT_FAVICON_URL
def favicon_exists(url):
try:
response = requests.head(url)
return response.status_code == 200
except requests.RequestException:
return False
def chatbot_response(message, history):
if not searched:
return "Please enter a query above before asking follow-up questions."
return rag_llm.generate(message)
# Function to simulate a search operation
def search(query):
global rag_llm
global searched
searched = True
retriever, sources = get_retriever_links(query, "./api_keys.json")
rag_llm = RAG_with_memory(retriever)
response = rag_llm.generate(query)
sources_html = ''.join([
f"""
{source['title']}