import gradio as gr
import requests
from bs4 import BeautifulSoup, Comment
import os
from llama_cpp import Llama

def tag_visible(element):
    if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
        return False
    if isinstance(element, Comment):
        return False
    return True

def get_text_from_url(url):
    response = requests.get(url, timeout=10)
    soup = BeautifulSoup(response.text, 'html.parser')
    # Use 'string=True' instead of deprecated 'text=True'
    texts = soup.find_all(string=True)
    visible_texts = filter(tag_visible, texts)
    return " ".join(t.strip() for t in visible_texts)

# Pre-fetch and truncate homepage text
text_list = []
homepage_url = "https://sites.google.com/view/abhilashnandy/home/"
extensions = ["", "pmrf-profile-page"]

for ext in extensions:
    try:
        full_text = get_text_from_url(homepage_url + ext)
        truncated_text = full_text[:2000]  # Adjust truncation length as needed
        text_list.append(truncated_text)
    except Exception as e:
        text_list.append(f"Error fetching {homepage_url+ext}: {str(e)}")

CONTEXT = " ".join(text_list)

# Set the model path. Make sure the model file is downloaded and placed in the 'models' directory.
model_path = "TheBloke/Mistral-7B-Instruct-v0.1-GGUF"
if not os.path.exists(model_path):
    raise ValueError(f"Model file not found at {model_path}. Please download the model file and place it in the 'models' folder.")

llm = Llama(model_path=model_path, n_ctx=4096, n_threads=6, verbose=False)

def answer_query(query):
    prompt = (
        "You are an AI chatbot answering queries based on Abhilash Nandy's homepage. "
        "Provide concise answers (under 30 words).\n\n"
        f"Context: {CONTEXT}\n\nUser: {query}\nAI:"
    )
    response = llm(prompt, max_tokens=50, stop=["\nUser:", "\nAI:"], echo=False)
    return response["choices"][0]["text"].strip()

iface = gr.Interface(
    fn=answer_query,
    inputs=gr.Textbox(lines=2, placeholder="Ask a question about Abhilash Nandy's homepage..."),
    outputs="text",
    title="Homepage QA Chatbot",
    description="A chatbot answering queries based on homepage context."
)

if __name__ == '__main__':
    iface.launch()